mok*_*mok 4 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)
Run Code Online (Sandbox Code Playgroud)
我正在使用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");
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
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");
}
Run Code Online (Sandbox Code Playgroud)
我想知道我的代码有问题或Sqlite根本不支持prepareStatement或者我的驱动程序有问题(例如由于过时)?
您不需要将sql变量传递给executeUpdate方法,因为您已在prepareStatement句子上配置它,所以只需尝试:
stmt.executeUpdate();
Run Code Online (Sandbox Code Playgroud)
PreparedStatement过着双重生活: it extends Statement,因此继承了该类的方法——尽管其中一些方法对于 来说并没有多大意义PreparedStatement。
在本例中,executeUpdate(String)来自Statement并直接运行一条语句,而不进行?替换。这不是你想要的:你想要的只是executeUpdate(),这是PreparedStatement变体。因此,从某种意义上说,他们不实施该Statement变体实际上是在帮你一个忙!
| 归档时间: |
|
| 查看次数: |
1746 次 |
| 最近记录: |