Connecting to Oracle Database in Java

This article is a step by step guide on connecting to an Oracle database from a Java program. The following standalone program demonstrates Oracle database connectivity using JDBC API. In this program we will use the Oracle's built dummy table DUAL to retrieve Oracle's system date.

Java provides a standard interface to connect to databases and it is known as JDBC (Java Database Connectivity). Oracle provides an implementation library of this JDBC interface using which Java programs can connect to a running oracle database instance.

Step 1 : Download Oracle JDBC Drivers

You can download Oracle JDBC drivers from here. Choose the version appropriate for your database version. In this example, I use the Oracle 11g JDBC driver since I connect to Oracle 11g database. There are two versions available for Oracle 11g, ojdbc5.jar (for JDK 1.5) and ojdbc6.jar (for JDK 1.6). Use appropriate version for your Java installation (Oracle now requires online registration for downloading drivers). I use ojdbc6.jar for this tutorial.

Step 2 : Java Program to Connect to Oracle

The following Java program uses Oracle JDBC driver to connect to a running Oracle database instance. You can use this program on any Oracle database as this example uses Oracle's built-in dummy table DUAL for fetching system date. DUAL enables us to get values such as system date using a normal SQL query.

// Example Java Program - Oracle Database Connectivity
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class OracleSample {

    public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:XE";
    public static final String DBUSER = "system";
    public static final String DBPASS = "manager";

    public static void main(String[] args) throws SQLException {
        
        // Load Oracle JDBC Driver
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        
        // Connect to Oracle Database
        Connection con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);

        Statement statement = con.createStatement();

        // Execute a SELECT query on Oracle Dummy DUAL Table. Useful for retrieving system values
        // Enables us to retrieve values as if querying from a table
        ResultSet rs = statement.executeQuery("SELECT SYSDATE FROM DUAL");
        
        
        if (rs.next()) {
            Date currentDate = rs.getDate(1); // get first column returned
            System.out.println("Current Date from Oracle is : "+currentDate);
        }
        rs.close();
        statement.close();
        con.close();
    }
}

Before you run the program ensure that you change the values for DBURL, DBUSER and DBPASS. DBURL is of the form,

jdbc:oracle:thin:@machinename:1521:databasename

Replace machinename with the name of the machine where oracle is running and replace databasename with service name of the database instance.

See this page for more details on JDBC API.

Step 3 : Add ojdbc.jar to Classpath

In order to compile or run the above program, you need to add ojdbc.jar to the classpath of your program. If you are using IDE such as NetBeans or Eclipse, you can add ojdbc.jar as a dependent library and NetBeans will automatically add it to classpath.

If you are running the above program from command line, copy ojdbc.jar to the folder where the above Java program is located and then compile the file using the following command (this adds ojdbc.jar to classpath),

javac -classpath ./ojdbc6.jar OracleSample.java

Run the Java program using the following command (ojdbc.jar is added to classpath),

java -classpath "./ojdbc6.jar;." OracleSample

Note that when you are running OracleSample, you need both the JDCB jar file and the current folder in the classpath. If everything went well, you will see the following output.

Connecting to Oracle database using Java - Example

If you don't have an oracle instance for testing this program, you can download the free Oracle 11g Express edition from here.