[英]JDBC PreparedStatement.setString() not working properly with SQLite?
I'm using Xerials SQLite JDBC driver and want to do the simplest thing you can imagine: 我正在使用Xerials SQLite JDBC驱动程序,并想做您可以想象的最简单的事情:
I have created the following table: 我已经创建了下表:
CREATE TABLE IF NOT EXISTS households (hostname_id_pk INTEGER PRIMARY KEY, hostname TEXT UNIQUE, vm TEXT);
Now, as I want a parametrized insertion statment I use PreparedStatment as follows: 现在,由于需要参数化的插入语句,因此按如下方式使用PreparedStatment:
public static void main(String[] args) throws ClassNotFoundException {
Class.forName("org.sqlite.JDBC");
Connection con = null;
PreparedStatement updateHouseholdStmt = null;
try {
con = DriverManager.getConnection(DB_IDENTIFIER);
String getHouseholdString = "SELECT hostname_id_pk FROM households WHERE hostname = ?";
String updateHouseholdString = "INSERT INTO households (hostname, vm) values(?, 'VM1')";
getHouseholdStmt = con.prepareStatement(getHouseholdString);
getHouseholdStmt.setString(1, "test1");
updateHouseholdStmt = con.prepareStatement(updateHouseholdString);
getHouseholdStmt.setString(1, "test1");
ResultSet hhRS = getHouseholdStmt.executeQuery();
int hhId = -1;
if(hhRS.next()){
hhId = hhRS.getInt(1);
System.out.println(hhId);
} else {
System.out.println(updateHouseholdStmt.executeUpdate());
}
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
try {
if (con != null) {
con.close();
}
if (getHouseholdStmt != null){
getHouseholdStmt.close();
}
if (updateHouseholdStmt != null) {
updateHouseholdStmt.close();
}
} catch (SQLException e) {
System.err.println(e);
}
}
}
In this case, the only thing I can find in the DB is a new entry with (1, , VM1)
meaning that the entry is created, but the string parameter is missing for some reason. 在这种情况下,我在数据库中唯一能找到的是带有
(1, , VM1)
的新条目,这意味着该条目已创建,但是由于某种原因缺少了字符串参数。
When I'm replacing the "?" 当我替换“?”时 in the
updateHousholdStatement()
with a sample value and remove the .setString(...)
method line everything works fine. 在带有示例值的
updateHousholdStatement()
并删除.setString(...)
方法行,一切正常。
What the heck am I missing?! 我到底想念什么? I already checked this a thousand times this morning.
今天早上我已经检查了一千遍了。
Thx in advance. 提前谢谢。
Please correct the following lines: 请更正以下几行:
updateHouseholdStmt = con.prepareStatement(updateHouseholdString);
getHouseholdStmt.setString(1, "test1");
to 至
updateHouseholdStmt = con.prepareStatement(updateHouseholdString);
updateHouseholdStmt.setString(1, "test1");
When updating, you're setting and executing the wrong variable: 更新时,您正在设置和执行错误的变量:
getHouseholdStmt = con.prepareStatement(getHouseholdString);
getHouseholdStmt.setString(1, "test1");
updateHouseholdStmt = con.prepareStatement(updateHouseholdString);
getHouseholdStmt.setString(1, "test1"); //updating the select query
ResultSet hhRS = getHouseholdStmt.executeQuery(); //you're executing the select query again!
It should be: 它应该是:
getHouseholdStmt = con.prepareStatement(getHouseholdString);
getHouseholdStmt.setString(1, "test1");
updateHouseholdStmt = con.prepareStatement(updateHouseholdString);
updateHouseholdStmt .setString(1, "test1"); //update stmt
ResultSet hhRS = updateHouseholdStmt .executeQuery(); //update stmt
First, getHouseholdStmt Statement isn't declared as a PreparedStatement. 首先,未将getHouseholdStmt语句声明为PreparedStatement。 Second, you need to executeUpdate() your PreparedStatement for updateHouseholdStmt(updateHouseholdStatement.executeUpdate();)
其次,您需要为updateHouseholdStmt(updateHouseholdStatement.executeUpdate();)执行PreparedStatement。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.