Java SQL Database Example Tutorial CRUD Operations With Code Snippets

Robert Jul 13, 2026

Java and SQL are two powerful tools when used together to manage and manipulate data. Java, a popular object-oriented programming language, can leverage SQL, the standard language for managing and manipulating relational databases, to connect to and interact with databases. Let's explore how to use Java to connect to a SQL database and perform basic operations with an example.

Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis
Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis

First, we need to include the necessary Java libraries in our project. For connecting to a SQL database, we'll use the JDBC (Java Database Connectivity) API. If you're using a MySQL database, you'll need the MySQL JDBC driver. Add this dependency to your project if you're using Maven:

Java code for maintaining a student records database using File Handling
Java code for maintaining a student records database using File Handling

Setting Up the Environment

Before we dive into the code, let's make sure our development environment is set up. If you're using Maven, add the following dependency to your pom.xml file:

What Is a Database in SQL? | SQL Database for Beginners
What Is a Database in SQL? | SQL Database for Beginners

```xml mysql mysql-connector-java 8.0.26 ```

MySQL Connection Example

Now let's see how to connect to a MySQL database using Java. Here's a simple example:

MySQL Sample Database
MySQL Sample Database

```java import java.sql.*; public class MySQLConnectExample { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "your_username"; String password = "your_password"; try (Connection connection = DriverManager.getConnection(url, user, password)) { System.out.println("Connected to the database"); } catch (SQLException e) { System.err.println("Error connecting to the database"); e.printStackTrace(); } } } ```

In this example, replace `mydatabase` with the name of your MySQL database, and `your_username` and `your_password` with your actual MySQL username and password.

Performing Queries

Dan Patrascu-Baba on LinkedIn: #sql #nosql #dotnet #java #javascript
Dan Patrascu-Baba on LinkedIn: #sql #nosql #dotnet #java #javascript

Now that we've established a connection, let's see how to perform a simple SELECT query:

```java try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM mytable")) { ResultSet results = statement.executeQuery(); while (results.next()) { System.out.println(results.getString("column_name")); } } catch (SQLException e) { e.printStackTrace(); } ```

In this example, replace `mytable` with the name of your table, and `column_name` with the name of the column you want to retrieve.

SQL WHERE Clause Explained with Examples | Beginner SQL Tutorial
SQL WHERE Clause Explained with Examples | Beginner SQL Tutorial

Inserting and Updating Data

Java can also be used to insert and update data in a SQL database. Let's see how:

Types of Databases in SQL | RDBMS, NoSQL, Cloud & Data Warehouse
Types of Databases in SQL | RDBMS, NoSQL, Cloud & Data Warehouse
a diagram showing the sequence of different types of data
a diagram showing the sequence of different types of data
SQL SUM() Explained with Examples | Beginner SQL Tutorial
SQL SUM() Explained with Examples | Beginner SQL Tutorial
SQL Quick Notes Cheat Sheet | SQL Queries, Joins, Functions & Database Commands for Beginners
SQL Quick Notes Cheat Sheet | SQL Queries, Joins, Functions & Database Commands for Beginners
a blue and white poster with text on it
a blue and white poster with text on it
SQL AND vs OR Explained with Examples | Beginner SQL Tutorial
SQL AND vs OR Explained with Examples | Beginner SQL Tutorial
SQL Joins Cheat Sheet Every Developer Should Save
SQL Joins Cheat Sheet Every Developer Should Save
SQL & Databases Cheat Sheet for Developers
SQL & Databases Cheat Sheet for Developers
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial
SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial
SQL ORDER BY Explained with Examples | Sort Data in SQL
SQL ORDER BY Explained with Examples | Sort Data in SQL
Introduction to SQL | Beginner Friendly Database Guide
Introduction to SQL | Beginner Friendly Database Guide
SQL GROUP BY Explained Visually (Beginner-Friendly Guide)
SQL GROUP BY Explained Visually (Beginner-Friendly Guide)
SQL BETWEEN Explained with Examples | Beginner SQL Tutorial
SQL BETWEEN Explained with Examples | Beginner SQL Tutorial
SQL Cheat Sheet for Beginners
SQL Cheat Sheet for Beginners
7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL Projects for Beginners | Build a Real Data Portfolio
Joshua Salami Peter
Joshua Salami Peter
SQL GROUP BY Explained with Examples | Beginner SQL Tutorial
SQL GROUP BY Explained with Examples | Beginner SQL Tutorial
SQL Basics Tutorial for Beginners – Structured Query Language Commands, Syntax & Examples Guide
SQL Basics Tutorial for Beginners – Structured Query Language Commands, Syntax & Examples Guide
SQL ORDER BY clause Explained for Beginners
SQL ORDER BY clause Explained for Beginners

Inserting Data

```java String insertQuery = "INSERT INTO mytable (column1, column2) VALUES (?, ?)"; try (PreparedStatement statement = connection.prepareStatement(insertQuery)) { statement.setString(1, "value1"); statement.setString(2, "value2"); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } ```

In this example, replace `mytable` with the name of your table, and `column1` and `column2` with the names of your columns. Replace `"value1"` and `"value2"` with the actual values you want to insert.

Updating Data

```java String updateQuery = "UPDATE mytable SET column1 = ? WHERE condition"; try (PreparedStatement statement = connection.prepareStatement(updateQuery)) { statement.setString(1, "new_value"); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } ```

In this example, replace `mytable` with the name of your table, `column1` with the name of your column, `new_value` with the new value you want to set, and `condition` with the condition that will help you identify the rows to update.

Working with Java and SQL allows you to create powerful applications that can manage and manipulate data. The examples provided here should give you a good starting point. With Java's vast ecosystem and SQL's power, the possibilities are endless. Happy coding!