CRUD operation using JDBC in Java program code example
Example 1: 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!");
}
Example 2: CRUD JAVA
String sql = "INSERT INTO Users (username, password, fullname, email) VALUES (?, ?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, "bill");
statement.setString(2, "secretpass");
statement.setString(3, "Bill Gates");
statement.setString(4, "[email protected]");
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("A new user was inserted successfully!");
}