JDBC Tutorial in Java W3Schools: Complete Guide with Examples

Robert Jul 13, 2026

Java Database Connectivity (JDBC) is a standard API for connecting to databases from Java programs. Possessing a robust set of features to facilitate communication between Java applications and databases, JDBC helps developers build scalable and maintainable solutions. This tutorial, based on W3Schools' comprehensive content, will guide you through the essentials of JDBC, empowers you to interact with databases effectively from your Java projects.

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

Before we dive into the specifics of JDBC, ensure you have a solid understanding of Java and the Relational Database Management System you'll be working with, like MySQL, Oracle, or SQL Server. Also, make sure you have the JDBC driver for your database installed in your project's classpath.

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

Establishing a Database Connection

Setting up a connection with your database is the first step. This involves loading the JDBC driver, specifying the database URL, and providing the correct username and password for authentication.

JAVA Database Tutorial with JDBC with MySql Complete Course
JAVA Database Tutorial with JDBC with MySql Complete Course

Here's a simple example of establishing a connection:

```java import java.sql.*; public class ConnectionExample { public static void main(String[] args) { String url = "jdbc:mysql://localhost/test"; String user = "username"; String password = "password"; try { Connection conn = DriverManager.getConnection(url, user, password); } catch (SQLException e) { System.out.println("Connection failed: " + e.getMessage()); } } } ```

Using DriverManager

Java Roadmap 2026 β˜• | Complete Beginner to Advanced Learning Guide
Java Roadmap 2026 β˜• | Complete Beginner to Advanced Learning Guide

The JDBC DriverManager class facilitates loading database drivers and establishing connections. It maintains a list of driver classes, allowing connections to be made to various types of databases:

  • PostgreSQL: oracle.jdbc.driver.OracleDriver
  • MySQL: com.mysql.cj.jdbc.Driver
  • SQL Server: com.microsoft.sqlserver.jdbc.SQLServerDriver

Using DataSource

the info sheet shows how to learn in 30 days
the info sheet shows how to learn in 30 days

The javax.sql.DataSource interface offers a more convenient and powerful way to establish connections by enabling the management of database connections in a pool. This enhances performance and security:

Note: DataSource requires deployment in a J2EE server environment.

Creating Statements and Executing SQL Commands

JDBC Tutorial: Connecting Java to Databases Like a Pro
JDBC Tutorial: Connecting Java to Databases Like a Pro

After establishing a connection, you can create Statement objects to execute SQL commands. JDBC provides several types of Statement objects catering to different use cases:

Statement

JDBC in Java
JDBC in Java
6 JDBC Performance Tips for Java Developers
6 JDBC Performance Tips for Java Developers
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 Become a Java Developer (Step-by-Step)
How to Become a Java Developer (Step-by-Step)
Complete Java Roadmap for Beginners in 2026
Complete Java Roadmap for Beginners in 2026
Java Constructors Explained | Easy Cheat Sheet for Beginners
Java Constructors Explained | Easy Cheat Sheet for Beginners
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
Java Project Ideas For Beginners
Java Project Ideas For Beginners
Learn Java in 30 Days Only
Learn Java in 30 Days Only
Java Learning Roadmap 2026 β˜•πŸš€
Java Learning Roadmap 2026 β˜•πŸš€
JDBC in Java
JDBC in Java
Web development programming coding for beginners
Web development programming coding for beginners
JavaScript road map #JavaScript #js #learn #gk #JavaScript #AI #WebDevelopment
JavaScript road map #JavaScript #js #learn #gk #JavaScript #AI #WebDevelopment
Java Basics in 15 Minutes β˜• | Beginner-Friendly Java Crash Course
Java Basics in 15 Minutes β˜• | Beginner-Friendly Java Crash Course
10 Best Websites to Learn Java for Free in 2026
10 Best Websites to Learn Java for Free in 2026
Steps to becoming a java pro
Steps to becoming a java pro
Understanding JDK, JRE, and JVM in Java

If you are starting with Java development, it’s important to understand these three terms:

JDK (Java Development Kit) – Includes JRE, compiler, debugger, and other development tools.
JRE (Java Runtime Environment) – Provides JVM and class libraries required to run applications.
JVM (Java Virtual Machine) – Executes Java bytecode and makes Java platform-independent.

In simple terms:

Developers use JDK to build programs.

Applications run on JRE.

