The method PrepareStatement(String, int) is undefined for the type Connection
时间: 2024-05-09 18:19:55 浏览: 128
This error occurs when you try to use the method PrepareStatement(String, int) on a Connection object, but this method is not defined for the Connection class.
The correct method to use for creating a prepared statement with parameters is PrepareStatement(String sql) or PrepareStatement(String sql, int autoGeneratedKeys) which are defined for the Connection class.
To resolve this error, replace the incorrect method call with the correct one. For example:
```
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "username", "password");
String sql = "SELECT * FROM mytable WHERE id = ?";
PreparedStatement statement = conn.prepareStatement(sql); // use PrepareStatement(String) instead of PrepareStatement(String, int)
statement.setInt(1, 1); // set the parameter value
ResultSet result = statement.executeQuery();
```
Note: The second parameter in the PrepareStatement(String sql, int autoGeneratedKeys) method is used for specifying whether the statement should return auto-generated keys or not. If you don't need this functionality, you can omit the second parameter and just use the PrepareStatement(String sql) method.
阅读全文