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

JDBC

JDBC (Java Database Connectivity) is a Java API that enables Java applications to connect and execute queries with various databases. It consists of four main components: JDBC API, JDBC Driver Manager, JDBC Test suite, and JDBC-ODBC Bridge Drivers, and supports both two-tier and three-tier architectures for database communication. The process of using JDBC involves registering the driver, creating a connection, creating a statement, executing queries, and closing the connection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

JDBC

JDBC (Java Database Connectivity) is a Java API that enables Java applications to connect and execute queries with various databases. It consists of four main components: JDBC API, JDBC Driver Manager, JDBC Test suite, and JDBC-ODBC Bridge Drivers, and supports both two-tier and three-tier architectures for database communication. The process of using JDBC involves registering the driver, creating a connection, creating a statement, executing queries, and closing the connection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

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).
Why Should We Use JDBC?
Before JDBC, ODBC (Open database connectivity) API was the
database API to connect and execute the query with the database. But,
ODBC API uses ODBC driver which is written in C language (i.e.
platform dependent and unsecured).
That is why Java has defined its own API (JDBC API) that uses
JDBC drivers (written in Java language).
What is the need of JDBC? (cont)

JDBC is a Java database API used for making connection


between java applications with various databases.

Basically, JDBC used for establishing stable database connection


with the application API.

To execute and process relational database queries (SQL or


Oracle queries), multiple application can connect to different types of
databases which supports both standard (SE) and enterprise (EE) edition
of java.
Components of JDBC:

There are generally four main components of JDBC through which


it can interact with a database.

1. JDBC API:
It provides various methods and interfaces for easy communication
with the database.

2. JDBC Driver Manager:


It loads a database-specific driver in an application to establish a
connection with a database. It is used to make a database-specific call to the
database to process the user request.
3. JDBC Test suite:
It is used to test the operation(such as insertion, deletion,
updation) being performed by JDBC Drivers.

4. JDBC-ODBC Bridge Drivers:


It connects database drivers to the database. This bridge translates
the JDBC method call to the ODBC function call. It makes use of
the sun.jdbc.odbc package which includes a native library to access
ODBC characteristics.
Architecture of JDBC:
Types of JDBC Architecture:

1. Two-tier model:
A java application communicates directly to the data source. The
JDBC driver enables the communication between the application and the data
source.
When a user sends a query to the data source, the answers for those
queries are sent back to the user in the form of results.
The data source can be located on a different machine on a network
to which a user is connected.

This is known as a client/server configuration, where the user’s


machine acts as a client, and the machine has the data source running acts as
the server.
2. Three-tier model:

In this, the user’s queries are sent to middle-tier services, from


which the commands are again sent to the data source.

The results are sent back to the middle tier, and from there to the
user.
This type of model is found very useful by management
information system directors.
Working of JDBC:

Java application that needs to communicate with the database has


to be programmed using JDBC API.

JDBC Driver supporting data sources such as Oracle and SQL


server has to be added in java application for JDBC support which can be
done dynamically at run time.

This JDBC driver intelligently communicates the respective data


source.
There are 5 steps to connect any java application with the database
using JDBC. These steps are as follows

1. Register the Driver class


2. Create connection
3. Create statement
4. Execute queries
5. Close connection
Register the driver class
The forName() method of Class class is used to register the driver
class. This method is used to dynamically load the driver class.

Create the connection object


The getConnection() method of DriverManager class is used to
establish connection with the database.

Create the Statement object


The createStatement() method of Connection interface is used to
create statement. The object of statement is responsible to execute queries
with the database.
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.

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.

You might also like