String insertQuery = "INSERT INTO items(name, price, quantity) VALUES(?,?,?)"; PreparedStatement pstmt = conn.prepareStatement(insertQuery); pstmt.setString(1, "item_1"); pstmt.setDouble(2, 12.99); pstmt.setInt(3, 2); pstmt.executeUpdate();
String updateQuery = "UPDATE items SET price = ?, quantity = ? WHERE name = ?"; PreparedStatement pstmt = conn.prepareStatement(updateQuery); pstmt.setDouble(1, 14.99); pstmt.setInt(2, 3); pstmt.setString(3, "item_1"); pstmt.executeUpdate();
String selectQuery = "SELECT * FROM items WHERE name = ?"; PreparedStatement pstmt = conn.prepareStatement(selectQuery); pstmt.setString(1, "item_1"); ResultSet rs = pstmt.executeQuery();The java.util.PreparedStatement class is a part of the java.sql package library in Java.