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:
o JDBC-ODBC Bridge Driver,
o Native Driver,
o Network Protocol Driver, and
o Thin Driver
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
A list of popular classes of JDBC API are given below:
o DriverManager class
o Blob class
o Clob class
o Types class
We can use JDBC API to handle database using Java program and can
perform the following activities:
1. Connect to the database
2. Execute queries and update statements to the database
3. Retrieve the result received from the database.
What is API
API (Application programming interface) is a document that contains a
description of all the features of a product or software. It represents
classes and interfaces that software programs can follow to communicate
with each other. An API can be created for applications, libraries,
operating systems, etc.
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)
1) JDBC-ODBC bridge driver
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC b
driver converts JDBC method calls into the ODBC function calls. This is now discouraged becau
thin driver.
Oracle does not support the JDBC-ODBC Bridge from Java 8. Oracle
recommends that you use JDBC drivers provided by the vendor of your
database instead of the JDBC-ODBC Bridge.
Advantages:
o easy to use.
o can be easily connected to any database.
Disadvantages:
o Performance degraded because JDBC method call is converted into the
ODBC function calls.
o The ODBC driver needs to be installed on the client machine.
2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver converts
method calls into native calls of the database API. It is not written entirely in java.
Advantage:
o performance upgraded than JDBC-ODBC bridge driver.
Disadvantage:
o The Native driver needs to be installed on the each client machine.
o The Vendor client library needs to be installed on client machine.
3) Network Protocol driver
The Network Protocol driver uses middleware (application server) that
converts JDBC calls directly or indirectly into the vendor-specific database
protocol. It is fully written in java.
Advantage:
o No client side library is required because of application server that can
perform many tasks like auditing, load balancing, logging etc.
Disadvantages:
o Network support is required on client machine.
o Requires database-specific coding to be done in the middle tier.
o Maintenance of Network Protocol driver becomes costly because it
requires database-specific coding to be done in the middle tier.
4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is w
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.
Java Database Connectivity with 5 Steps
There are 5 steps to connect any java application with the database using
JDBC. These steps are as follows:
o Register the Driver class
o Create connection
o Create statement
o Execute queries
o Close connection
1) Register the driver class
The forName() method of Class class is used to register the driver class. This method is us
dynamically load the driver class.
Syntax of forName() method
public static void forName(String className)throws ClassNotFoundException
Example to register the OracleDriver class
Here, Java program is loading oracle driver to esteblish database
connection.
Class.forName("oracle.jdbc.driver.OracleDriver");
2) Create the connection object
The getConnection () method of DriverManager class is used to establish connection with
database.
Syntax of getConnection () method
1) public static Connection getConnection (String url)
throws SQLException
2) public static Connection getConnection
(String url,String name,String password)
throws SQLException
3) Create the Statement object
The createStatement() method of Connection interface is used to create statement. The obje
statement is responsible to execute queries with the database.
Syntax of createStatement() method
public Statement createStatement()throws SQLException
4) Execute the query
The executeQuery() method of Statement interface is used to execute queries to the
database. This method returns the object of ResultSet that can be used to get all the records
of a table.
Syntax of executeQuery() method
public ResultSet executeQuery(String sql)throws SQLException
5) Close the connection object
By closing connection object statement and ResultSet will be closed automatically. The
close() method of Connection interface is used to close the connection.
Syntax of close() method
public void close()throws SQLException
Java Database Connectivity with MySQL
To connect Java application with the MySQL database, we need to follow 5
following steps.
Driver class: The driver class for the mysql database
is com.mysql.jdbc.Driver.
Connection URL : jdbc:mysql://localhost:3306/ITB
where jdbc is the API, mysql is the database, localhost is the server name on
which mysql is running, we may also use IP address, 3306 is the port number
and ITB is the database name. We may use any database, in such case, we need
to replace the ITB with our database name.
1. Username: The default username for the mysql database is root.
2. Password: It is the password given by the user at the time of
installing the mysql database.
Let's first create a table in the mysql database, but before creating table, we
need to create database first.
create database ITB;
use ITB;
create table employee(id int(10),name varchar(40),age int(3));
Example to Connect Java Application with mysql
database
import java.sql. *;
class MyJdbc{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ITB","root","root");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from employee");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}