繁体   English   中英

prepareStatement不适用于sqlite

[英]prepareStatement doesn't work with sqlite

我尝试使用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();

PreparedStatement有点双重生活:它extends Statement ,因此继承了该类的方法 - 尽管其中一些对PreparedStatement没有多大意义。

在这种情况下, executeUpdate(String)来自Statement并直接运行语句,而不执行? 换人。 这不是你想要的:你只需要executeUpdate() ,这是PreparedStatement变体。 所以在某种意义上,他们实际上是通过不实现Statement变体来帮助你的!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM