java - prepareStatement 不适用于 sqlite

标签 java sqlite jdbc prepared-statement

我尝试使用 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/

相关文章:

android - 为生产部署 django 应用程序的必要程序

python - 从 python sqlite 异常获取更多信息

Java JDBC mysql 连接池

java - 错误 : Target id is not valid.

Java:将文本文件输出到控制台

Python SQLite 错误

java - 从 Java 连接到托管在虚拟机中的 MYSQL 数据库

java.lang.ClassNotFoundException : com. mysql.jdbc.Driver - 不工作

java - Vaadin 多个浏览器窗口/选项卡

java - 我可以使用什么库来解析 Java 中的单词?