2010-03-16 62 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驱动程序是负责确保必要的转义琴弦制造。

相关问题