A STUDY ON:-Interacting With Database Micro Project by Devansh Deshmukh
A STUDY ON:-Interacting With Database Micro Project by Devansh Deshmukh
By Devansh Deshmukh
Third year of Diploma program in Engineering and Technology of Maharashtra State board of Technical
Education, Mumbai.
AT
SHIVAJIRAO S. JONDHLE POLYTECHNIC ASANGAON
-1-
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
This is Certify that Mr. Devansh R Deshmukh, Roll no. 48 in fifth semester of Computer Engineering Diploma
program in Engineering and Technology At 0935- SHIVAJIRAO S. JONDHLE POLYTECHNIC has completed the
Micro Project Satisfactorily in subject Advance Java Programming (22517) in the academic year 2024 - 2025 as
per the MSBTE Prescribed curriculum of ‘I’ Scheme.
Seal of Institute
-2-
INDEX
2 JDBC vs ODBC 7
3 Connecting to a Database 8
4 JDBC Architecture 16
8 Conclusion 22
9 References 22
-3-
Introduction to JDBC and ODBC
JDBC (Java Database Connectivity) and ODBC (Open Database Connectivity) are both APIs
(Application Programming Interfaces) that facilitate communication between Java
applications and databases. They provide a standard way for developers to interact with
various database systems, eliminating the need for specific database-dependent code.
JDBC is Java-specific, designed to work seamlessly with the Java programming language.
ODBC, on the other hand, is platform-independent, allowing various programming languages
to access databases. Both JDBC and ODBC utilize drivers to translate requests from the
application to the database, enabling data access and manipulation.
Key Features:
o Allows access to a variety of database systems (Oracle, SQL Server, MySQL, etc.)
using a common set of functions.
o Uses drivers that act as a bridge between the application and the database.
o Supports different types of databases through the installation of respective
ODBC drivers.
-4-
How it Works:
1. The application uses ODBC functions to send SQL queries.
2. The ODBC driver translates these SQL queries into a form that the database
understands.
3. The results are returned to the application via the ODBC interface.
Advantages:
o Database-agnostic: Works with many databases via the installation of the
appropriate drivers.
o Platform-independent in terms of database communication.
Disadvantages:
o ODBC is mostly used in Windows environments and can be difficult to configure.
o Performance can be affected depending on the driver used.
JDBC is an API specifically designed for Java applications to interact with databases. It enables
Java programs to connect to relational databases, send SQL queries, and process the results.
-5-
Key Features:
o JDBC allows Java applications to access a variety of databases using SQL queries.
o JDBC provides both low-level and high-level interfaces to perform database
operations.
o Unlike ODBC, JDBC is native to Java, meaning no additional platform-specific
libraries are needed.
How it Works:
1. The Java program loads the database driver (which is specific to the type of database).
2. It establishes a connection to the database using the DriverManager.getConnection()
method.
3. SQL queries are executed using the Statement object.
4. The results are processed by iterating over the ResultSet object.
5. Once done, the connection is closed.
Advantages:
-6-
JDBC vs ODBC
Java-based Platform-Specific
Driver
As evident from the table, JDBC and ODBC differ in their scope, drivers, architecture,
performance, and intended use cases. JDBC is specifically designed for Java applications,
while ODBC offers a broader reach to various languages. Choosing between JDBC and ODBC
depends on the specific programming language and the desired level of performance. JDBC is
usually favoured for Java applications due to its performance and ease of use within the Java
environment.
-7-
Connecting to Database
To interact with a database from a program, you need to establish a connection between
your application and the database system. This connection allows you to send SQL queries,
retrieve results, and manipulate the data stored in the database.
The process of connecting to a database typically involves several key steps, including loading
the database driver, establishing the connection, and managing the connection effectively
during use.
-8-
1. Load the Database Driver (JDBC)
Before connecting to a database, you must load the database driver, which is responsible for
translating Java calls into database-specific calls.
In JDBC, a database driver is usually provided by the database vendor (e.g., Oracle, MySQL,
PostgreSQL). JDBC drivers can be divided into four types based on their architecture:
Type 1: JDBC-ODBC bridge driver (outdated, not recommended).
Type 2: Native-API driver (uses database-specific client libraries).
Type 3: Network Protocol driver (uses a middleware server for communication).
Type 4: Thin driver (pure Java driver, most commonly used today).
To load a driver, you typically use Class.forName() in Java to dynamically load the driver class.
Example (MySQL Driver):
java
Copy code
try {
// Load MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
-9-
2. Establish a Connection to the Database
Once the driver is loaded, you can establish a connection to the database using
DriverManager.getConnection() or a DataSource object (for more advanced connection
pooling). The getConnection() method takes a URL, a username, and a password for the
database.
Example of JDBC Connection:
For a MySQL database, the URL format is:
php
Copy code
jdbc:mysql://<hostname>:<port>/<database_name>
Example code to establish a connection:
java
Copy code
import java.sql.*;
-11-
3. Create and Execute SQL Statements
Once you have a connection, you can create SQL statements (queries, inserts, updates,
deletes, etc.) using the Statement, PreparedStatement, or CallableStatement objects in JDBC.
Statement: For executing simple SQL queries.
PreparedStatement: For executing SQL queries with parameters (precompiled for
efficiency).
CallableStatement: For executing stored procedures.
Example of Executing a Simple Query:
java
Copy code
Statement stmt = null;
ResultSet rs = null;
try {
// Create a Statement object
stmt = conn.createStatement();
-13-
5. Close the Connection
Always close the database resources after use to avoid memory leaks or connection pool
exhaustion. This includes:
ResultSet
Statement
Connection
A good practice is to use a finally block to ensure resources are always closed, even if an
exception occurs.
Example:
java
Copy code
try {
// Use the resources (e.g., Statement, ResultSet)
} catch (SQLException e) {
e.printStackTrace();
} finally {
// Close resources
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
-14-
Connection Pooling (Advanced)
In a production environment, managing database connections can become inefficient if each
request opens a new connection. Connection pooling helps manage connections efficiently
by reusing connections from a pool rather than opening new ones for every database
interaction.
Why use connection pooling?
o Reduces the overhead of opening and closing connections.
o Improves performance and scalability of applications.
Popular libraries for connection pooling include Apache Commons DBCP, HikariCP, and C3P0.
-15-
JDBC Architecture
JDBC architecture comprises several key components that work together to enable database
interaction:
JDBC API: Provides a set of interfaces and classes that define the standard for
interacting with databases. This API serves as the foundation for communicating with
various database systems.
JDBC Driver Manager: Acts as a central manager responsible for loading and managing
JDBC drivers. It handles the task of finding and connecting to the appropriate driver for
the specific database.
JDBC Driver: A software component that translates JDBC API calls into a database-
specific format. It acts as a bridge between the Java application and the database,
making the communication seamless. There are four types of JDBC drivers: Type 1
(JDBC-ODBC bridge), Type 2 (Native API partly Java), Type 3 (Network Protocol), and
Type 4 (Native Protocol Pure Java).
Database: The actual database management system (DBMS) where the data is stored.
It can be any type of database like MySQL, Oracle, PostgreSQL, etc.
This architectural model ensures flexibility and portability for Java applications interacting
with databases. The separation of concerns between the JDBC API, driver manager, and
drivers allows for a standard, well-defined interaction process.
A two-tier architecture refers to a system where the application is divided into two distinct
layers: the client tier and the database tier. In a two-tier design, the client directly interacts
with the database without the involvement of any intermediary layers (such as a middle-tier
application server).
-16-
Three-Tier Database Driver
-17-
Types of JDBC Drivers
JDBC drivers come in various types, each with its own advantages and disadvantages.
Understanding the types of drivers available is crucial for selecting the best option for your
specific project. These types include:
JDBC-ODBC Bridge Driver (Type 1): Uses ODBC to communicate with databases. It
requires ODBC drivers to be installed on the system. This type is generally less efficient
and less preferred.
Native API Partly Java Driver (Type 2): Combines Java code with database-specific
native libraries. This approach offers better performance but introduces platform-
specific dependencies. It might require native library installation on the system.
-18-
Network Protocol Driver (Type 3): Communicates with a middle-tier server that then
interacts with the database. This type is typically used in multi-tier architectures and
offers flexibility for distributed environments.
Native Protocol Pure Java Driver (Type 4): The most efficient and preferred type. It
communicates directly with the database using a pure Java implementation. It offers
the best performance and eliminates the need for platform-specific libraries.
Choosing the right driver type depends on factors such as performance requirements,
platform compatibility, and security considerations. Type 4 drivers are usually the most
efficient and preferred choice, offering a pure Java implementation and direct
communication with the database.
-19-
Advantages & Disadvantages of Types of JDBC Drivers
-20-
3. Type 3: Network Protocol Driver
Advantages:
o Database Independence: The Type 3 driver is database-agnostic, as it
communicates with a middleware layer that can connect to different types of
databases.
o Platform Independence: Since the driver communicates through a network
protocol and does not depend on the native database libraries, it can work
across different platforms (cross-platform compatibility).
Disadvantages:
o Performance Overhead: Since the driver communicates through a middleware
layer, the added hop can introduce performance overhead, particularly with
high latency or large-scale operations.
o Complexity: Requires an additional middleware layer, which can complicate the
system architecture, requiring extra configuration, monitoring, and
maintenance.
JDBC plays a crucial role in bridging the gap between Java applications and databases,
enabling seamless data access and manipulation. Understanding the fundamentals of JDBC,
ODBC, driver types, and connection processes empowers developers to build robust and
data-driven applications. To further your knowledge, you can explore online resources,
books, and tutorials specifically designed for JDBC.
References
-22-