【发布时间】:2015-08-28 12:37:10
【问题描述】:
我正在尝试在 JDBC 中执行准备好的语句,每次执行时都会收到错误消息“未设置参数”。 我反复尝试检查未设置的参数 - 但我只有一个。这让我相信这是我的另一个愚蠢错误。
如果您能指出这一点,我将不胜感激
public book search(String key){
book temp = null;
try {
String stmt = "SELECT BookID, Title, Author, Media, Available FROM BOOK WHERE Title LIKE ?;";
connection = DatabaseConnection();
PreparedStatement preparedStmt = connection.prepareStatement(stmt);
System.out.println(key);
preparedStmt.setObject(1, key);//the name
statement = connection.prepareStatement(stmt); //Using Prepared Statements prepare the query
resultSet = statement.executeQuery(); //Execute the query
while (resultSet.next()) {
int bookID = resultSet.getInt("BookID");//Get the userName
String title = resultSet.getString("Title"); //Get the score
String author = resultSet.getString("Author"); //Get the score
String media = resultSet.getString("Media"); //Get the score
boolean available = resultSet.getBoolean("Available"); //Get the score
temp = new book(title,author,media,available,bookID);
}
connection.close(); //close connection
} catch (SQLException e) {
System.err.println("Got an exception!");
System.err.println(e.getMessage());
}
return temp;
}
【问题讨论】:
-
不相关但是:最好使用对应实际数据类型的
setXXX()方法。setObject()可能并不总是按预期工作。所以在你的情况下使用setString(1, key)会更可靠。