You are on page 1of 5

This JDBC tutorial helps you write Java code to establish database connection with an Oracle

database server – the first step to have Java applications working with one of the most popular
database systems. Suppose you already had a version of Oracle database installed, such as Oracle
Database Express Edition.
Table of content:

1.
1. Download JDBC driver library for Oracle database
2. JDBC database URL for Oracle database
3. Register Oracle JDBC driver
4. Establish connection to Oracle database
5. Java Connect to Oracle Database Example program

 
1. Download JDBC driver library for Oracle database
To make a Java program talks with Oracle database, we need to have the Oracle JDBC driver
(OJDBC) present in the classpath. Click here to visit Oracle’s JDBC driver download page. Then
select the JDBC driver version that matches Oracle database sever and JDK installed on your
computer. Currently there are two main versions of OJDBC:
- OJDBC 8: certified with JDK 8, for Oracle database 11g and 12c.
- OJDBC 10: certified with JDK 10, for Oracle database 18c and 19c.
NOTE:  Oracle requires users to have an Oracle account for downloading, so you may have to
register an account if you don’t have one.
Extract the downloaded archive, and place the ojdbc10.jar file under your project’s classpath as
usual as using any jar file.
If you use Maven, add the following dependency to the pom.xml file:
1 <dependency>
2     <groupId>com.oracle</groupId>
3     <artifactId>ojdbc8</artifactId>
4     <version>1.0</version>
5     <scope>system</scope>
6     <systemPath>d:/Path/To/Oracle/JDBC/ojdbc8.jar</systemPath>
7 </dependency>

2. JDBC database URL for Oracle database


The syntax of database URL for Oracle database is as follows:
jdbc:oracle:<drivertype>:@<database>

jdbc:oracle:<drivertype>:<user>/<password>@<database>

Where:

 drivertype  can be thin, oci or kprb.


 database  can be in the form of  hostname:port:SID  or a  TNSNAMES  entry listed in
the file  tnsnames.ora  reside on the client computer. The default port is 1521.

Oracle categorizes their JDBC driver into four different types, as described in the following table:
Driver type Usage drivertype
Thin Driver For client-side use without an Oracle installation thin
OCI Driver For client-side use with an Oracle installation oci
Server-Side Thin Driver Same as Thin Driver, but runs inside an Oracle thin
server to access a remote server
Server-Side Internal Driver Runs inside the target server kprb
 
According to Oracle, if your JDBC client and Oracle database server are running on the same
machine, you should use the OCI Driver because it is much faster than the Thin Driver (The OCI
Driver can use Inter Process Communication – IPC, whereas the Thin Driver can use only network
connection).
For example, if you want to connect user  tiger  with password  scott  to an Oracle database with
SID  productDB  through default port on host  dbHost  using the Thin Driver, you can construct the
URL as follows:
1 String url = “jdbc:oracle:thin:tiger/scott@dbHost:1521:productDB”
If using the OCI Driver:
1 String url = “jdbc:oracle:oci:tiger/scott@localhost:1521:productDB”
2 String url = “jdbc:oracle:oci:tiger/scott@dbHost:1521:productDB”
If you have a  TNSNAMES  entry  productionDB  in the  tnsnames.ora  file, you can construct the
URL as follows:
1 String url = “jdbc:oracle:oci:@productionDB”
For the Server-Side Thin Driver, use the same URL as the Thin Driver.
For the Server-Side Internal Driver, use the following URLs:
1 String url = "jdbc:oracle:kprb:"
2 String url = "jdbc:default:connection:"
Because in that environment, the driver actually runs within a default session, and the client is
always connected so the connection should never be closed.
 
3. Register Oracle JDBC driver
The Oracle JDBC driver class name is oracle.jdbc.OracleDriver.You can register this driver as
follows:
1 DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
or:
1 Class.forName("oracle.jdbc.OracleDriver");
NOTE:Since Java 6 (JDBC 4.0), registering the driver explicitly as above becomes optional. As long
as we put the ojdbc10.jar file in the classpath, JDBC driver manager can detect and load the driver
automatically.
 
4. Establish connection to Oracle database
With JDBC, we can establish a database connection by calling the method getConnection() of
the DriverManager class. There are three versions of this method:

o static Connection getConnection(String url)
o static Connection getConnection(String url, Properties info)
o static Connection getConnection(String url, String user, String password)

So we can have three ways for making a connection as follows:


Using only database URL for everything
In this method, we specify all connection properties in a single URL string, for example:
1 String dbURL = "jdbc:oracle:thin:tiger/scott@localhost:1521:productDB";
2 Connection conn = DriverManager.getConnection(dbURL);
3 if (conn != null) {
4     System.out.println("Connected");
5 }
 
That uses the Thin Driver to connect the user tiger with password scott to the database
SID productDB running on the same machine through the default port 1521.
Using database URL, username and password
In this method, we pass the username and password as additional arguments to the
method getConnetion(), for example:
1 String dbURL = "jdbc:oracle:thin:@localhost:1521:productDB";
2 String username = "tiger";
3 String password = "scott";
4 Connection conn = DriverManager.getConnection(dbURL, username, password);
 
Using database URL and Properties object
In this method, we use a java.util.Properties object to hold username, password and other
additional properties. For example:
1 String dbURL = "jdbc:oracle:oci:@ProductDB";
2 Properties properties = new Properties();
3 properties.put("user", "scott");
4 properties.put("password", "tiger");
5 properties.put("defaultRowPrefetch", "20");
6  
7 Connection conn = DriverManager.getConnection(dbURL, properties);
In this example, we are using the OCI Driver with a TNSNAMES entry ProductDB, and specifying an
additional property defaultRowPrefetch which is the number of rows to prefetch from the server.
 
5. Java Connect to Oracle Database Example program
To demonstrate, we create a small example program below that establishes three different
connections in 3 ways mentioned above, and finally close all the connections:
1 package net.codejava.jdbc;
2  
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.SQLException;
6 import java.util.Properties;
7  
8 /**
9  * This program demonstrates how to make database connection with Oracle
10  * database server.
11  * @author www.codejava.net
12  *
13  */
14 public class JdbcOracleConnection {
15  
16     public static void main(String[] args) {
17  
18         Connection conn1 = null;
19         Connection conn2 = null;
20         Connection conn3 = null;
21  
22         try {
23             // registers Oracle JDBC driver - though this is no longer required
24             // since JDBC 4.0, but added here for backward compatibility
25             Class.forName("oracle.jdbc.OracleDriver");
26  
27             // METHOD #1
28             String dbURL1 = "jdbc:oracle:thin:tiger/scott@localhost:1521:productDB";
29             conn1 = DriverManager.getConnection(dbURL1);
30             if (conn1 != null) {
31                 System.out.println("Connected with connection #1");
32             }
33  
34             // METHOD #2
35             String dbURL2 = "jdbc:oracle:thin:@localhost:1521:productDB";
36             String username = "tiger";
37             String password = "scott";
38             conn2 = DriverManager.getConnection(dbURL2, username, password);
39             if (conn2 != null) {
40                 System.out.println("Connected with connection #2");
41             }
42  
43             // METHOD #3
44             String dbURL3 = "jdbc:oracle:oci:@ProductDB";
45             Properties properties = new Properties();
46             properties.put("user", "tiger");
47             properties.put("password", "scott");
48             properties.put("defaultRowPrefetch", "20");
49             conn3 = DriverManager.getConnection(dbURL3, properties);
50  
51             if (conn3 != null) {
52                 System.out.println("Connected with connection #3");
53             }
54         } catch (ClassNotFoundException ex) {
55             ex.printStackTrace();
56         } catch (SQLException ex) {
57             ex.printStackTrace();
58         } finally {
59             try {
60                 if (conn1 != null && !conn1.isClosed()) {
61                     conn1.close();
62                 }
63                 if (conn2 != null && !conn2.isClosed()) {
64                     conn2.close();
65                 }
66                 if (conn3 != null && !conn3.isClosed()) {
67                     conn3.close();
68                 }
69             } catch (SQLException ex) {
70                 ex.printStackTrace();
71             }
72         }
73     }
74 }

You might also like