JDBC is a Java API that enables Java applications to interact with relational databases. It provides a standard interface for connecting to databases, executing SQL queries, and retrieving results.
Stands for Java Database Connectivity
Part of java.sql package
Enables platform-independent database access
Provides methods for connecting, executing SQL statements, and processing results
Key Benefits:
Database-independent interface
Simplifies SQL execution from Java
Supports transaction management
JDBC architecture consists of two layers:
Contains classes and interfaces for connecting, executing queries, and handling results
Examples: Connection, Statement, PreparedStatement, ResultSet
Handles communication between Java application and database
Different types of JDBC drivers: Type 1, Type 2, Type 3, Type 4
| Type | Description | Example |
|---|---|---|
| Type 1 | JDBC-ODBC bridge driver | Rarely used now |
| Type 2 | Native API driver | Oracle OCI driver |
| Type 3 | Network protocol driver | Middleware driver |
| Type 4 | Thin driver (pure Java) | MySQL Connector/J, PostgreSQL driver |
Most commonly used: Type 4 (thin driver) – platform-independent and fast.
Load the JDBC Driver
Establish a Connection
Create a Statement
Execute SQL Query
Process ResultSet
Close Connection
| Statement Type | Use Case |
|---|---|
Statement | For static SQL queries |
PreparedStatement | Precompiled SQL queries; prevents SQL injection |
CallableStatement | Executes stored procedures |
Database independent → Works with any relational database
Supports SQL operations → Insert, update, delete, query
Integrates with Java → Easily used in applications, servlets, and JSP
Supports transaction management → Commit, rollback
Always close JDBC resources to prevent memory leaks
Use PreparedStatement for dynamic queries to improve performance and security
JDBC is core for Java database programming and forms the base for JPA/Hibernate
JDBC is a powerful API that enables Java programs to interact with relational databases efficiently. Mastery of JDBC is essential for building database-driven Java applications, web applications, and enterprise solutions.
Take quizzes related to this topic and see where you stand!
Start Quiz Now