Wednesday, 25 January 2017

How to connect oracle with JDBC Type1 driver

How to connect oracle with JDBC Type1 driver                  

Connect to oracle using Type1 driver

Hy guyss.... howz you all..

Today we here to explains you, “How to connect oracle with JDBC Type1 driver”, “Connect to oracle database using Type1 driver”. We all know that JDBC type1 driver is out-dated and also removed from jdk8 version.

But only for concept purpose we will learn type1 driver and also see their drawbacks that helps to learning other driver.

At first we are going to write a JDBC program that show connection from oracle database and according to that code i will explain all those things here.



Sample code....

Step1:  create DSN in your computer by following path
Control panel -> Administrative Tool -> odbc data source -> user DSN -> Add -> select “Microsoft odbc for oracle” or “oracle XE”enter data source name and username of oracle. -> finish

In my case data source name is “ordsn”.



File Name: Type1DriverTest.java
--------------------------------------------------Program Starts Here------------------------------------------------
import java.sql.*;
class Type1Test 
{
public static void main(String[] args)throws Exception 
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:ordsn","scott","tiger");
if(con==null){
System.out.println("not connected..");
}else{
System.out.println("Smartyy u r connectedd to oracle database...");
}
}
}
-----------------------------------------------------------explanation-------------------------------------------------
Class.forName(): 
Class is a class that contains forName() method. This method id used to load database driver. By loading of driver class automatically it is registered. It happens because every driver class contains registering information in static block. In this case when class is loaded the static block of driver class is automatically executed and register driver. 

Sun.jdbc.odbc.driver.OracleDriver: 
 It is driver class that is located in given package. OracleDriver is Driver class name.
Connection con=DriverManager.getConnection("jdbc:odbc:ordsn","scott","tiger");
In this line Connection is an interface of java.sql.Connection interface.
Con is the reference variable that that holds object of implemented Connection class object.
DriverManager is another class that has method getConnection() that takes three parameters url,username and password For establishing connection.
These are the explanation of above code...

One and other problem is there in Type1 driver is compatibility issues. So, if you want to run type1 driver in your system make sure that the computer system is having 32bit operating system with 32bit oracle software. You also have to ensure that you are running jdk7 or older version. Because from jdk8  type1 driver has been removed. I will also give you snapshot of creating DSN, that also make your task easy.

If you have any query then you can ask me or mail me your question at smartiesgeeks@gmail.com
So, guys enjoyyy coding with type 1 jdbc driver and wish you happy coding........
(Smartyy sunny) 

No comments:

Post a Comment

Make Top Rank Blog