0% found this document useful (0 votes)
3 views

Java Database Connectivity

Java Database Connectivity (JDBC) is an API that allows Java programs to connect with various databases using SQL for querying and updating data. The process involves five steps: loading the driver, creating a connection, creating a statement, executing a query, and closing the connection. There are four types of JDBC drivers: Type-1 (JDBC-ODBC Bridge), Type-2 (Native API), Type-3 (Network Protocol), and Type-4 (Thin Driver), each with different performance characteristics and requirements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Database Connectivity

Java Database Connectivity (JDBC) is an API that allows Java programs to connect with various databases using SQL for querying and updating data. The process involves five steps: loading the driver, creating a connection, creating a statement, executing a query, and closing the connection. There are four types of JDBC drivers: Type-1 (JDBC-ODBC Bridge), Type-2 (Native API), Type-3 (Network Protocol), and Type-4 (Thin Driver), each with different performance characteristics and requirements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Database Connectivity (JDBC) is an API (Application Program Interface) or platform-

independent interface which helps to connect java programs with various databases such as
Oracle, My SQL, MS Access and SQL Server. It provides ways to query and update the database
using Structured Query Language (SQL) update statements such as CREATE, DELETE,
INSERT and UPDATE and query statements like SELECT. It is almost similar to ODBC (Open
Database Connectivity) that was provided by Microsoft.

To connect the java program or application with the database there are five steps to be followed:

1. Load the Driver: Driver helps to make a connection to the database hence driver must be
loaded once in the program. Class.forName(): By using this, the driver’s class file is
loaded in the memory during run time. There is no need to create a new object. For
example:

Class.forName(“oracle.jdbc.driver.OracleDriver”);

2. Creating Connections: After the driver is loaded, the connection is set up. The connection
object uses username, password, and URL to set up the connection. URL has a
predefined format which contains database name, the driver used, IP address where the
database is stored, Port number and the service provider. The connection can be set up by
using the command:

Connection con = DriverManager.getConnection(URL, user, password);

3. Creating Statement: After establishing the connection, the user can interact with the
database. The interfaces such as JDBC statement, PreparedStatement, CallableStatement
provides methods that allow a user to send SQL statements and get data from the
database. Command used to create statement is-

Statement stmt = con.createStatement();

4. Executing Query: The SQL query is executed to interact with the database. A query can
be for updating/inserting in the database or for retrieving data. Statement interface
provides two methods i.e. executeQuery() method to execute queries for retrieving data
while executeUpdate() method to execute queries for updating or inserting. For Example:

stmt.executeUpdate(“DELETE TABLENAME”);

5. Closing Connection: After executing our query, the data user wanted to update or retrieve
has been done so now it’s time to close the established connection. The connection
interface provides a method close() to close the connection. For example:

con.close();
Components of JDBC Architecture
The components are explained below.

 Driver Manager: It is a class that contains a list of all drivers. When a connection

request is received, it matches the request with the appropriate database driver using a

protocol called communication sub-protocol. The driver that matches is used to establish

a connection.

 Driver: It is an interface which controls the communication with the database server.

DriverManager objects are used to perform communication.

 Connection: It is an interface which contains methods to contact a database.

 Statement: This interface creates an object to submit SQL queries or statements to the

database.

 ResultSet: This contains the results retrieved after the execution of the SQL statements

or queries.

 SQLException: Any errors that occur in database application are handled by this class.
Basic JDBC architectural diagram

Types of JDBC drivers


There are four types of JDBC drivers:

1. Type-1 Driver or JDBC-ODBC Bridge


This driver acts as a bridge between JDBC and ODBC. It converts JDBC calls into
ODBC calls and then sends the request to ODBC driver. It is easy to use but execution
time is slow.
2. Type-2 Driver or Native API Partly Java Driver
This driver uses JNI (Java Native Interface) call on database specific native client API. It
is comparatively faster than Type-1 driver but it requires native library and cost of
application also increases.
3. Type-3 Driver or Network Protocol Driver
These drivers communicate to JDBC middleware server using proprietary network
protocol. This middleware translates the network protocol to database specific calls. They
are database independent. They can switch from one database to another but are slow due
to many network calls.
4. Type-4 or Thin Driver
This driver is also called pure Java driver because they directly interact with the database.
It neither requires any native library nor middleware server. It has better performance
than other drivers but comparatively slow due to an increase in a number of network
calls.

You might also like