JDBC connection to Oracle database and jdbcoracle Database

Source: Internet
Author: User

JDBC connection to Oracle database and jdbcoracle Database

I remember when I was in my sophomore year, we took a java course. At that time, the teacher said JDBC, and I didn't know how to learn it well. Until now, I didn't know what JDBC was, I know that this stuff can extract data from the database. So what is JDBC?

JDBC is the abbreviation of Java DataBase Connectivity. It means a java DataBase connection. I don't need to explain it any more!

So how can we establish such a connection? Once it is used, you will find that it is actually very simple. Remember several steps and import the necessary driver package. Below are several important steps:

The first step is to import necessary database driver packages in your project environment. Here I use the Demo written in the Oracle 11g database as an example, I imported it to the project and added it to the ClassPath of the WEB project. For Oracle, The class12.jar package can be imported now.

Step 2: write the code and instantiate your driver package:

Class. forName ("oracle. jdbc. driver. OracleDriver"); // modify the value of the string parameter in other databases.
Step 3: create a connection. here we need to use the DriverManager Driver Class.

Connection conn = DriverManager. getConnection ("jdbc: oracle: thin: @ localhost: 1521: cgzs", "system", "hj123"); // when I first started learning, all the teachers talked about this operation class, in fact, this is rarely used now. For details, I will introduce it in another blog.
Step 4: if the driver is instantiated and the connection is also obtained, start querying the database. We need a database SQL status object and a query result set receiving object, statement object and ResultSet object

Statement s = conn.createStatement();ResultSet rs = null;rs = s.executeQuery("select ID from A");while(rs.next()){System.out.println(rs.getString("ID"));}
Last step: Do not forget. The Statement object we used earlier, the ResultSet object and the Connection object both need to close the Connection, otherwise it will be finished.

Okay, that's all. It's easy. I wrote a complete Demo on the following!

Public class Demo {/*** @ param args * @ throws SQLException * @ throws ClassNotFoundException */public static void main (String [] args) throws SQLException, classNotFoundException {// TODO Auto-generated method stub // instantiate the driver Class. forName ("oracle. jdbc. driver. oracleDriver "); // establish Connection conn = DriverManager. getConnection ("jdbc: oracle: thin: @ localhost: 1521: cgzs", "system", "hj123"); // The statement object is used to interact with the database. Statement s = conn. createStatement (); // execute the query operation ResultSet rs = s.exe cuteQuery ("select * from V_CANCLE"); while (rs. next () {// print the query result System. out. println (rs. getString ("WWID");} // closes the connection, which is critical to conn. close (); s. close (); rs. close ();}}










Jdbc connection to oracle Database

Exception in thread "main" java. lang. NoClassDefFoundError: oracle/dms/console/DMSConsole
Mainly look at this line, NoClassDefFoundError
NoClassDefFoundError:

1. Windows is case-insensitive. You have incorrect file names. They are case-insensitive to public class names.
2. A Class in the static {} initialization code block or static Clz instance = new Clz (); new Clz () throws an exception. Such a class will automatically execute class-Level Initialization code after the first loading, such as the Code in static variables and static {}. if an exception is not processed, class initialization will fail, this class exists and is successfully loaded, but an error occurred while define the class after loading. Therefore, the wrong name is NoClassDefFoundError instead of ClassNotFound. It is indeed found, but define fails.

From the perspective of your case, there are certainly not 1st cases.
Therefore, you need to find out from the log what causes an error during Hibernate initialization. Is it possible for your code to initialize a Hibernate Code directly try {...} catch (Exception e) {/* a newbie often does nothing here and does not even print a log */}. If a newbie writes such code, you cannot guess the cause, the only way you can do is to manually check the code and read it one by one.

Jdbc connection to oracle Database

Exception in thread "main" java. lang. NoClassDefFoundError: oracle/dms/console/DMSConsole
Mainly look at this line, NoClassDefFoundError
NoClassDefFoundError:

1. Windows is case-insensitive. You have incorrect file names. They are case-insensitive to public class names.
2. A Class in the static {} initialization code block or static Clz instance = new Clz (); new Clz () throws an exception. Such a class will automatically execute class-Level Initialization code after the first loading, such as the Code in static variables and static {}. if an exception is not processed, class initialization will fail, this class exists and is successfully loaded, but an error occurred while define the class after loading. Therefore, the wrong name is NoClassDefFoundError instead of ClassNotFound. It is indeed found, but define fails.

From the perspective of your case, there are certainly not 1st cases.
Therefore, you need to find out from the log what causes an error during Hibernate initialization. Is it possible for your code to initialize a Hibernate Code directly try {...} catch (Exception e) {/* a newbie often does nothing here and does not even print a log */}. If a newbie writes such code, you cannot guess the cause, the only way you can do is to manually check the code and read it one by one.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.