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.
Step 2: To Add new DSN, Click on Add
Step 3: Select Oracle in XE
driver from the list, Click on Finish
Step 4: Give a DSN Name
- Data Source Name: CollegeekDSN ; Give any name to Data Source that you want to give.
- Description: Write description or leave blank.
- TNS Service Name: XE ; for Oracle is XE, you can check it in
tnsnames.ora
file. - User ID: SYSTEM ; we will give it later.
tnsnames.ora
file.
Step 5: Oracle Driver Connection
- Service Name: XE ; from previous step it will appear.
- User Name: SYSTEM ;
- Password: Enter Password that you have given at Oracle Database installation time. In my case it is admin.
Step 6: Click Ok , you can see that new DSN name in list. Click OK
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).
- Step 1: Download JDK 7 and extract it anywhere.
- Step 2: Copy the path of jdk/bin directory
- Step 3: Open command prompt and write:SET PATH=copied_path
- Step 4: Compile JAVA program and Run
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());
}
}
}