Complete Java JDBC Example Tutorial With Code Snippets

Robert Jul 13, 2026

Java Database Connectivity (JDBC) is a set of Java API specifications for accessing and manipulating with different databases. It provides a standard interface for application code to access data in databases irrespective of the underlying database system. Let's dive into an example of JDBC to understand how to connect to a database, execute SQL queries, and fetch results.

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

To get started, first you need to add the JDBC driver to your project. For instance, if you're connecting to a MySQL database, add the MySQL JDBC connector to your dependencies:

Introduction to JDBC | Java Database Connectivity
Introduction to JDBC | Java Database Connectivity
Maven: com.mysql:mysql-connector-java
Gradle: implementation 'mysql:mysql-connector-java'

Establishing a JDBC Connection

JDBC Architecture
JDBC Architecture

The first step in performing database operations using JDBC is establishing a connection to the database. This involves providing the necessary credentials and configuration details.

Here's an example of how to connect to a MySQL database using JDBC:

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

```java String url = "jdbc:mysql://localhost/db_name"; String user = "username"; String password = "password"; try (Connection connection = DriverManager.getConnection(url, user, password)) { // Connection established successfully } catch (SQLException e) { // Handle exception } ```

Executing SQL Queries

Once you have established a connection, you can create statements to execute SQL queries.

Here's an example of fetching data from a table and processing the results:

JDBC in Java
JDBC in Java

```java try (Connection connection = DriverManager.getConnection(url, user, password); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM table_name")) { while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); // Process the fetched data } } catch (SQLException e) { // Handle exception } ```

Using PreparedStatements for Parameterized Queries

To avoid SQL injection, use PreparedStatements when dealing with user input in queries. Here's an example:

```java try (Connection connection = DriverManager.getConnection(url, user, password); PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM table_name WHERE id = ?")) { preparedStatement.setInt(1, userId); ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { // Process the fetched data } } catch (SQLException e) { // Handle exception } ```

Demonstrating JDBC with Java 8 Features

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

JDBC can be used in conjunction with Java 8 features like try-with-resources and lambda expressions. Here's an example:

```java try (Connection connection = DriverManager.getConnection(url, user, password); Statement statement = connection.createStatement()) { List tableNames = statement.executeQuery("SHOW TABLES") .map(rs -> rs.getString(1)) .collect(Collectors.toList()); // Process the table names } catch (SQLException e) { // Handle exception } ```

Understanding and using JDBC effectively can significantly simplify the process of accessing and manipulating databases in your Java applications. Happy coding!

JDBC - Java Database Connectivity
JDBC - Java Database Connectivity
How to use PreparedStatement in Java - JDBC Example Tutorial
How to use PreparedStatement in Java - JDBC Example Tutorial
A Beginners Guide to Java JDBC
A Beginners Guide to Java JDBC
Java Roadmap 2026 ☕ | Complete Beginner to Advanced Learning Guide
Java Roadmap 2026 ☕ | Complete Beginner to Advanced Learning Guide
6 JDBC Performance Tips for Java Developers
6 JDBC Performance Tips for Java Developers
Java Decision Making Cheat Sheet – Code Smarter Decisions! ☕🚀
Java Decision Making Cheat Sheet – Code Smarter Decisions! ☕🚀
JDBC Tutorial – Principal JDBC Interfaces and Classes
JDBC Tutorial – Principal JDBC Interfaces and Classes
JDBC in Java
JDBC in Java
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
JDBC in Java
JDBC in Java
Java Cheatsheet
Java Cheatsheet
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 '
JDBC Scrollable ResultSet
JDBC Scrollable ResultSet
✅ Ngôn ngữ Java ⭐️⭐️⭐️⭐️⭐️
✅ Ngôn ngữ Java ⭐️⭐️⭐️⭐️⭐️
JDBC ODBC Bridge - Java | Geekboots
JDBC ODBC Bridge - Java | Geekboots
Java JDBC CRUD Tutorial (SQL Insert, Select, Update and Delete Examples)
Java JDBC CRUD Tutorial (SQL Insert, Select, Update and Delete Examples)
Why use PreparedStatement in Java JDBC? Example Tutorial
Why use PreparedStatement in Java JDBC? Example Tutorial
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Java Database Programming (JDBC) by Examples with MySQL
Java Database Programming (JDBC) by Examples with MySQL