JDBC connection with Oracle 11g - ojdbc6.jar - configure java build path - registering driver - getConnection - Writing your First jdbc program


In this tutorial we will learn JDBC connection with Oracle 11g using  ojdbc6.jar - How to configure java build path in eclipse - How to register driver in java- How to getConnection in java - Writing your First jdbc(Java database connectivity) program.


Download ojdbc6.jar (JDBC jar to connect to Oracle database from java)

Right click on Java project in eclipse, and click properties.
Select Java build path, click add external jar, give path of ojdbc6.jar, click Ok.


Registering the Driver class in java JDBC>

Here we will be registering Oracle driver class in java JDBC
Class.forName("oracle.jdbc.driver.OracleDriver");

OR
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());



Setting up connection with Oracle database in JDBC java>
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@Hostname:Port:SID",
                              "Username", "Password");

For following details - Refer Oracle SQL Developer installation - STEP 7
Hostname = localHost,
Port =1521 (default port)
SID= orcl
Username =ankit
Password=Oracle123

Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl",
                              "ankit", "Oracle123");


Example/ Full program to connect to Oracle database  using Type 4 driver - java JDBC program >

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyJdbcConnectionSetupExample {
   public static void main(String... arg) {
         
          Connection con =null;
          try {
                 // registering Oracle driver class
                 Class.forName("oracle.jdbc.driver.OracleDriver");
                 // getting connection
                 con = DriverManager.getConnection(
                              "jdbc:oracle:thin:@localhost:1521:orcl",
                              "ankit", "Oracle123");
                 // Test connection is null or not
                 if (con != null)
                       System.out.println("Connection established successfully!");
                 else
                       System.out.println("No Connection!");
          } catch (ClassNotFoundException e) {
                 e.printStackTrace();
          } catch (SQLException e) {
                 e.printStackTrace();
          }
          finally{
                 try {
                       if(con!=null) con.close(); // close connection
                 } catch (SQLException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/*OUTPUT
Connection established successfully!
*/

Above JDBC java program will connect you to Oracle database using Type 4 java JDBC driver .


So, in this tutorial we learned JDBC connection code with Oracle 11g using  ojdbc6.jar - configuring java build path in eclipse - registering driver in java- How to getConnection in java- Writing your First jdbc(Java database connectivity) program in java.

RELATED LINKS>

JDBC connection with PostgreSQL database


JDBC tutorial connection with Oracle 11g


No comments:

Post a Comment