To develop a Java standalone application that connects to a MySQL database using Eclipse
and performs CRUD operations (Create, Read, Update, Delete) on a database table, follow
these steps.
Prerequisites:
MySQL: Ensure that MySQL is installed and running on your machine, and that MySQL
Workbench is set up.
Eclipse IDE: Make sure you have Eclipse IDE installed on your system.
JDBC Driver: You will need to download the MySQL JDBC driver (Connector/J), or use Maven to
handle it.
Steps to Implement the Java Standalone Application:
1. Set Up the Database Table
1.1 Create a Database and Table in MySQL:
Use MySQL Workbench or MySQL command line to create a database and a users table.
-- Create the database
CREATE DATABASE testdb;
-- Use the database
USE testdb;
-- Create the table 'users'
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL
);
-- Insert sample data
INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]');
INSERT INTO users (name, email) VALUES ('Jane Doe', '
[email protected]');
2. Set Up the Eclipse Project
2.1 Create a New Java Project in Eclipse:
Open Eclipse IDE.
Go to File > New > Java Project.
Name your project, e.g., DatabaseCRUDApp.
Click Finish.
2.2 Add the MySQL JDBC Driver to the Project:
Option 2: (Manual Method)
Download the MySQL JDBC driver from MySQL Downloads.
Right-click on your project in Eclipse and select Properties.
Go to Java Build Path > Libraries > Add External JARs.
Select the downloaded mysql-connector-java-x.x.x.jar file and add it.
3. Java Code for CRUD Operations
3.1 Create a Java Class for CRUD Operations:
Right-click on the src folder in Eclipse.
Select New > Class and name it DatabaseCRUDOperations.
Copy the following code into your DatabaseCRUDOperations.java file.
import java.sql.*;
public class DatabaseCRUDOperations {
// Database connection credentials
static final String URL = "jdbc:mysql://localhost:3306/testdb"; // Your MySQL URL
static final String USER = "root"; // MySQL username
static final String PASSWORD = "password"; // MySQL password
public static void main(String[] args) {
// Perform CRUD operations
createUser("Alice", "[email protected]");
readUsers();
deleteUser(2);
readUsers(); // Check the updated data
// CREATE operation: Insert new user
public static void createUser(String name, String email) {
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement preparedStatement = connection.prepareStatement(
"INSERT INTO users (name, email) VALUES (?, ?)")) {
preparedStatement.setString(1, name);
preparedStatement.setString(2, email);
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
System.out.println("User added successfully: " + name);
} catch (SQLException e) {
e.printStackTrace();
// READ operation: Retrieve all users
public static void readUsers() {
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM users")) {
System.out.println("Users in the database:");
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String email = resultSet.getString("email");
System.out.println("ID: " + id + ", Name: " + name + ", Email: " + email);
} catch (SQLException e) {
e.printStackTrace();
// UPDATE operation: Update user details
public static void updateUser(int userId, String newName, String newEmail) {
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement preparedStatement = connection.prepareStatement(
"UPDATE users SET name = ?, email = ? WHERE id = ?")) {
preparedStatement.setString(1, newName);
preparedStatement.setString(2, newEmail);
preparedStatement.setInt(3, userId);
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
System.out.println("User with ID " + userId + " updated successfully.");
} catch (SQLException e) {
e.printStackTrace();
// DELETE operation: Delete user by ID
public static void deleteUser(int userId) {
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement preparedStatement = connection.prepareStatement(
"DELETE FROM users WHERE id = ?")) {
preparedStatement.setInt(1, userId);
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
System.out.println("User with ID " + userId + " deleted successfully.");
} catch (SQLException e) {
e.printStackTrace();
4. Running the Application in Eclipse
4.1 Run the Program:
Right-click on the DatabaseCRUDOperations.java file.
Select Run As > Java Application.
4.2 Sample Output:
If everything is set up correctly, the output should look like this:
User added successfully: Alice
Users in the database:
User with ID 1 updated successfully.
User with ID 2 deleted successfully.
Users in the database: