JDBC Components
About
JDBC provides a standard set of Java interfaces and classes that enable communication with relational databases. These components abstract the underlying database operations, making database interactions in Java uniform, flexible, and vendor-independent.
The core JDBC components include:
DriverManager
Driver
Connection
Statement
PreparedStatement
CallableStatement
ResultSet
SQLException
DatabaseMetaData
ResultSetMetaData
1. DriverManager
Role: DriverManager
is a class in the java.sql
package responsible for managing a list of database drivers.
Responsibilities
Loads JDBC drivers at runtime.
Establishes connections to the database via
getConnection()
method.Acts as a factory for
Connection
objects.
Example
Internal Mechanism
It scans registered drivers and delegates the connection request to the appropriate
Driver
.
2. Driver
Role: Driver is an interface that every JDBC driver class must implement. It acts as a bridge between DriverManager and the actual database.
Responsibilities
Understand the connection URL and initiate database communication.
Register itself with
DriverManager
.
Example
Note: Most modern drivers auto-register using META-INF/services/java.sql.Driver
.
3. Connection
Role: Connection
is an interface representing a session/connection with the database.
Responsibilities
Creates
Statement
,PreparedStatement
,CallableStatement
.Manages transactions (commit, rollback).
Provides access to metadata.
Controls connection settings like auto-commit, isolation level.
Example
Important Methods
createStatement()
prepareStatement()
setAutoCommit(boolean)
commit()
rollback()
close()
4. Statement
Role: Statement
is an interface used to execute static SQL queries.
Responsibilities
Executes DDL and DML queries without input parameters.
Returns results via
ResultSet
.
Example
Limitations
Not suitable for dynamic queries.
Prone to SQL injection.
Poor performance on repeated queries due to re-parsing.
5. PreparedStatement
Role: PreparedStatement
extends Statement
and is used to execute parameterized queries.
Responsibilities
Precompiles the SQL query for better performance.
Prevents SQL injection.
Suitable for repeated queries.
Example
Advantages
Improved performance.
Safe input handling.
Supports batch processing.
6. CallableStatement
Role: Used to execute stored procedures in the database.
Responsibilities
Handles IN, OUT, and INOUT parameters.
Useful for business logic embedded in the database.
Example
7. ResultSet
Role: ResultSet
represents the tabular data returned by SQL queries.
Responsibilities
Navigates through the result set (next, previous, absolute, etc.).
Retrieves column data by index or name.
Example
Cursor Types
TYPE_FORWARD_ONLY
TYPE_SCROLL_INSENSITIVE
TYPE_SCROLL_SENSITIVE
Concurrency Modes:
CONCUR_READ_ONLY
CONCUR_UPDATABLE
8. SQLException
Role: Handles database access errors and other SQL-related issues.
Responsibilities
Provides details about errors (SQL state, error code, message).
Supports chained exceptions.
Example
9. DatabaseMetaData
Role: Provides metadata about the database such as tables, users, supported features, etc.
Responsibilities
Discover table names, schemas, column data types.
Check for support of SQL features or data types.
Example
10. ResultSetMetaData
Role: Provides metadata about the ResultSet, such as column count, column name, data type.
Responsibilities
Useful in dynamic query execution or generic data processing.
Example
Lifecycle of JDBC Components
Driver registration (
Driver
withDriverManager
)Create a connection (
Connection
)Create and execute query (
Statement
/PreparedStatement
)Process results (
ResultSet
)Close resources (important to prevent leaks)
Interactions
DriverManager
returns aConnection
.Connection
createsStatement
or its variants.Statement
executes SQL and returns aResultSet
.ResultSet
is used to read data row by row.Exceptions during these operations are captured by
SQLException
.
Last updated
Was this helpful?