exp 5 (1)
exp 5 (1)
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.
Use MySQL Workbench or MySQL command line to create a database and a users table.
USE testdb;
);
Click Finish.
import java.sql.*;
createUser("Alice", "alice@example.com");
readUsers();
deleteUser(2);
preparedStatement.setString(1, name);
preparedStatement.setString(2, email);
if (rowsAffected > 0) {
} catch (SQLException e) {
e.printStackTrace();
while (resultSet.next()) {
int id = resultSet.getInt("id");
System.out.println("ID: " + id + ", Name: " + name + ", Email: " + email);
} catch (SQLException e) {
e.printStackTrace();
preparedStatement.setString(1, newName);
preparedStatement.setString(2, newEmail);
preparedStatement.setInt(3, userId);
if (rowsAffected > 0) {
} catch (SQLException e) {
e.printStackTrace();
preparedStatement.setInt(1, userId);
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
} catch (SQLException e) {
e.printStackTrace();