JDBC Examples: Master Database Connectivity with Practical Code Snippets

Robert Jul 13, 2026

Java Database Connectivity (JDBC) is a standard Java API used for database connectivity and access. It provides a mechanism to access data from a variety of databases using uniform methods. Mastering JDBC is crucial for any Java developer, as it bridges the gap between Java applications and databases. Let's dive into some practical examples of using JDBC.

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

Before we start, ensure you have the correct database driver (e.g., MySQL Connector/J, Oracle JDBC Driver) added to your project's classpath.

JDBC in Java
JDBC in Java

Establishing a Database Connection

Connecting to a database is the first step in interacting with a data source. It's accomplished using the DriverManager or DataSource class. Here's how you can connect to a MySQL database using both methods:

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

Using DriverManager

DriverManager provides a convenient way to load a driver and obtain a connection to the database.

JDBC - Java Database Connectivity
JDBC - Java Database Connectivity

```java String url = "jdbc:mysql://localhost:3306/mydatabase"; Properties props = new Properties(); props.setProperty("user", "myuser"); props.setProperty("password", "mypassword"); Connection conn = DriverManager.getConnection(url, props); ```

Using DataSource

DataSource is part of the Java EE/Jakarta EE specification and provides a pooled connection interface. Here's an example using a MySQL DataSource:

```java MySQLDataSource dataSource = new MySQLDataSource(); dataSource.setURL("jdbc:mysql://localhost:3306/mydatabase"); dataSource.setUser("myuser"); dataSource.setPassword("mypassword"); DataSource myDataSource = dataSource; Connection conn = myDataSource.getConnection(); ```

Executing SQL Queries

6 JDBC Performance Tips for Java Developers
6 JDBC Performance Tips for Java Developers

Once connected, you can execute SQL statements using the Connection object's createStatement() and prepareStatement() methods.

Executing Simple SELECT Statements

To execute a simple SELECT query, use the createStatement() method and the executeQuery() method:

Difference Between ODBC and JDBC
Difference Between ODBC and JDBC

```java Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM MyTable"); while (rs.next()) { System.out.println(rs.getString("column_name")); } ```

Executing PreparedStatements

PreparedStatements are used for parameterized queries and are more efficient and secure.

JavaScript Basics | Chapter 1 📖 | Part 1
JavaScript Basics | Chapter 1 📖 | Part 1
JavaScript Methods: Add Functionality to Your Objects 🚀
JavaScript Methods: Add Functionality to Your Objects 🚀
JavaScript|DOM操作(要素の取得, 追加, 削除) - わくわくBank
JavaScript|DOM操作(要素の取得, 追加, 削除) - わくわくBank
JDBC Tutorial: Connecting Java to Databases Like a Pro
JDBC Tutorial: Connecting Java to Databases Like a Pro
JTBC 안방판사 Title
JTBC 안방판사 Title
Java Decision Making Cheat Sheet – Code Smarter Decisions! ☕🚀
Java Decision Making Cheat Sheet – Code Smarter Decisions! ☕🚀
the diagram shows how to use django
the diagram shows how to use django
JavaScript Basics | Chapter 1 📖 | Part 2
JavaScript Basics | Chapter 1 📖 | Part 2
a blue and white poster with information about the different types of people's interests
a blue and white poster with information about the different types of people's interests
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JDBC Connection
JDBC Connection
an info sheet with the names and symbols on it
an info sheet with the names and symbols on it
Coding Tips For Beginners, Javascript Function Types, Understanding Javascript Syntax, Learn Javascript Functions, Javascript Functions List, Javascript Function Expression, Javascript Functions Tutorial, Javascript Functions
Coding Tips For Beginners, Javascript Function Types, Understanding Javascript Syntax, Learn Javascript Functions, Javascript Functions List, Javascript Function Expression, Javascript Functions Tutorial, Javascript Functions
Spring JdbcTemplate: Query Single Column Value Example | Spring JDBC Tutorial
Spring JdbcTemplate: Query Single Column Value Example | Spring JDBC Tutorial
the docker registration process is shown in this diagram, which shows how to use it
the docker registration process is shown in this diagram, which shows how to use it
JWT
JWT
a calculator is shown with the numbers below it
a calculator is shown with the numbers below it
the docker containers info sheet
the docker containers info sheet
JavaScript Basics for Beginners | Learn JS Step by Step
JavaScript Basics for Beginners | Learn JS Step by Step
an image of a computer screen with many different types of text and numbers on it
an image of a computer screen with many different types of text and numbers on it

Here's an example of using a PreparedStatement to fetch user data by ID:

Executing DML Queries (INSERT, UPDATE, DELETE)

ForINSERT, UPDATE, and DELETE operations, use the executeUpdate() method of the Statement or PreparedStatement objects.

To insert a new user record:

```java String sql = "INSERT INTO Users (username, email) VALUES (?, ?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "newuser"); pstmt.setString(2, "newuser@example.com"); pstmt.executeUpdate(); ```

To update an existing user's email:

```java PreparedStatement pstmt = conn.prepareStatement("UPDATE Users SET email = ? WHERE user_id = ?"); pstmt.setString(1, "updated_email@example.com"); pstmt.setInt(2, 101); pstmt.executeUpdate(); ```

Mastering JDBC allows you to interact with databases securely and efficiently. Continuously learning and practicing JDBC examples will make you proficient in working with databases in Java.

Happy coding! Keep exploring the world of JDBC to unlock its full potential in your applications.