我尝试使用 prepareStatement 查询 sqlite,但遇到异常:
java.sql.SQLException: not supported by PreparedStatment
at org.sqlite.PrepStmt.unused(PrepStmt.java:328)
at org.sqlite.PrepStmt.executeUpdate(PrepStmt.java:314)
我正在使用 Eclipse 开发我的程序,所以当我点击 at org.sqlite.PrepStmt.unused(PrepStmt.java:328)
时,它会将我重定向到 PrepStmt.class
在里面我发现了这些:
@Override
public int executeUpdate(String sql) throws SQLException {
throw unused();
}
private SQLException unused() {
return new SQLException("not supported by PreparedStatment");
}
这是我的代码:
public static void deleteOp(String word) throws Exception {
Connection c = null;
PreparedStatement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection(connectionString);
c.setAutoCommit(false);
System.out.println("Opened database successfully");
String sql = "DELETE from " + tableName + " where WORD = ? ;";
System.out.println(sql);
stmt = c.prepareStatement(sql);
stmt.clearParameters();
stmt.setString(1, word);
stmt.executeUpdate(sql);
c.commit();
stmt.close();
c.close();
} catch ( Exception e ) {
throw e;
}
System.out.println("Operation done successfully");
}
我想知道是我的代码有问题还是 Sqlite 根本不支持 prepareStatement 还是我的驱动程序有问题(例如由于过时)?
最佳答案
你不需要将 sql
变量传递给 executeUpdate
方法,因为你已经在 prepareStatement
语句中配置了它,所以只需尝试:
stmt.executeUpdate();
关于java - prepareStatement 不适用于 sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22232904/