crud example in java

Example 1: CRUD JAVA

String sql = "DELETE FROM Users WHERE username=?";
 
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, "bill");
 
int rowsDeleted = statement.executeUpdate();
if (rowsDeleted > 0) {
    System.out.println("A user was deleted successfully!");
}

Example 2: CRUD JAVA

String sql = "UPDATE Users SET password=?, fullname=?, email=? WHERE username=?";
 
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, "123456789");
statement.setString(2, "William Henry Bill Gates");
statement.setString(3, "[email protected]");
statement.setString(4, "bill");
 
int rowsUpdated = statement.executeUpdate();
if (rowsUpdated > 0) {
    System.out.println("An existing user was updated successfully!");
}

Tags:

Java Example