java crud swing code example

Example 1: CRUD JAVA

String sql = "SELECT * FROM Users";
 
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(sql);
 
int count = 0;
 
while (result.next()){
    String name = result.getString(2);
    String pass = result.getString(3);
    String fullname = result.getString("fullname");
    String email = result.getString("email");
 
    String output = "User #%d: %s - %s - %s - %s";
    System.out.println(String.format(output, ++count, name, pass, fullname, email));
}

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