how to use prepareStatement
时间: 2024-06-01 20:09:57 浏览: 50
Modular Programming with Python
1. Establish a connection to the database using DriverManager.getConnection() method.
2. Create a PreparedStatement object by calling the prepareStatement() method of the Connection object.
Example:
PreparedStatement stmt = conn.prepareStatement("INSERT INTO students (name, age) VALUES (?, ?)");
3. Set the parameter values using the appropriate setter methods.
Example:
stmt.setString(1, "John");
stmt.setInt(2, 20);
4. Execute the SQL statement using the executeUpdate() or executeQuery() method of the PreparedStatement object.
Example:
int rows = stmt.executeUpdate();
5. Close the PreparedStatement object and the Connection object using the close() method.
Example:
stmt.close();
conn.close();
阅读全文