JDBC in Java with Example: A Complete Beginner’s Guide

Robert Jul 13, 2026

Java Database Connectivity (JDBC) is a standard Java API used for database access and manipulation. It's like a bridge that connects the Java applications with the database, enabling them to interact and communicate. JDBC uses a set of interfaces and classes to define how a client application should access a database.

Introduction to JDBC | Java Database Connectivity
Introduction to JDBC | Java Database Connectivity

At its core, JDBC is an abstraction layer that allows Java programs to connect to diverse databases like Oracle, MySQL, or SQL Server, with a single API. This means you can write your database queries once and run them on any supported database without changing the code.

two hands holding each other with the words jdbc in front of them and buildings
two hands holding each other with the words jdbc in front of them and buildings

Key Components of JDBC

JDBC is built around a few key components that work together to facilitate database interaction:

JDBC Architecture
JDBC Architecture

Driver Manager

The Driver Manager is a class that loads the database drivers and manages connections to the database. It's the entry point to the JDBC API and is created by default when a Java application starts. The Driver Manager uses the java.sql.DriverManager class.

JDBC in Java
JDBC in Java

To load a driver, you would typically use the following code: ```java DriverManager.registerDriver(new com.mysql.jdbc.Driver()); ```

Database Drivers

Drivers are the implementations of JDBC interfaces that understand the dialect of a specific database. They enable a Java application to connect to that database and execute queries or update operations. Drivers can be Type 1, Type 2, Type 3, Type 4, or pure Java, depending on how they interact with the database.

JDBC Statement Interface in Java with Example
JDBC Statement Interface in Java with Example

To connect to a database, you can use the following code snippet: ```java String url = "jdbc:mysql://localhost:3306/mydatabase"; Properties props = new Properties(); props.setProperty("user","root"); props.setProperty("password","password"); Connection conn = DriverManager.getConnection(url, props); ```

Executing Queries with JDBC

Once a connection is established, you can execute SQL queries and manipulate data in the database:

JDBC in Java Explained | Database Connectivity Easy Notes
JDBC in Java Explained | Database Connectivity Easy Notes

Statement Interface

The java.sql.Statement interface is used for executing static SQL queries. It provides methods like executeQuery(), executeUpdate(), and execute() to run SELECT, UPDATE, INSERT, or DELETE queries, respectively.

Java Variables Explained | Local, Static & Instance Variables in Java
Java Variables Explained | Local, Static & Instance Variables in Java
A Beginners Guide to Java JDBC
A Beginners Guide to Java JDBC
JDBC in Java
JDBC in Java
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
JDBC - Java Database Connectivity
JDBC - Java Database Connectivity
6 JDBC Performance Tips for Java Developers
6 JDBC Performance Tips for Java Developers
JDBC in Java
JDBC in Java
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
How to use PreparedStatement in Java - JDBC Example Tutorial
How to use PreparedStatement in Java - JDBC Example Tutorial
JDBC Tutorial – Principal JDBC Interfaces and Classes
JDBC Tutorial – Principal JDBC Interfaces and Classes
Java Roadmap 2026 ☕ | Complete Beginner to Advanced Learning Guide
Java Roadmap 2026 ☕ | Complete Beginner to Advanced Learning Guide
Difference Between JDBC Statement and PreparedStatement
Difference Between JDBC Statement and PreparedStatement
JDBC Introduction: Connecting Java Applications to Databases
JDBC Introduction: Connecting Java Applications to Databases
Advanced Java (May – 2018) [IDOL: Revised Course | Question Paper]
Advanced Java (May – 2018) [IDOL: Revised Course | Question Paper]
☕ What Is Java? | Simple Guide for Beginners 🚀
☕ What Is Java? | Simple Guide for Beginners 🚀
JDBC : Steps to Connect to the Database
JDBC : Steps to Connect to the Database
✅ Ngôn ngữ Java ⭐️⭐️⭐️⭐️⭐️
✅ Ngôn ngữ Java ⭐️⭐️⭐️⭐️⭐️
Why use PreparedStatement in Java JDBC? Example Tutorial
Why use PreparedStatement in Java JDBC? Example Tutorial
JDBC ODBC Bridge - Java | Geekboots
JDBC ODBC Bridge - Java | Geekboots

Here's an example of executing a SELECT query: ```java Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM Employee"); while (rs.next()) { System.out.println(rs.getString("name") + " - " + rs.getInt("age")); } ```

PreparedStatement Interface

The java.sql.PreparedStatement interface is used for executing pre-compiled SQL queries. It's more efficient and secure than the Statement interface as it separates SQL logic from query parameters, helping to prevent SQL injection attacks.

Here's an example of using a PreparedStatement: ```java String sql = "SELECT * FROM Employee WHERE age = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, 30); ResultSet rs = pstmt.executeQuery(); ```

Finally, JDBC provides advanced features like batch updates, callable statements, and transaction management to handle complex database operations efficiently.

JDBC is a powerful API that simplifies database access in Java. By understanding and utilizing its components and features, developers can create robust and maintainable database applications. To get started with JDBC, ensure you have the required JDBC drivers for your target database and refer to the official Oracle documentation for detailed API information.