Understanding JDBC with Oracle

JDBC (Java Database Connectivity) is a Java API used to connect a Java application with an Oracle Database and execute SQL statements. In the non-Maven approach, we manually download the Oracle JDBC driver JAR and include it in the classpath while compiling and running the Java program. Oracle recommends choosing the JDBC driver that matches the JDK used by your application—for example, ojdbc11.jar for JDK 11 and ojdbc8.jar for JDK 8. (Oracle)

Step 1: Install and Start Oracle Database

Make sure Oracle Database is installed and running.

Example connection details:

Host    : localhost

Port    : 1521

Service : XEPDB1

Step 2: Download the Oracle JDBC Driver

Go to Oracle’s official JDBC driver download page:

Oracle JDBC Driver Downloads

Oracle provides different driver versions, including:

Java Version

JDBC Driver

JDK 8

ojdbc8.jar

JDK 11

ojdbc11.jar

JDK 17+

Check the latest compatible Oracle JDBC driver

Step 3: Create the Project Folder Structure

Create the following folders:

OracleJDBC/

├── ojdbc11.jar

├── StudentApp.java

oracle-ai

Step 4: Create the Database Table

Before running the Java program, create a table in Oracle:
CREATE TABLE student (
    id NUMBER PRIMARY KEY,
    name VARCHAR2(50)
);
Insert sample data:
INSERT INTO student VALUES (1, 'Raja');

INSERT INTO student VALUES (2, 'Ram');

COMMIT;
Code language: JavaScript (javascript)

Step 5: Write the JDBC Program

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class StudentApp {

    public static void main(String[] args) {

        try {

            // Load Oracle JDBC Driver
            Class.forName("oracle.jdbc.OracleDriver");

            // Create Connection
            Connection con = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521/XEPDB1",
                    "system",
                    "password"
            );

            // Create Statement
            Statement st = con.createStatement();

            // Execute Query
            ResultSet rs = st.executeQuery(
                    "SELECT * FROM student"
            );

            // Display Results
            while (rs.next()) {

                System.out.println(
                        rs.getInt("id") + " " +
                        rs.getString("name")
                );
            }

            // Close Resources
            rs.close();
            st.close();
            con.close();

        } catch (Exception e) {

            e.printStackTrace();

        }
    }
}

Step 6: Open Command Prompt or PowerShell

Navigate to the project folder.

For example:

cd D:\OracleJDBC

Step 7: Compile Using the Classpath

Compile:

javac -classpath “.;D:\OracleJDBC\ojdbc11.jar” StudentApp.java

Run

java -classpath “.;D:\OracleJDBC\ojdbc11.jar” StudentApp

Execution and Output:

PS D:\OracleJDBC> javac -classpath ".;D:\OracleJDBC\ojdbc11.jar" StudentApp.java
PS D:\OracleJDBC> java -classpath ".;D:\OracleJDBC\ojdbc11.jar" StudentApp
1 Raja
2 RamCode language: CSS (css)
Scroll to Top