JDBC Type -1 driver example

I am using MySQL database server. The following are login credentials :

  • Database Server - localhost
  • User - root
  • Password - root
  • Database - test

Step1 : Install MySQL database Server [ Download MySQL Server ]
Step2 : Install ODBC driver for MySQL [ Download ODBC driver for MySQL ]
The JDBC type1 driver of MySQL server is not install by default, when you install MySQL server. Before using MySQL type1 driver you will have to download it from the above URL and install it. Here I will be guide you step by step

After download driver, run the setup. See picture below -
InstallType1Driver1

Click Next and see picture below -
InstallType1Driver2

Click next and see picture below -
InstallType1Driver3

Click Install and see picture below -
InstallType1Driver4

Click Finish.

Step3 : Create ODBC (DSN) datasource with the following steps -
Start -> Run -> odbcad32 -> Ok. See picture below -
CreateODBC1

Click Add and see picture below -
CreateODBC1

Select MySQL ODBC 5.1 Driver and click Finish and see picture below -
CreateODBC1

Provide the following informations -

  • Data Source Name - datasource1
  • TCP/IP Server - localhost (IP Address of MySQL Server)
  • User - root (User name of MySQL server )
  • Password - root (Password of MySQL Server )
  • Database - test ( Select database name, test is default database )

Click Test to check database connection. If it shows above dialog box Connection successful, means connection has established successfully to database server.
Click Ok.

Loading a database driver - Type1

Firstly we load database specific driver by calling Class.forName("Name of the driver class file").
Syntax - Class.forName("sun.jdbc.odbc.jdbcOdbcDriver");
The Class class is avaliable in java.lang package. The database driver is different for each DBMS. The type1 driver is same for all the databases. Once JDBC driver is loaded, The DriverManager is used establish connection with database server using getConnection() static method. The Connection type is the return type of getConnection(). The DriverManager and Connection are available in java.sql package.

Syntax - Connection con=DriverManager.getConnection("URL","userid","password");

  • URL - jdbc:odbc:<datasourcename>
  • Userid : user Id of database (Optional)
  • Password : Password of database (Optional)

Step4 : Create following table in MySQL database server -

use test;
drop table customer;
create table customer
(
custId varchar(10) primary key,
custName varchar(30),
custAddress varchar(50),
custContact varchar(20)
);
insert into customer values("a001",'Ram Kumar','Delhi','99887766');
select * from customer;
Download

Step5 :Create Java source code to connect with database -

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCType1Demo1 
{
public Connection getConnection1() 
{
  Connection con = null;
  try 
  {
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  con = DriverManager.getConnection("jdbc:odbc:datasource1");
  
  catch (Exception e
  {
  System.out.println("Error in connection" + e);
  }
  return con;
}
public void getCustomer() 
{
  try 
  {
  Connection con = getConnection1();
  Statement st = con.createStatement();
  ResultSet res = st.executeQuery("select * from customer");
  while (res.next()) 
  {
    System.out.print(res.getString(1));
    System.out.print(res.getString(2));
    System.out.print(res.getString(3));
    System.out.println(res.getString(4));
  }
  res.close();
  con.close();
  
  catch (Exception e
  {
  System.out.println("Error in fetching data" + e);
  }
}
public static void main(String[] args
{
  new JDBCType1Demo1().getCustomer();
}
}
Download

Previous Next

Comment by : AKSHAY MISAL November 22nd, 2014
Comment : The type 1 driver is not considered a deployment-level driver and is typically used for development and testing purposes only.

Comment by : raju August 12th, 2013
Comment : this is very best example

Comment by : vinod July 26th, 2013
Comment : Industry people are not recamended to use type1 Driver no need to worry where we r using this driver

Comment by : vinod July 26th, 2013
Comment : you are throwing SQLException only while using class.forName it will throw classNotFoundException just Write the exception insted of SQLException it will clear error msg

Comment by : monica June 14th, 2013
Comment : am getting classnotfound exception :sun.jdbc.odbc.JdbcOdbcDriver what shall i do..??

Comment by : rahul March 8th, 2013
Comment : while running the program am getting classNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver where we need to set the Classpath..?

Comment by : shruthi keerthi March 2nd, 2013
Comment : nice example...but where exactly the type1 driver is used?

Comment by : netra prasanna kumar November 1st, 2012
Comment : need some examples


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code