2014-03-06 30 views
4

我嘗試使用prepareStatement來查詢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,或者我的驅動程序有問題(例如由於過時)?

回答

2

PreparedStatement過着有點雙重生活:它extends Statement,從而繼承該類的方法—,即使其中一些對PreparedStatement沒有太大意義。

在這種情況下,executeUpdate(String)來自Statement並且直接運行聲明,而不進行?替換。這不是你想要的:你只需要executeUpdate(),這是PreparedStatement變種。所以從某種意義上說,他們實際上是在幫你實現Statement變種!

+1

請參閱幾年前的[此主題](https://groups.google.com/forum/#!topic/xerial/YMIJga6TDmA)。它看起來像SQLite的人知道它,但它不是一個高優先級的問題。 – yshavit

5

你並不需要通過sql變量executeUpdate的方法,因爲你已經在prepareStatement句子配置,所以只是嘗試:

stmt.executeUpdate(); 
+1

噢,謝謝。我只是試了一下,它的工作。但是它提供了一個誤導性的錯誤信息! – mok

相關問題