Connecting to Oracle Database using JDBC Type-1 Driver


NOTE: In JAVA 8, the JDBC-ODBC Bridge has been removed. It is no longer supported.

To connect a JAVA application with Oracle database using JDBC-ODBC Bridge(type-1) Driver. You need to follow the following steps.

Create DSN Name

Step 1: Go to control panel > Administrative Tools and select ODBC Data Source.

Administrative tools

Step 2: To Add new DSN, Click on Add

ODBC Data Source 64-Bit

Step 3: Select Oracle in XE driver from the list, Click on Finish

Create new Data Source

Step 4: Give a DSN Name

Than click on Test Connection

Oracle ODBC Driver config

tnsnames.ora file.

tnsnames.ora file

Step 5: Oracle Driver Connection

Than click on OK

Oracle Driver Connection

Step 6: Click Ok , you can see that new DSN name in list. Click OK

Successful Creation of CollegeekDSN

Setting Up JDBC-ODBC bridge

Method 1: Enable JDBC-ODBC bridge for JDK

Method 2: We will download JDK 7 and set Temporary Java Path (This will not affect your current Java environment because we are not installing it).

Temporary Java Path

Example:

import java.sql.*;
public class Collegeek_JDBC1_example {
  public static void main(String[] args) {
      try{
      	   // Load and register the driver
      	   try {
      			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      	   }
      	   catch(Exception e) {
      	       e.printStackTrace();
      	   }
      		//Driver myDriver = new sun.jdbc.odbc.JdbcOdbcDriver();
      		//DriverManager.registerDriver(myDriver);

      		// Establish the connection to the database server
      		// urlstring,enter workspace username or SYSTEM,password
      		Connection cn = DriverManager.getConnection("jdbc:odbc:CollegeekDSN","admin","admin");

      		// Create a statement
      		Statement st = cn.createStatement();

      		// Execute the statement
      		ResultSet rs = st.executeQuery("select * from emp1000");
      	  // Retrieve the results
      		while(rs.next())
      			System.out.println(rs.getString(1)+" "+rs.getString(2));

      		// Close the statement and connection
      		st.close();
      		cn.close();
        }
        catch(SQLException e) {
      		System.out.println(e.getMessage());
        }

    }
}