Simple Java Program to connect Oracle database by using Oracle JDBC thin driver
GOAL

To create a simple Java program that connects with an Oracle database and get some data from V$DATABASE view.

DEMO

1 Create DEMO user

-- DROP USER DEMO CASCADE;

CREATE USER DEMO IDENTIFIED BY DEMO;
GRANT CONNECT TO DEMO;
GRANT SELECT ON SYS.V_$DATABASE TO DEMO;

 
2 Create a directory where your java program is to be created

mkdir /home/oracle/java_demo

 
3Set up 3 Oracle environment variables

NOTE

In this example I'm using Oracle database 19c. But the same steps should be valid for other Oracle database releases.

## Current value of ORACLE_HOME variable 

echo $ORACLE_HOME/
/u01/app/oracle/product/19c/

## All required variables are based on ORACLE_HOME variable 

export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export CLASSPATH=$ORACLE_HOME/jdbc/lib/ojdbc8.jar:/home/oracle/java_demo
export PATH=$ORACLE_HOME/jdk/bin:$PATH

 
4In a directory created in step 2 create a file with the following content

NOTE

A file name can be anything you want, for instance JavaDemo.java. A class name of a java program can be anything you want, for instance class JavaDemo

// JavaDemo.java

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;

class JavaDemo
{
   public static void main (String args []) throws SQLException
   {

OracleDataSource ods = null;
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;

      // Create DataSource and connect to the local database
      
      ods = new OracleDataSource();
      ods.setURL("jdbc:oracle:thin:@//localhost:1521/tst19c.vmware.local");
      ods.setUser("DEMO");
      ods.setPassword("DEMO");
      conn = ods.getConnection();

try
{
      // Query the database
      stmt = conn.createStatement ();
      rset = stmt.executeQuery ("SELECT NAME, DBID, TO_CHAR(CREATED,'DD-MON-YYYY HH24:MI') CREATED FROM V$DATABASE");

      // Print out query result
      while (rset.next ())
         System.out.println (rset.getString(1) +" " +rset.getString(2) +" " +rset.getString(3));
    }

      // Close the result set, statement, and the connection

finally{
      if(rset!=null) rset.close();
      if(stmt!=null) stmt.close();
      if(conn!=null) conn.close();
}
   }
}

For instance, the following file has been created

## Created file

oracle@dbpilot~/java_demo:<tst19c> pwd
/home/oracle/java_demo

oracle@dbpilot~/java_demo:<tst19c> ls -ltrh
total 4.0K
-rw-r--r--. 1 oracle dba 1.2K Apr 23 21:44 JavaDemo.java
NOTE

Modify the following lines to meet your database connection parameters

ods.setURL("jdbc:oracle:thin:@//hostname:port/service_name");
ods.setUser("YOUR_USERNAME");
ods.setPassword("YOUR_PASSWORD");

 
5 Complile a file created in step 4 with javac command

## Location of a file

oracle@dbpilot~/java_demo:<tst19c> pwd
/home/oracle/java_demo

oracle@dbpilot~/java_demo:<tst19c> ls -ltrh
total 4.0K
-rw-r--r--. 1 oracle dba 1.2K Apr 23 21:44 JavaDemo.java

## Compile a file 

oracle@dbpilot~/java_demo:<tst19c> javac JavaDemo.java

## javac command will create a binary file 

oracle@dbpilot~/java_demo:<tst19c> ls -ltrh
total 8.0K
-rw-r--r--. 1 oracle dba 1.2K Apr 23 21:44 JavaDemo.java
-rw-r--r--. 1 oracle dba 1.6K Apr 23 21:46 JavaDemo.class

 
6Run compiled java program with java command. A program will return a database name, a database DBID and time of database creation.

oracle@dbpilot~/java_demo:<tst19c> java JavaDemo
TST19C 1170886558 26-DEC-2021 14:17
NOTE

java command is able to find your compiled program because of defined CLASSPATH variable that is pointed to location where compiled java program is placed.


oracle@dbpilot~/java_demo:<tst19c> echo $CLASSPATH
/u01/app/oracle/product/19c/jdbc/lib/ojdbc8.jar:/home/oracle/java_demo

That's it. We've created a simple java program that gets some data from V$DATABASE view.

 
7Drop DEMO user and remove created files

-- DROP USER DEMO CASCADE;

## rm JavaDemo.java JavaDemo.class

Written At
23 APRIL 202322:15
Red Hat Release
7.x x64
Database Release
19.13.0.0.0