JDBC Architecture
JDBC Architecture
JDBC API helps Java applications interact with different databases like MSSQL,
ORACLE, MYSQL, etc. It consists of classes and interfaces of JDBC that allow the
applications to access databases and send requests made by users to the specified
database.
Purpose of JDBC
Java programming language is used to develop enterprise applications. These
applications are developed with intention of solving real-life problems and need to
interact with databases to store required data and perform operations on it. Hence, to
interact with databases there is a need for efficient database connectivity, ODBC(Open
Database Connectivity) driver is used for the same.
ODBC is an API introduced by Microsoft and used to interact with databases. It can be
used by only the windows platform and can be used for any language like C, C++, Java,
etc. ODBC is procedural.
JDBC is an API with classes and interfaces used to interact with databases. It is used only
for Java languages and can be used for any platform. JDBC is highly recommended for
Java applications as there are no performance or platform dependency problems.
Components of JDBC
JDBC has four main components that are used to connect with a database as follows:
1. JDBC API
2. JDBC Driver Manager
3. JDBC Test Suite
4. JDBC-ODBC Bridge Drivers
JDBC API
JDBC API is a collection of various classes, methods, and interfaces for easy
communication with a database. JDBC API provides two different packages to connect
with different databases.
java.sql.*;
javax.sql.*;
JDBC Driver Manager
JDBC DriverManager is a class in JDBC API that loads a database-specific driver in Java
application and establishes a connection with a database. DriverManager makes a call to a
specific database to process the user request.
JDBC Test Suite is used to test operations being performed on databases using JDBC
drivers. JDBC Test Suite tests operations such as insertion, updating, and deletion.
TARUN LUTHRA
Java Course Online for Beginners
42k+ enrolled
RAHUL JANGHU
Free Python Certification Course: Master the essentials
25k+ enrolled
PRATEEK NARANG
Free C++ Course: Learn the Essentials
23k+ enrolled
35,262+ learners have attended these Courses.
As the name suggests JDBC-ODBC Bridge Drivers are used to translate JDBC methods
to ODBC function calls. JDBC-ODBC Bridge Drivers are used to connect database drivers
to the database. Even after using JDBC for Java Enterprise Applications, we need an ODBC
connection for connecting with databases.
JDBC-ODBC Bridge Drivers are used to bridge the same gap between JDBC and ODBC
drivers. The bridge translates the object-oriented JDBC method call to the procedural
ODBC function call. It makes use of the sun.jdbc.odbc package. This package includes a
native library to access ODBC characteristics.
Architecture of JDBC
Let's take a look at the JDBC architecture in java.
As we can see in the above image the major components of JDBC architecture are as
follows:
1. Application
2. The JDBC API
3. DriverManager
4. JDBC Drivers
5. Data Sources
Application
Applications in JDBC architecture are java applications like applets or servlet that
communicates with databases.
JDBC API
The JDBC API is an Application Programming Interface used to create Databases. JDBC
API uses classes and interfaces to connect with databases. Some of the important classes
and interfaces defined in JDBC architecture in java are the DriverManager class,
Connection Interface, etc.
DriverManager
DriverManager class in the JDBC architecture is used to establish a connection between
Java applications and databases. Using the getConnection method of this class a
connection is established between the Java application and data sources.
JDBC Drivers
JDBC drivers are used to connecting with data sources. All databases like Oracle, MSSQL,
MYSQL, etc. have their drivers, to connect with these databases we need to load their
specific drivers. Class is a java class used to load drivers. Class.forName() method is used
to load drivers in JDBC architecture.
Data Sources
Data Sources in the JDBC architecture are the databases that we can connect using this
API. These are the sources where data is stored and used by Java applications. JDBC API
helps to connect various databases like Oracle, MYSQL, MSSQL, PostgreSQL, etc.
1. 2-tier model
2. 3-tier model
2 Tier Model
• 2-tier JDBC architecture model is a basic model.
• In this model, a java application communicates directly to the data sources. JDBC
driver is used to establish a connection between the application and the data
source.
• When an application needs to interact with a database, a query is directly executed
on the data source and the output of the queries is sent back to the user in form of
results.
• In this model, the data source can be located on a different machine connected to
the same network the user is connected to.
• This model is also known as a client/server configuration. Here user's machine
acts as a client and the machine on which the database is located acts as a server.
3 Tier Model
• 3-tier model is a complex and more secure model of JDBC architecture in java.
• In this model the user queries are sent to the middle tier and then they are
executed on the data source.
• Here, the java application is considered as one tier connected to the data
source(3rd tier) using middle-tier services.
• In this model user queries are sent to the data source using middle-tier services,
from where the commands are again sent to databases for execution.
• The results obtained on the database are again sent to the middle-tier and then to
the user/application.
Interfaces of JDBC API
JDBC API uses various interfaces to establish connections between applications and data
sources. Some of the popular interfaces in JDBC API are as follows:
Steps to connect a Java program using JDBC API. 1. Load Driver: Load JDBC Driver for
specific databases using forName() method of class Class.
Syntax: Class.forName("com.mysql.jdbc.Driver")
3. Create Query: To manipulate the database we need to create a query using commands
like INSERT, UPDATE, DELETE, etc. These queries are created and stored in string format.
Syntax: String sql_query = "INSERT INTO Student(name, roll_no) values('ABC','XYZ')"
4. Create Statement: The query we have created is in form of a string. To perform the
operations in the string on a database we need to fire that query on the database. To
achieve this we need to convert a string object into SQL statements. This can be done
using createStatement() and prepareStatement() interfaces. Syntax: Statement St =
con.createStatement();
5. Execute Statement: To execute SQL statements on the database we can use two
methods depending on which type of query we are executing.
• Execute Update: Execute update method is used to execute queries like insert,
update, delete, etc. Return type of executeUpdate() method is int. Syntax: int
check = st.executeUpdate(sql);
• Execute Query: Execute query method is used to execute queries used to display
data from the database, such as select. Return type of executeQuery() method is
result set. Syntax: Resultset = st.executeUpdate(sql);
import java.sql.*;
// Obtain a connection
con = DriverManager.getConnection("","","");
System.out.println("Database Connected Successfully!");
//Create Query
String sql = "INSERT INTO Student(sname, scity)
values('ABC','Pune')";
// Obtain a statement
st = con.createStatement();
if (check > 0)
{
System.out.println("Insert Done");
}
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
//Close Connection
st.close();
con.close();
}
}
}
Conclusion
• JDBC stands for Java Database Connectivity and is a Java API(Application
Programming Interface) used to interact with databases.
• JDBC architecture is divided into 4 main components: Application, JDBC API,
DriverManager, and JDBC Drivers.
• The interfaces and classes in JDBC API are used to establish a connection and
interact with databases.
• JDBC architecture in Java is of two types: 2-tier model and 3-tier model.