1String sql = "SELECT * FROM Users";
2
3Statement statement = conn.createStatement();
4ResultSet result = statement.executeQuery(sql);
5
6int count = 0;
7
8while (result.next()){
9 String name = result.getString(2);
10 String pass = result.getString(3);
11 String fullname = result.getString("fullname");
12 String email = result.getString("email");
13
14 String output = "User #%d: %s - %s - %s - %s";
15 System.out.println(String.format(output, ++count, name, pass, fullname, email));
16}
17
1String sql = "DELETE FROM Users WHERE username=?";
2
3PreparedStatement statement = conn.prepareStatement(sql);
4statement.setString(1, "bill");
5
6int rowsDeleted = statement.executeUpdate();
7if (rowsDeleted > 0) {
8 System.out.println("A user was deleted successfully!");
9}
10
1String sql = "UPDATE Users SET password=?, fullname=?, email=? WHERE username=?";
2
3PreparedStatement statement = conn.prepareStatement(sql);
4statement.setString(1, "123456789");
5statement.setString(2, "William Henry Bill Gates");
6statement.setString(3, "bill.gates@microsoft.com");
7statement.setString(4, "bill");
8
9int rowsUpdated = statement.executeUpdate();
10if (rowsUpdated > 0) {
11 System.out.println("An existing user was updated successfully!");
12}
13
1String sql = "INSERT INTO Users (username, password, fullname, email) VALUES (?, ?, ?, ?)";
2
3PreparedStatement statement = conn.prepareStatement(sql);
4statement.setString(1, "bill");
5statement.setString(2, "secretpass");
6statement.setString(3, "Bill Gates");
7statement.setString(4, "bill.gates@microsoft.com");
8
9int rowsInserted = statement.executeUpdate();
10if (rowsInserted > 0) {
11 System.out.println("A new user was inserted successfully!");
12}
13