Maurice Muteti
2 min readDec 22, 2022

--

Java CRUD Operation MySql (SQL Insert, Select, Update and Delete)

To perform CRUD (create, read, update, delete) operations on a MySQL database using Java, you can use the MySQL Connector/J driver, which is a MySQL-specific JDBC driver that allows you to connect to a MySQL database and execute SQL statements.

Here is an example of how you can use the MySQL Connector/J driver to perform a simple CRUD operation in Java:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main
public static void main(String[] args)
// Load the MySQL Connector/J driver
try
Class.forName("com.mysql.cj.jdbc.Driver");
catch (ClassNotFoundException e)
System.err.println("Error loading MySQL Connector/J driver: " + e);
return;


// Establish a connection to the database
Connection conn = null;
try
conn = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "username", "password");
catch (SQLException e)
System.err.println("Error connecting to the database: " + e);
return;


// Perform a simple CRUD operation
try
// Create a new record
PreparedStatement stmt = conn.prepareStatement("INSERT INTO users (name, email) VALUES (?, ?)");
stmt.setString(1, "John Smith");
stmt.setString(2, "john@example.com");
stmt.executeUpdate();

// Read all records
stmt = conn.prepareStatement("SELECT * FROM users");
ResultSet rs = stmt.executeQuery();
while (rs.next())
int id = rs.getInt("id");
String name = rs.getString("name");
String email = rs.getString("email");
System.out.println("id: " + id + ", name: " + name + ", email: " + email);


// Update a record
stmt = conn.prepareStatement("UPDATE users SET name = ? WHERE id = ?");
stmt.setString(1, "Jane Smith");
stmt.setInt(2, 1);
stmt.executeUpdate();

// Delete a record
stmt = conn.prepareStatement("DELETE FROM users WHERE id = ?");
stmt.setInt(1, 1);
stmt.executeUpdate();
catch (SQLException e)
System.err.println("Error performing CRUD operation: " + e);


// Close the connection
try
conn.close();
catch (SQLException e)
System.err.println("Error closing connection: " + e);

phpMyAdmin SQL Dump

-- phpMyAdmin SQL Dump
-- version 5.1.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Dec 22, 2022 at 11:07 PM
-- Server version: 10.4.20-MariaDB
-- PHP Version: 8.0.8

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `testdb`
--
CREATE DATABASE IF NOT EXISTS `testdb` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `testdb`;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

--

--