JDBC Type -4 driver example

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

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

Loading a database driver - Type4

Firstly we load database specific driver by calling Class.forName("Name of the driver class").

Syntax - Class.forName("com.mysql.jdbc.Driver")
The Class class is avaliable in java.lang package. The database driver is different for each DBMS.
The com.mysql.jdbc.Driver class is JDBC driver for MySQL database.
The oracle.jdbc.driver.OracleDriver class is JDBC driver for Oracle database.
The type4 driver is different for different databases. Once JDBC driver is loaded using above syntax, The DriverManager class is used to 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:mysql://localhost/test?user=root&password=root
  • Database : test
  • Database Server : localhost
  • Userid : user Id of database
  • Password : Password of database

Step1 : Download MySQL Connector/J and add in classpath. Or if you are using any IDE (Integrated Development Environment).
Step2 : 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;

Step3 :Create a java file to connect with MySQL database server and execute Query.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCType4Demo1 
public Connection getConnection1() 
  Connection con = null;
  con = DriverManager.getConnection
  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()) 
  catch (Exception e
  System.out.println("Error in fetching data" + e);
public static void main(String[] args
  new JDBCType4Demo1().getCustomer();
SQL statement 
create table customer 

custId int primary key, 
custName varchar(30), 
custAddress varchar(50), 
custContact varchar(20) 
insert into customer values(1,'Ram Kumar','Delhi','99887766'); 
select * from customer;

Previous Next

Share your valuable comment

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

Comment by : Adele November 17th, 2013
Comment : good one dude........

Comment by : Adele November 17th, 2013
Comment : good one

Comment by : hazarath February 20th, 2013
Comment : very good boss keep it up.