Exe... Class Library, Java Programming, Backend Developer, Java, Programming, Coding
Understanding JDK, JRE, and JVM in Java If you are starting with Java development, it’s important to understand these three terms: JDK (Java Development Kit) – Includes JRE, compiler, debugger, and other development tools. JRE (Java Runtime Environment) – Provides JVM and class libraries required to run applications. JVM (Java Virtual Machine) – Executes Java bytecode and makes Java platform-independent. In simple terms: Developers use JDK to build programs. Applications run on JRE. Exe... Class Library, Java Programming, Backend Developer, Java, Programming, Coding
Learn Java in 30 Days Only
Learn Java in 30 Days Only
Master Two Pointers Technique πŸ’» | Java DSA Interview Preparation Guide
Master Two Pointers Technique πŸ’» | Java DSA Interview Preparation Guide

The Statement class is used for executing SQL queries that don't return result sets. You can execute SQL statements andmn store the results in a ResultSet:

```java String sql = "CREATE TABLE IF NOT EXISTS Customers (" + "FirstName varchar(50)," + "LastName varchar(50)," + "Email varchar(50));"; try (Statement stmt = conn.createStatement()) { stmt.execute(sql); } ```

PreparedStatement

The PreparedStatement class is used for executing statements where certain values need to be specified:

```java String sql = "INSERT INTO Customers (FirstName, LastName, Email) VALUES (?,?,?)"; try (PreparedStatement pstmt = conn.prepareStatement(sql)) { pstmt.setString(1, "John"); pstmt.setString(2, "Doe"); pstmt.setString(3, "john@example.com"); pstmt.executeUpdate(); } ```

CallableStatement

The CallableStatement class is used to execute SQL stored procedures and functions:

```java Call call = conn.prepareCall("{call AddCustomer(?,?,?)}"); call.setString(1, "John"); call.setString(2, "Doe"); call.setString(3, "john@example.com"); call.execute(); ```

The examples above demonstrate the sqlite capabilities of JDBC statements and how to use them effectively. Remember to close your Statement objects after use to free up system resources.

Retrieving and Processing Results

After executing queries that return result sets, you can process those results using ResultSet objects. The JDBC API provides various methods to retrieve and navigate the data:

ΠšΡƒΡ€ΡΠΎΡ€ movement and access to columns values

The ResultSet class allows for cursor movement and access to the data in columns. You can retrieve data using different get methods, such as getString(), getInt(), or getDouble(), depending on the column's type:

```java ResultSet result = stmt.executeQuery("SELECT * FROM Customers"); while (result.next()) { System.out.println(result.getString("FirstName")); System.out.println(result.getString("LastName")); System.out.println(result.getString("Email")); } ```

Metadata access

The ResultSet provides metadata information about the result set and the columns it contains. You can retrieve metadata information using methods like ResultSetMetaData:

```java ResultSet result = stmt.executeQuery("SELECT * FROM Customers"); ResultSetMetaData md = result.getMetaData(); int columnCount = md.getColumnCount(); for (int i = 1; i <= columnCount; i++) { System.out.println("Column " + i + ": " + md.getColumnName(i)); } ```

Batch Processing

JDBC allows for batch processing – the execution of multiple SQL statements grouped together. This increases efficiency, as only one connection is required:

```java String sql = "UPDATE Customers SET Email = ? WHERE FirstName = ?"; try (PreparedStatement pstmt = conn.prepareStatement(sql)) { pstmt.setString(1, "new_email@example.com"); pstmt.setString(2, "John"); pstmt.addBatch(); pstmt.setString(1, "updated_email@example.com"); pstmt.setString(2, "Jane"); pstmt.addBatch(); pstmt.executeBatch(); } ```

Batch processing ensures optimized performance when updating multiple records simultaneously.

Transactions and Connection Management

Maintaining database consistency is essential when updating, deleting, or retrieving data. JDBC enables the creation of transactions and ensures that all operations are performed consistently:

Transaction control

Transactions are managed using the Connection object's commit(), rollback(), and setAutoCommit() methods:

```java try (Connection conn = DriverManager.getConnection(url, user, password)) { conn.setAutoCommit(false); try (Statement stmt = conn.createStatement()) { stmt.execute("UPDATE Customers SET Email = 'updated@example.com' WHERE FirstName = 'John'"); conn.commit(); } catch (SQLException ex) { conn.rollback(); } } ```

Connection pooling

To optimize resource usage and improve performance, database connections can be managed in pools using technologies like Java Database Connectivity (JDBC).

Mastering JDBC is a vital skill for any Java developer working with databases. This tutorial has provided an overview of the essential concepts and methods to help you establish effective communication with databases from your Java applications.

Now that you have learned about JDBC, delve deeper into advanced topics like connection pooling, transaction management, and working with databases that support SQL Operations Language (SOL). Explore more resources on W3Schools and FAQs to build a robust JDBC skill set and elevate your development prowess.