Java JDBC Insert Example: A Step-by-Step Guide with Code

Robert Jul 13, 2026

Java Database Connectivity (JDBC) empowers developers to connect and interact with databases within the Java programming language. As a fundamental skill, knowing how to execute an insert operation using JDBC enables you to persist data into your database. Let's dive into a practical example illustrating how to insert data into a MySQL database using JDBC.

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

The first step involves adding the MySQL JDBC connector to your project. If you're using Maven, add this dependency to your pom.xml file: <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.26</version></dependency>

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

Setting up the Database Connection

Before inserting data, establish a connection with your MySQL database. Use the following code snippet as a template:

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

```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DatabaseConnection { public static Connection connect() throws SQLException { return DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "your_username", "your_password"); } } ```

Creating a Statement

JDBC Tutorial – Principal JDBC Interfaces and Classes
JDBC Tutorial – Principal JDBC Interfaces and Classes

After establishing a connection, create a Statement object to execute SQL queries.

```java try (Connection conn = connect(); Statement stmt = conn.createStatement()) { // Your insert code here } catch (SQLException e) { System.out.println(e.getMessage()); } ```

Preparing the Insert Statement

JDBC Insert Record Demo
JDBC Insert Record Demo

Craft your SQL INSERT statement, ensuring it aligns with your table schema. Here's an example for a 'users' table:

```java String sql = "INSERT INTO users (id, name, email) VALUES (1, 'John Doe', 'john.doe@example.com')"; ```

Executing the Insert Operation

Java JDBC CRUD Tutorial (SQL Insert, Select, Update and Delete Examples)
Java JDBC CRUD Tutorial (SQL Insert, Select, Update and Delete Examples)

Finally, execute the SQL insert statement using the executeUpdate() method of the Statement object to insert data into the table.

```java try (Connection conn = connect(); Statement stmt = conn.createStatement()) { stmt.executeUpdate(sql); System.out.println("Insertion successful"); } catch (SQLException e) { System.out.println(e.getMessage()); } ```

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 '
Java Decision Making Cheat Sheet – Code Smarter Decisions! ☕🚀
Java Decision Making Cheat Sheet – Code Smarter Decisions! ☕🚀
Implement JavaScript with JSP
Implement JavaScript with JSP
Insertion Sort Algorithm in Java with Example
Insertion Sort Algorithm in Java with Example
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
JDBC in Java
JDBC in Java
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 🚀
JAVA - Student Information System Project In Java Using NetBeans With Source Code | Part 0 - ...
JAVA - Student Information System Project In Java Using NetBeans With Source Code | Part 0 - ...
Bus Reservation System Project in Java with Source Code
Bus Reservation System Project in Java with Source Code
Advanced Java (May – 2018) [IDOL: Revised Course | Question Paper]
Advanced Java (May – 2018) [IDOL: Revised Course | Question Paper]
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Cheat Sheet: Operators, Scanner & User Input ☕
java basics
java basics
Efficient Data Insertion: Specifying Columns with SimpleJdbcInsert in Spring | Spring JDBC Tutorial
Efficient Data Insertion: Specifying Columns with SimpleJdbcInsert in Spring | Spring JDBC Tutorial
Java Conditional Statements Cheat Sheet
Java Conditional Statements Cheat Sheet
6 JDBC Performance Tips for Java Developers
6 JDBC Performance Tips for Java Developers
Java Input Output Cheat Sheet 💻🔥 | Beginner Friendly
Java Input Output Cheat Sheet 💻🔥 | Beginner Friendly
HashMap vs HashSet in Java ⚡ Key Differences Explained (DSA Cheat Sheet)
HashMap vs HashSet in Java ⚡ Key Differences Explained (DSA Cheat Sheet)
a poster describing how to use the data flow chart for an organization's workflow
a poster describing how to use the data flow chart for an organization's workflow
Difference Between JDBC Statement and PreparedStatement
Difference Between JDBC Statement and PreparedStatement

Using PreparedStatements for Better Performance and Security

PreparedStatements offer improved performance as they're precompiled and can be executed multiple times. Moreover, they prevent SQL injection by handling variable binding. Here's how to use them:

```java try (Connection conn = connect(); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO users (id, name, email) VALUES (?, ?, ?)")) { pstmt.setInt(1, 2); pstmt.setString(2, "Jane Doe"); pstmt.setString(3, "jane.doe@example.com"); pstmt.executeUpdate(); System.out.println("Insertion successful"); } catch (SQLException e) { System.out.println(e.getMessage()); } ```

In the realm of JDBC, mastering the insert operation opens doors to persisting data, a crucial aspect of any application. Now that you've understood and executed the JDBC insert example, jump right into refining your skills and exploring more advanced JDBC topics!