CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > JDBC > What is JDBC: Bridging Java and Databases

JDBC Tutorial

  • Introduction
  • Setting Environment
  • Connecting to a Database
  • Executing Statement
  • Processing Results with ResultSet
  • CRUD Operations
  • Transactions
  • JDBC Best Practices
  • Simple JDBC Project
  • Advanced JDBC Topics
  • Common JDBC Issues
  • Interview Questions

What is JDBC: Bridging Java and Databases

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 3 min read · 0 Comments
Share: in X

Learn how Java applications talk to databases using JDBC.

Table of Contents

  • What is JDBC?
  • Why Use JDBC? Benefits Over Manual Database Handling
  • JDBC vs. ORM Tools (e.g., Hibernate)
  • JDBC Architecture: Key Components
    • 1. JDBC Driver
    • 2. Connection
    • 3. Statement
    • 4. ResultSet
  • How JDBC Works: Step-by-Step Flow
  • Hello JDBC: A Simple Example
  • Summary

What is JDBC?

JDBC (Java Database Connectivity) is a Java API (Application Programming Interface) that allows Java programs to interact with relational databases like MySQL, PostgreSQL, or Oracle. Think of it as a “translator” that helps your Java code to communicate with databases using SQL commands.

Real-World Example:
Imagine you’re at a restaurant. You (the Java program) give your order (SQL query) to a waiter (JDBC), who delivers it to the kitchen (database). The waiter then brings back your food (query results). Without JDBC, you’d have to cook yourself!

Why Use JDBC? Benefits Over Manual Database Handling

Before JDBC, developers wrote custom code for every database, which was tedious and error-prone. JDBC simplifies this with:

  1. Abstraction
    • Write database-agnostic code. Switch databases (e.g., MySQL to PostgreSQL) by just changing the driver, not your entire codebase.
  2. Security
    • Built-in safeguards against SQL injection (via PreparedStatement).
  3. Performance
    • Optimized connection handling and batch processing.
  4. Portability
    • JDBC drivers exist for almost all relational databases.
  5. Control
    • Execute raw SQL for complex queries, unlike higher-level ORM tools.

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

FeatureJDBCORM (Hibernate)
ComplexityLow-level controlHigh-level abstraction
Boilerplate CodeMore (manual handling)Less (auto-mapping objects to DB)
Learning CurveEasier for SQL-savvy developersSteeper (requires ORM concepts)
Use CaseSimple apps, raw SQL needsComplex object-relational mapping

When to Choose JDBC:

  • Small projects with simple SQL needs.
  • You need direct control over database operations.
  • Learning fundamentals before diving into ORM.

JDBC Architecture: Key Components

JDBC follows a layered architecture with four core components:

1. JDBC Driver

  • A database-specific implementation (e.g., mysql-connector-java.jar).
  • Converts Java calls into database-understandable commands.

2. Connection

  • Represents a session with the database.
  • Created via DriverManager.getConnection(url, user, password).

3. Statement

  • Object to execute SQL queries (e.g., Statement, PreparedStatement).
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM employees");

4. ResultSet

  • Holds data returned from a query (like a table).
  • Navigate rows and columns using methods like next() and getString().
while (rs.next()) {
  String name = rs.getString("name");
  System.out.println(name);
}

How JDBC Works: Step-by-Step Flow

  1. Load the JDBC driver (e.g., com.mysql.cj.jdbc.Driver).
  2. Establish a Connection to the database.
  3. Create a Statement or PreparedStatement.
  4. Execute SQL (SELECT/INSERT/UPDATE/DELETE).
  5. Process results using ResultSet (for SELECT).
  6. Close resources (ResultSet, Statement, Connection).

Hello JDBC: A Simple Example

import java.sql.*;

public class JdbcIntro {
  public static void main(String[] args) {
    String url = "jdbc:mysql://localhost:3306/mydb";
    String user = "root";
    String password = "secret";

    try (Connection conn = DriverManager.getConnection(url, user, password);
         Statement stmt = conn.createStatement();
         ResultSet rs = stmt.executeQuery("SELECT 'Hello JDBC!' AS message")) {

      if (rs.next()) {
        System.out.println(rs.getString("message")); // Output: Hello JDBC!
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
}

Output:

what is jdbc? A Simple Example

Summary

  • JDBC is the standard API for Java-to-database communication.
  • It provides control, portability, and security over manual database handling.
  • While ORM tools simplify complex mappings, JDBC remains essential for understanding database interactions at a foundational level.

Next Up: Setting Up Your Environment for JDBC – Learn how to install a database, add drivers, and create your first table!

Related Posts:

  • MySQL Commands for Developers
  • How to Connect Java Application to a Remote Database…
  • Control Statements in Java
  • Compile and Run Java Program
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Command Line Arguments in Java
Tags:javajdbc
Was this article helpful?
← Previous ArticleDialog Box in Java Swing
Next Article →What Is Java Swing? A Complete Guide to Java’s GUI Toolkit

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap