How to connect Oracle Database in Java

Connect Oracle Database

If you want to access oracle 10g database via JDBC connection, you need to set CLASS PATH environment variable to find the ojdbc14.jar file provided by Oracle.

The file is located in  “C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar ”

This is sample JDBC Connection string to connect Oracle database in Java

Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","user_name","Pass_word”");
conn.close();      

Following example will help you to connect with oracle database.


package db_conn;

// Now you have to import below mentioned packages in your java file:

importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;

// Create a public class name like DBConnection to use it from outside.


publicclass DBConnection {
       public Statement stat;
       public ResultSet result;
       Connection conn = null;

    public Connection getDBConnection() throws Exception{
      System.out.println("--------Testing of Oracle JDBC Connection ------");
            try {
                  Class.forName("oracle.jdbc.driver.OracleDriver");
            } catch(ClassNotFoundException e) {
                  System.out.println("Oracle JDBC Driver is not registered!");
                  e.printStackTrace();
            }

            System.out.println("Oracle JDBC Driver is registered!");
            try {          
              String sURL="jdbc:oracle:thin:@localhost:1521:XE";
              String sUserName="User_Name";
              String sPwd="Pass_Word";
              System.out.println("Connecting to the database...");

 // After loading the driver, you can make a connection using the DriverManager.getConnection method.

     conn = DriverManager.getConnection(sURL,sUserName,sPwd);
            } catch (SQLException e) {
                  System.out.println("Connection Failed!");
                  e.printStackTrace();
            }

            if (conn != null) {
                  System.out.println("You are connected to database!");
            } else {
                  System.out.println("Failed to connect with oracle database!");
            }
            returnconn;
    }

 // Following function (closeConnection ) is used to close the connection.  

    publicvoidcloseConnection(){
        try{
         conn.close();
        }

        catch(Exception ex){ }
       }

  // updatedeleteRecord function is used to update/delete records from database.  

    publicbooleanupdatedeleteRecord(String strSQL){

        try{
         stat=conn.createStatement();
         int status=stat.executeUpdate(strSQL);

         if(status==1){
          returntrue;
         }

         else{
          returnfalse;
         }
        }

        catch(Exception ex){

         returnfalse;
        }
       }

// SelectRecord function is used to query records from database. 

     public ResultSet SelectRecord(String strSQL){

        try{
         stat=conn.createStatement();
         result=stat.executeQuery(strSQL);
         returnresult;
        }

        catch(Exception ex){
         returnnull;
        }
       }
}

You can use following MySQL & Oracle JDBC driver names and database URL.

RDBMS


JDBC driver name

URL format

MySQL com.mysql.jdbc.Driver jdbc:mysql://hostname/DatabaseName
ORACLE oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@hostname:PortNumber:DatabaseName

I hope by this way you will be able to connect Oracle Database in Java.

How to send an E-Mail from 10g Oracle Database

If you have any query about connectivity, feel free to comment below.

SiteGround Black Friday Sale Below Banner

Get Free Blogging Tips & Technology updates in your Email !!!

Subscribe via RSS feed

The following two tabs change content below.
Anil Kumar
IT Blogger Tips focus on Blogging Tips, SEO Tips, Social Media, SQL Tips, PL/SQL Tips, Oracle DBA, Linux/Unix, Latest Technology, How Tos and Technical Solutions. You can find us on Facebook | Twitter |
Find on Google+

Siteground SALE
70% OFF SiteGround WordPress Hosting – $2.95/month (Exclusive Offer !!!)