5 Ways to Connect Java to Oracle DB(Data Base)

Last Updated on June 3, 2021 by admin

To Connect Java to the Oracle DB information base, we need to follow 5 steps.

In this model, we are utilizing Oracle 10g as the data set.

So we want to know the following data for the oracle information base:

  • Driver class: The driver class for the oracle data set is oracle.JDBC.driver.OracleDriver.
  • Association URL: The association URL for the oracle10G information base is JDBC:oracle:thin:@localhost:1521:xe where JDBC is the API, oracle is the data set, meager is the driver, localhost is the worker name on which oracle is running, we may likewise utilize IP address, 1521 is the port number and XE is the Oracle administration name.
  • You may get every one of these data from the tnsnames.ora document.
  • Username: The default username for the oracle data set is the framework.
  • Password: It is the secret word given by the client at the hour of introducing the oracle data set.

5 ways to connect Java to Oracle DB

Before establishing the connection, let’s first create a table in the oracle database.

Following is the SQL query to create a table.

Create table emp(id number(10),name jon2(30)age number 30

In this example, we are connecting to an Oracle database and getting data from the emp table.

Here, the system and oracle are the username and password of the Oracle database.


Import java.sql.*; 

Class OracleCon{ 

Public static void main(String args[]){ 

Try{ 

step1 load the driver class


Class.forName(“oracle.jdbc.driver.OracleDriver”); 

step2 create  the connection object


Connection con=DriverManager.getConnection( 

“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”oracle”); 

step3 create the statement object


Statement stmt=con.createStatement(); 

step4 execute query


ResultSet rs=stmt.executeQuery(“select * from emp”); 

While(rs.next()) 

System.out.println(rs.getInt(1)+”  “+rs.getString(2)+”  “+rs.getString(3)); 

step5 close the connection object


Con.close(); 

catch(Exception e)

{ System.out.println€;} 

To connect the java application with the Oracle database ojdbc14.jar file is required to be loaded.

Two ways to load the jar file:

  • Paste the ojdbc14.jar file in jre/lib/ext folder
  • Set classpath

Register JBDC Driver

You should enroll the driver in your program before you use it.

Registering the driver is the interaction by which the Oracle driver’s class record is loaded into the memory, so it tends to be used as an execution of the JDBC interfaces.

You need to do this enlistment just a single time in your program.

You can register a driver in one of two different ways.

After you’ve loaded the driver, you can build up an association utilizing the DriverManager.getConnection() strategy.

For simple reference, let me list the three over-burden DriverManager.getConnection() techniques:

By using the Connection and pass out the URL with string function.

getConnection(String URL, Properties prop)

getConnection(String URL, String client, String secret phrase)

Here each form requires an information base URL.

A data set URL is a location that focuses on your information base.

Defining a data set URL is the place where a large portion of the issues related to setting up an association happens.

Create Connection Object

We have listed down three forms of the DriverManager.getConnection() method to create a connection object.

Using a Database URL with a username and password

The most commonly used form of getConnection() requires you to pass a database URL, a username, and a password.

If you are using Oracle’s thin driver, you’ll specify a host:port:databaseName value for the database portion of the URL.

A second form of the DriverManager.getConnection( ) method requires only a database URL –

DriverManager.getConnection(String url);

The database URL includes the username and password and has the following general form


Jdbc:oracle:driver:username/password@database

So, the above connection can become create as follows –

String URL = “jdbc:oracle:thin:username/password@amrood:1521:EMP”;

Connection conn = DriverManager.getConnection(URL);

Using a Database URL and a Properties Object

A third form of the DriverManager.getConnection( ) method requires a database URL and a Properties object –

DriverManager.getConnection(String URL, Properties info);

A Properties object holds a set of keyword value pairs.

It uses to pass driver properties to the driver during a call to the getConnection() method.

Closing JDBC Connections:

Toward the finish of your JDBC program, it is required expressly to close every one of the associations with the information base to end every data set meeting.

Be that as it may, on the off chance that you neglect, Java’s garbage man will close the association when it tidies up old items.

To ensure that a connection close, you could provide a ‘finally’ block in your code.

A final block always executes, regardless of an exception occurs or not.

Read More: 4 Simple ways to use Python global function?

Leave a Comment