Introduction to JDBC in Java: A Beginner’s Guide with Examples

8/16/2025

#Introduction JDBC in Java: A Beginner’s Guide with Examples

Go Back

Introduction to JDBC in Java: A Beginner’s Guide with Examples


Introduction

In modern software development, most applications interact with databases. Java provides a powerful API known as JDBC (Java Database Connectivity) to connect, query, and manipulate relational databases.

JDBC acts as a bridge between Java applications and databases, making it possible to write database-independent code.

In this guide, we’ll explore JDBC architecture, drivers, steps to connect, and a working example.


#Introduction  JDBC in Java: A Beginner’s Guide with Examples

What is JDBC in Java?

JDBC (Java Database Connectivity) is a standard Java API that allows Java programs to interact with relational databases such as MySQL, Oracle, PostgreSQL, SQL Server, etc.

  • JDBC is part of Java SE.

  • It provides a set of classes and interfaces in the package:

    java.sql
    
  • It follows a standard protocol so developers can switch databases without changing much code.


JDBC Architecture

The JDBC architecture consists of two main layers:

  1. JDBC API Layer – Interfaces and classes in Java (java.sql package).

  2. JDBC Driver Layer – Database-specific driver implementation that communicates with the database.


Types of JDBC Drivers

JDBC supports four types of drivers:

  1. JDBC-ODBC Bridge Driver (Type 1) – Legacy, rarely used.

  2. Native API Driver (Type 2) – Uses database-specific native libraries.

  3. Network Protocol Driver (Type 3) – Middleware driver, communicates via server.

  4. Thin Driver (Type 4) – Pure Java driver, most commonly used (e.g., MySQL Connector/J).

✅ Best choice today: Type 4 Driver.


Steps to Connect to a Database using JDBC

  1. Load the Driver Class

  2. Establish the Connection

  3. Create a Statement

  4. Execute Queries (SQL)

  5. Process Results

  6. Close the Connection


JDBC Example: Connecting to MySQL

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCDemo {
    public static void main(String[] args) {
        try {
            // Step 1: Load and register driver
            Class.forName("com.mysql.cj.jdbc.Driver");

            // Step 2: Establish connection
            Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/testdb", "root", "password");

            // Step 3: Create statement
            Statement stmt = con.createStatement();

            // Step 4: Execute query
            ResultSet rs = stmt.executeQuery("SELECT * FROM users");

            // Step 5: Process results
            while (rs.next()) {
                System.out.println(rs.getInt(1) + " " + rs.getString(2));
            }

            // Step 6: Close connection
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output: Prints records from the users table in testdb.


Advantages of JDBC

  • Platform-independent database connectivity.

  • Supports multiple relational databases.

  • Provides both statement and prepared statement execution.

  • Built-in transaction management.


Use Cases of JDBC

  • Building Java desktop applications that interact with databases.

  • Backend web applications connecting to MySQL/Oracle/PostgreSQL.

  • Data migration between databases.

  • Running SQL queries inside Java applications.


Best Practices

Always close connections after use.
Use PreparedStatement instead of Statement to prevent SQL Injection.
Handle exceptions properly.
Use connection pooling in large applications.


Conclusion

JDBC in Java is the foundation of database programming. It provides a reliable and efficient way to connect Java applications with relational databases. With JDBC, developers can build powerful, database-driven applications that are scalable, secure, and portable.

Table of content