JDBC Pure-drive connection to Oracle
1 Download Oracle-supplied driver packages
Download Address:
Http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html
Which version to download, related to the JDK version you use, such as using JDK1.5, download Ojdbc5.jar. If you use JDK1.6, download Ojdbc6.jar. Because there is no ojdbc7.jar or Ojdbc8.jar, if you use JDK1.7 or JDK1.8, you also download Ojdbc6.jar.
If you have installed Oracle server-side or client, you do not need to download the driver package again. The JDBC driver package directory for the ORACLE server is located under $oracle_home\jdbc\lib.
For example, the author installed the Oracle 11g server side in the D:\oracle directory, the driver package is located in the
D:\oracle\product\11.2.0\dbhome_1\jdbc\lib
2 Introducing the driver package into the project
The author's integrated development environment uses the Intellijidea 14.0.1,JDK version of 1.7. The driver package should use Ojdbc6.jar.
(1) Establish the project, the project name is random, for example MYJDBC.
(2) Click "File" à "projectstructure ..." in the upper left corner menu bar.
(3) In the pop-up interface select "Modules" à "dependencies" à right "+" button, in the pop-up right side of the small window to select the second option "Library ..."
(4) In the pop-up "Choose Libraries" window, select the bottom of the "New Library" à "Java", pop-up "select Library Files" window,
(5) Find D:\oracle\product\11.2.0\dbhome_1\jdbc directory, select Ojdbc6.jar
(6) Select a good driver package interface as shown in the following figure, click on the bottom of the "Add Selected" button
(7) The following figure shows the driver package has been added to the MYJDBC project, click on the bottom of the "OK" button
(8) Return to the project main interface, you can see the lower left corner in addition to JDK1.7, Ojdbc6.jar also been added in the
3 Writing Programs
Create a Java class with a random name, such as Jdbctest. The code is as follows:
ImportJava.sql.Connection;
ImportJava.sql.DriverManager;
ImportJava.sql.ResultSet;
ImportJava.sql.SQLException;
ImportJava.sql.PreparedStatement;
ImportJava.util.Date;
Public classjdbctest{
publicstatic voidMain (string[] args) {
String Driver ="Oracle.jdbc.OracleDriver"; Driver identifier
String URL ="Jdbc:oracle:thin: @localhost: 1521:orcl"; Link string
url = "JDBC:ORACLE:THIN:@10.0.30.64:1521:ORCL"; Connecting to a remote database can be written like this
String user ="Scott."; User name of the database
String Password ="Tiger"; Password for the database
Connection con =NULL;
PreparedStatement pstm =NULL;
ResultSet rs =NULL;
BooleanFlag =false;
Try{
Class.forName (driver);
con = drivermanager.getconnection (url,user, password);
String sql ="SELECT * from emp";
Pstm =con.preparestatement (SQL);
rs = Pstm.executequery ();
while(Rs.next ()) {
intEmpno = Rs.getint ("Empno");
String ename =rs.getstring ("Ename");
DoubleSal = Rs.getdouble ("Sal.");
Date HireDate =rs.getdate ("HireDate");
intDeptno = Rs.getint ("Deptno"));
System. out. println (Empno +"\ T"+ ename +"\ T"+ Sal +"\ T"+ HireDate +"\ T"+ Deptno);
}
Flag =true;
}Catch(ClassNotFoundException e) {
E.printstacktrace ();
}
Catch(SQLException e) {
E.printstacktrace ();
}
finally{
if(Rs!=NULL) {
Try{
Rs.close ();
}Catch(SQLException e) {
E.printstacktrace ();
}
}
Close Execution channel
if(Pstm!=NULL) {
Try{
Pstm.close ();
}Catch(SQLException e) {
E.printstacktrace ();
}
}
Close Connection Channel
Try{
if(Con!=NULL&& (!con.isclosed ()) {
Try{
Con.close ();
}Catch(SQLException e) {
E.printstacktrace ();
}
}
}Catch(SQLException e) {
E.printstacktrace ();
}
}
if(flag) {
System. out. println ("execution succeeded." ");
}Else{
System. out. println ("Execution failed. ");
}
}
}
4 Running Results
5 Verification
Login to the database, query the data in the scott.emp, you can see the relevant information and the results of the above operation is consistent.