Oracle Database

Connect to Oracle DB via JDBC Driver Using Java

Oracle database is a popular relational database management system (RDBMS) that is widely used in enterprise and large-scale environments. In the process of developing any application, establishing a connection with the Oracle Database can prove to be a challenging task for developers. Fortunately, Java provides a variety of tools and drivers, including the JDBC driver, that can be utilized to make a connection.

This post will demonstrate how to connect to the Oracle database via JDBC driver using Java.

Connect to Oracle DB via JDBC Driver Using Java

Before beginning with this post, make sure that Java is installed in your system. For this purpose, type the below command in the Command Prompt:

java --version

Output

The output displays the currently installed version of java.

Step 1: Download JDBC Driver

Download the JDBC driver (jar file) according to the version of your database. For that purpose, visit the Oracle database JDBC driver and companion jars downloads page and select the JDBC driver accordingly:

Step 2: Connect to Oracle Database Using Java

Create a new folder named “dbconnection” and paste the downloaded JDBC driver (jar file) into the folder. Next, create a new Java file named “DBConnection.java” in the selected folder:

Open the “DBConnection.java” file in any code editor and type this code to import all the necessary libraries for connecting Oracle database using Java:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

Create and initialize a connection variable:

Connection conn = null;

Provide the connection details, such as “connection string”, “username” and “password” of your Oracle database in the “try” block. Establish the connection using the “getConnection()” function. Confirm the connection using the “if” statement as given below:

try {
String connString = "jdbc:oracle:thin:@//localhost:1521/xe";

String username = "c##md";
String password = "md1234";

     conn = DriverManager.getConnection(connString, username, password);

if (conn != null) {
         System.out.println("Connected to Oracle database successfully!");
     } else {
         System.out.println("Failed to connect to Oracle database.");
       }
}

Type the following code for the “catch” block to catch any exception (error):

catch (SQLException e) {

  System.out.println("SQL Exception: " + e.getMessage());

}

The “finally” block closes the connection if no error occurs in the code:

finally {
if (conn != null) {
try {
                    conn.close();
                } catch (SQLException e) {
                    System.out.println("SQL Exception: " + e.getMessage());
                }
            }
        }

Review the complete code as given below. Save and exit this Java file:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

publicclassDBConnection{
publicstaticvoidmain(String[] args) {
        Connection conn = null;

try {
            String connString = "jdbc:oracle:thin:@//localhost:1521/xe";

            String username = "c##md";
            String password = "md1234";

            conn = DriverManager.getConnection(connString, username, password);

if (conn != null) {
                System.out.println("Connected to Oracle database successfully!");
            } else {
                System.out.println("Failed to connect to Oracle database.");
            }
        } catch (SQLException e) {
            System.out.println("SQL Exception: " + e.getMessage());
        } finally {
if (conn != null) {
try {
                    conn.close();
                } catch (SQLException e) {
                    System.out.println("SQL Exception: " + e.getMessage());
                }
            }
        }
    }
}

Open the Command Prompt in this folder location by typing “CMD” in the address bar:

Type the command given below to run the Java file “DBConnection.java” through the JDBC driver:

java -cp ojdbc11.jar DBConnection.java

Output

The output displays that the Oracle database is connected through the JDBC driver using Java.

Conclusion

Download the JDBC driver and paste the JDBC jar file into a new folder. Create a Java file inside the newly created folder and open it with any code editor. Provide the credentials according to your Oracle database and save the file. Open the Command Prompt in the folder where JDBC is located and run the Java file through the JDBC driver. This post demonstrated a practical guide on how to connect to the Oracle database via JDBC driver using Java.

About the author

Danish Ghafoor

I am a computer science graduate with a certification in cybersecurity. My passion for technology has led me to develop a deep understanding of the challenges and solutions in the field of information security. I am dedicated to staying up-to-date with the latest industry trends and technologies, and constantly seeking out new opportunities to expand my knowledge and skills.