Introduction to JDBC

📘 Java 👁 65 views 📅 Dec 01, 2025
⏱ Estimated reading time: 2 min

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.


1. What is JDBC?

  • 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


2. JDBC Architecture

JDBC architecture consists of two layers:

A. JDBC API (Application Level)

  • Contains classes and interfaces for connecting, executing queries, and handling results

  • Examples: Connection, Statement, PreparedStatement, ResultSet

B. JDBC Driver Manager (Driver Level)

  • Handles communication between Java application and database

  • Different types of JDBC drivers: Type 1, Type 2, Type 3, Type 4


3. JDBC Drivers

TypeDescriptionExample
Type 1JDBC-ODBC bridge driverRarely used now
Type 2Native API driverOracle OCI driver
Type 3Network protocol driverMiddleware driver
Type 4Thin driver (pure Java)MySQL Connector/J, PostgreSQL driver

Most commonly used: Type 4 (thin driver) – platform-independent and fast.


4. Steps to Use JDBC

  1. Load the JDBC Driver

Class.forName("com.mysql.cj.jdbc.Driver");
  1. Establish a Connection

Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mydb", "username", "password");
  1. Create a Statement

Statement stmt = con.createStatement();
  1. Execute SQL Query

ResultSet rs = stmt.executeQuery("SELECT * FROM students");
  1. Process ResultSet

while(rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("name")); }
  1. Close Connection

rs.close(); stmt.close(); con.close();

5. Types of Statements

Statement TypeUse Case
StatementFor static SQL queries
PreparedStatementPrecompiled SQL queries; prevents SQL injection
CallableStatementExecutes stored procedures

6. Advantages of JDBC

  • 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


7. Key Points

  • 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


8. Conclusion

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.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes