0% found this document useful (0 votes)
6 views

Unit2-CRUD-AdditionalReference

The document outlines the creation of a MySQL database named 'testdb' and a 'users' table with fields for id, name, and email. It includes Java code for performing CRUD operations (Create, Read, Update, Delete) on the users table, utilizing JDBC for database connectivity. The code demonstrates error handling and requires the MySQL JDBC driver to be included in the project.

Uploaded by

dali cocky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Unit2-CRUD-AdditionalReference

The document outlines the creation of a MySQL database named 'testdb' and a 'users' table with fields for id, name, and email. It includes Java code for performing CRUD operations (Create, Read, Update, Delete) on the users table, utilizing JDBC for database connectivity. The code demonstrates error handling and requires the MySQL JDBC driver to be included in the project.

Uploaded by

dali cocky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CREATE DATABASE IF NOT EXISTS testdb;

USE testdb;

CREATE TABLE IF NOT EXISTS users (

id INT AUTO_INCREMENT PRIMARY KEY,

name VARCHAR(255) NOT NULL,

email VARCHAR(255) NOT NULL UNIQUE

);

INSERT INTO users (name, email) VALUES

('Alice Johnson', '[email protected]'),

('Bob Smith', '[email protected]'),

('Charlie Brown', '[email protected]'),

('Diana Prince', '[email protected]'),

('Ethan Hunt', '[email protected]'),

('Fiona Gallagher', '[email protected]'),

('George Miller', '[email protected]'),

('Hannah Davis', '[email protected]'),

('Ian Wright', '[email protected]'),

('Julia Roberts', '[email protected]');

------------------------------------------------------Java code -----------------------------------------------

import java.sql.*;

public class MySQLCRUD {

static final String URL = "jdbc:mysql://localhost:3306/testdb";

static final String USER = "root";

static final String PASSWORD = "yourpassword";


public static void main(String[] args) {

try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {

createUser(conn, "John Doe", "[email protected]");

readUsers(conn);

updateUser(conn, 1, "Jane Doe", "[email protected]");

deleteUser(conn, 1);

} catch (SQLException e) {

e.printStackTrace();

public static void createUser(Connection conn, String name, String email) throws SQLException {

String sql = "INSERT INTO users (name, email) VALUES (?, ?)";

try (PreparedStatement stmt = conn.prepareStatement(sql)) {

stmt.setString(1, name);

stmt.setString(2, email);

stmt.executeUpdate();

System.out.println("User added successfully.");

public static void readUsers(Connection conn) throws SQLException {

String sql = "SELECT * FROM users";

try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) {

while (rs.next()) {

System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Email: " +
rs.getString("email"));

}
}

public static void updateUser(Connection conn, int id, String name, String email) throws SQLException
{

String sql = "UPDATE users SET name = ?, email = ? WHERE id = ?";

try (PreparedStatement stmt = conn.prepareStatement(sql)) {

stmt.setString(1, name);

stmt.setString(2, email);

stmt.setInt(3, id);

stmt.executeUpdate();

System.out.println("User updated successfully.");

public static void deleteUser(Connection conn, int id) throws SQLException {

String sql = "DELETE FROM users WHERE id = ?";

try (PreparedStatement stmt = conn.prepareStatement(sql)) {

stmt.setInt(1, id);

stmt.executeUpdate();

System.out.println("User deleted successfully.");

/*

Before running this code, ensure that:

1. MySQL JDBC driver is added to the project.

2. A 'users' table exists in 'testdb' with columns: id (INT, AUTO_INCREMENT, PRIMARY KEY), name
(VARCHAR), email (VARCHAR).

*/
// Advanced JDBC Connection with PreparedStatement and Error Handling

import java.sql.*;

public class AdvancedJDBCConnection {

private static final String URL = "jdbc:mysql://localhost:3306/testdb";

private static final String USER = "root";

private static final String PASSWORD = "yourpassword";

public static void main(String[] args) {

try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {

createUser(conn, "Advanced User", "[email protected]");

readUsers(conn);

updateUser(conn, 1, "Updated User", "[email protected]");

deleteUser(conn, 1);

} catch (SQLException e) {

System.err.println("Connection failed: " + e.getMessage());

public static void createUser(Connection conn, String name, String email) throws SQLException {

String sql = "INSERT INTO users (name, email) VALUES (?, ?)";

try (PreparedStatement stmt = conn.prepareStatement(sql)) {

stmt.setString(1, name);

stmt.setString(2, email);

stmt.executeUpdate();

System.out.println("User added successfully.");

}
public static void readUsers(Connection conn) throws SQLException {

String sql = "SELECT * FROM users";

try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) {

while (rs.next()) {

System.out.println("ID: " + rs.getInt("id") + ", Name: " + rs.getString("name") + ", Email: " +
rs.getString("email"));

public static void updateUser(Connection conn, int id, String name, String email) throws SQLException
{

String sql = "UPDATE users SET name = ?, email = ? WHERE id = ?";

try (PreparedStatement stmt = conn.prepareStatement(sql)) {

stmt.setString(1, name);

stmt.setString(2, email);

stmt.setInt(3, id);

stmt.executeUpdate();

System.out.println("User updated successfully.");

public static void deleteUser(Connection conn, int id) throws SQLException {

String sql = "DELETE FROM users WHERE id = ?";

try (PreparedStatement stmt = conn.prepareStatement(sql)) {

stmt.setInt(1, id);

stmt.executeUpdate();

System.out.println("User deleted successfully.");

}
}

You might also like