DEV Community

Sudhakar V
Sudhakar V

Posted on

JDBC Connectivity in Java (Java Database Connectivity)

🔌 JDBC Connectivity in Java (Java Database Connectivity)

JDBC is an API in Java that allows you to connect and interact with a relational database (like PostgreSQL, MySQL, Oracle, etc.).


📌 What is JDBC?

JDBC (Java Database Connectivity) is a standard Java API that enables Java applications to execute SQL statements, retrieve results, and manage database connections.

It is part of the Java Standard Edition (J2SE).


🔧 JDBC Architecture

Application
   ↓
JDBC API (java.sql)
   ↓
JDBC Driver Manager
   ↓
JDBC Driver (provided by DB vendor)
   ↓
Database (e.g., PostgreSQL, MySQL)
Enter fullscreen mode Exit fullscreen mode

✅ Steps to Connect Java with Database using JDBC

1. Import the package

import java.sql.*;
Enter fullscreen mode Exit fullscreen mode

2. Load and Register the Driver

Class.forName("org.postgresql.Driver"); // For PostgreSQL
Enter fullscreen mode Exit fullscreen mode

3. Create a Connection

Connection con = DriverManager.getConnection(
    "jdbc:postgresql://localhost:5432/mydb", "username", "password");
Enter fullscreen mode Exit fullscreen mode

4. Create a Statement

Statement stmt = con.createStatement();
Enter fullscreen mode Exit fullscreen mode

5. Execute the Query

ResultSet rs = stmt.executeQuery("SELECT * FROM users");
Enter fullscreen mode Exit fullscreen mode

6. Process the Results

while (rs.next()) {
    System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
Enter fullscreen mode Exit fullscreen mode

7. Close the Connection

con.close();
Enter fullscreen mode Exit fullscreen mode

✨ Complete Example (JDBC + PostgreSQL)

import java.sql.*;

public class JDBCExample {
    public static void main(String[] args) {
        try {
            Class.forName("org.postgresql.Driver");

            Connection con = DriverManager.getConnection(
                "jdbc:postgresql://localhost:5432/mydb", "postgres", "password");

            Statement stmt = con.createStatement();

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

            while (rs.next()) {
                System.out.println(rs.getInt(1) + " " + rs.getString(2));
            }

            con.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

🧠 JDBC Interfaces Overview

Interface Purpose
Driver Loads the JDBC driver
Connection Establishes the connection to the database
Statement Executes SQL statements
PreparedStatement Precompiled statement for better performance
ResultSet Holds data retrieved from a query
DriverManager Manages a list of database drivers

🚀 Uses of JDBC

  1. Database Connectivity: Connect Java apps to databases.
  2. CRUD Operations: Perform Create, Read, Update, Delete operations.
  3. Data Processing: Fetch and process large datasets.
  4. Enterprise Applications: Backend for web and enterprise systems.
  5. Automation Tools: Create tools that generate reports or manipulate data.
  6. Desktop Applications: Enable local apps to access local/remote databases.
  7. Batch Processing: Insert/update multiple records efficiently.

🆚 JDBC vs ORM (e.g., Hibernate)

Feature JDBC Hibernate (ORM)
Control Full SQL control SQL abstracted with HQL/objects
Learning Curve Easier to start Steeper learning curve
Flexibility High High, but with more abstraction
Productivity Manual mapping Automatic mapping (Entity classes)
Performance Fast for small tasks Better for large applications

Would you like a JDBC program that inserts or updates data, or a JDBC + GUI integration example?

Top comments (0)