HomeLinuxConnect with Oracle DB by way of JDBC Driver Utilizing Java

Connect with Oracle DB by way of JDBC Driver Utilizing Java


Oracle database is a well-liked relational database administration system (RDBMS) that’s broadly utilized in enterprise and large-scale environments. Within the means of creating any software, establishing a reference to the Oracle Database can show to be a difficult job for builders. Luckily, Java offers a wide range of instruments and drivers, together with the JDBC driver, that may be utilized to make a connection.

This put up will exhibit how to hook up with the Oracle database by way of JDBC driver utilizing Java.

Connect with Oracle DB by way of JDBC Driver Utilizing Java

Earlier than starting with this put up, ensure that Java is put in in your system. For this function, kind the under command within the Command Immediate:

Output

The output shows the at present put in model of java.

Step 1: Obtain JDBC Driver

Obtain the JDBC driver (jar file) in line with the model of your database. For that function, go to the Oracle database JDBC driver and companion jars downloads web page and choose the JDBC driver accordingly:

Step 2: Connect with Oracle Database Utilizing Java

Create a brand new folder named “dbconnection” and paste the downloaded JDBC driver (jar file) into the folder. Subsequent, create a brand new Java file named “DBConnection.java” within the chosen folder:

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

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

Create and initialize a connection variable:

Present the connection particulars, resembling “connection string”, “username” and “password” of your Oracle database within the “attempt” block. Set up the connection utilizing the “getConnection()” operate. Affirm the connection utilizing the “if” assertion as given under:

attempt {
String connString = “jdbc:oracle:skinny:@//localhost:1521/xe”;

String username = “c##md”;
String password = “md1234”;

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

if (conn != null) {
         System.out.println(“Linked to Oracle database efficiently!”);
     } else {
         System.out.println(“Failed to hook up with Oracle database.”);
       }
}

Kind the next code for the “catch” block to catch any exception (error):

The “lastly” block closes the connection if no error happens within the code:

lastly {
if (conn != null) {
attempt {
                    conn.shut();
                } catch (SQLException e) {
                    System.out.println(“SQL Exception: “ + e.getMessage());
                }
            }
        }

Assessment the entire code as given under. 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;

attempt {
            String connString = “jdbc:oracle:skinny:@//localhost:1521/xe”;

            String username = “c##md”;
            String password = “md1234”;

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

if (conn != null) {
                System.out.println(“Linked to Oracle database efficiently!”);
            } else {
                System.out.println(“Failed to hook up with Oracle database.”);
            }
        } catch (SQLException e) {
            System.out.println(“SQL Exception: “ + e.getMessage());
        } lastly {
if (conn != null) {
attempt {
                    conn.shut();
                } catch (SQLException e) {
                    System.out.println(“SQL Exception: “ + e.getMessage());
                }
            }
        }
    }
}

Open the Command Immediate on this folder location by typing “CMD” within the tackle bar:

Kind the command given under to run the Java file “DBConnection.java” by way of the JDBC driver:

java cp ojdbc11.jar DBConnection.java

Output

The output shows that the Oracle database is related by way of the JDBC driver utilizing Java.

Conclusion

Obtain the JDBC driver and paste the JDBC jar file into a brand new folder. Create a Java file contained in the newly created folder and open it with any code editor. Present the credentials in line with your Oracle database and save the file. Open the Command Immediate within the folder the place JDBC is situated and run the Java file by way of the JDBC driver. This put up demonstrated a sensible information on how to hook up with the Oracle database by way of JDBC driver utilizing Java.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments