2010-03-16 59 views
0

我已經使用JDBC中的Java創建了與SQLite的數據庫連接。我的SQL語句執行正常,但有時我得到以下錯誤,而我用conn.commit()java.sql.SQLException:SQL邏輯錯誤或缺少數據庫,SQLite,JDBC

java.sql.SQLException: SQL logic error or missing database 

任何人都可以請幫我如何避免這類問題。有沒有更好的方法來調用JDBC程序?

Class.forName("org.sqlite.JDBC"); 
conn = DriverManager.getConnection("jdbc:sqlite:/home/Data/database.db3"); 
conn.setAutoCommit(false); 

String query = "Update Chits set BlockedForChit = 0 where ServerChitID = '" + serverChitId + "' AND ChitGatewayID = '" + chitGatewayId + "'"; 
     Statement stmt = conn.createStatement(); 
     try { 
      stmt.execute(query); 
      conn.commit(); 
      stmt.close(); 
      stmt = null; 
     } 
+1

如果stmt爲null,你怎麼調用.execute呢? – unholysampler 2010-03-16 15:24:43

回答

2

的變量可以serverChitId & chitGatewayId包含字符會破壞SQL?它通常是使用更安全的PreparedStatement:

PreparedStatement ps = conn.prepareStatement("Update Chits set BlockedForChit = 0 where ServerChitID = ? AND ChitGatewayID = ?"); 
ps.setString(1, serverChitId); 
ps.setString(2, chitGatewayId); 
ps.executeUpdate(); 

這樣的JDBC驅動程序是負責確保必要的轉義琴絃製造。

相關問題