Practical - 01 Introduction To JDBC
Practical - 01 Introduction To JDBC
Practical - 01 Introduction To JDBC
Practical – 01
Introduction to JDBC.
What is JDBC?
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the
query with the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC
drivers to connect with the database. There are four types of JDBC drivers:
We can use JDBC API to access tabular data stored in any relational database. By the help of
JDBC API, we can save, update, delete and fetch data from the database. It is like Open Database
Connectivity (ODBC) provided by Microsoft.
Fig. -01
The current version of JDBC is 01. It is the stable release since 21st September, 2017. It is based
on the X/Open SQL Call Level Interface. The java.sql package contains classes and interfaces for
JDBC API. A list of popular interfaces of JDBC API are given below:
o Driver interface
o Connection interface
o Statement interface
o PreparedStatement interface
o CallableStatement interface
o ResultSet interface
o ResultSetMetaData interface
o DatabaseMetaData interface
o RowSet interface
IT,SVIIT,Indore Page 1
Component Technology(BTIT-505) Suyash Jain (1601DMBIT01011)
JDBC Driver-
JDBC Driver is a software component that enables java application to interact with the database.
There are 4 types of JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
IT,SVIIT,Indore Page 2
Component Technology(BTIT-505) Suyash Jain (1601DMBIT01011)
2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver converts JDBC
method calls into native calls of the database API. It is not written entirely in java.
Advantage:
Disadvantage:
IT,SVIIT,Indore Page 3
Component Technology(BTIT-505) Suyash Jain (1601DMBIT01011)
4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is
why it is known as thin driver. It is fully written in Java language.
Advantage:
o Better performance than all other drivers.
o No software is required at client side or server side.
Disadvantage:
o Drivers depend on the Database.
IT,SVIIT,Indore Page 4
Component Technology(BTIT-505) Suyash Jain (1601DMBIT01011)
IT,SVIIT,Indore Page 5
Component Technology(BTIT-505) Suyash Jain (1601DMBIT01011)
IT,SVIIT,Indore Page 6
Component Technology(BTIT-505) Suyash Jain (1601DMBIT01011)
Sample Code:
import java.sql.*;
class Conndemo
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/conn","root","root");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next())
{
System.out.println(rs.getInt("sid")+" "+rs.getString(2)+" "+rs.getString(3));
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
IT,SVIIT,Indore Page 7
Component Technology(BTIT-505) Suyash Jain (1601DMBIT01011)
Output:-
IT,SVIIT,Indore Page 8