[英]JDBC doesn't store update in sqlite database
我編寫了一個應用程序,它使用JDBC在SQlite數據庫中插入一些數據,並具有讀取/更新該數據的能力。
問題是當我通過應用程序更改數據時它會更新數據,而當程序打開時,讀取這些數據會產生新的(更新的)數據。
但是當我退出程序並重新運行它時,所有這些更改都會丟失,並且數據會在第一次打開程序時重置為!
我這樣連接:
Class.forName("org.sqlite.JDBC");
con=DriverManager.getConnection("jdbc:sqlite:archive.sqlite");
然后像這樣更新四個表:
try{
String Q="UPDATE data SET startdate=?,fazelab=?,length=?,maliat=?,other=?,price=?,public=?,usage=?,abbaha=?,enddate=? WHERE id='"+ID+"'";
PreparedStatement PS= this.con.prepareStatement(Q);
PS.setString(1, (String)data.get("startdate"));
PS.setInt(2, (Integer)data.get("fazelab"));
PS.setInt(3, (Integer)data.get("length"));
PS.setInt(4, (Integer)data.get("maliat"));
if(data.containsKey("other"))
PS.setInt(5, (Integer)data.get("other"));
else
PS.setInt(5, 0);
PS.setLong(6, (Long)data.get("price"));
PS.setDouble(7, (Double)data.get("public"));
PS.setInt(8, (Integer)data.get("usage"));
PS.setInt(9, (Integer)data.get("abbaha"));
PS.setString(10, (String)data.get("enddate"));
COUNT=PS.executeUpdate();
Q="UPDATE cold SET cE1=?,cW1=?,cE2=?,cW2=?,cE3=?,cW3=?,cE4=?,cW4=? WHERE dataid='"+ID+"'";
PS= this.con.prepareStatement(Q);
PS.setDouble(1, cold.get("cE1"));
PS.setDouble(2, cold.get("cW1"));
PS.setDouble(3, cold.get("cE2"));
PS.setDouble(4, cold.get("cW2"));
PS.setDouble(5, cold.get("cE3"));
PS.setDouble(6, cold.get("cW3"));
PS.setDouble(7, cold.get("cE4"));
PS.setDouble(8, cold.get("cW4"));
COUNT+=PS.executeUpdate();
Q="UPDATE hot SET hE1=?,hW1=?,hE2=?,hW2=?,hE3=?,hW3=?,hE4=?,hW4=? WHERE dataid='"+ID+"'";
PS= this.con.prepareStatement(Q);
PS.setDouble(1, hot.get("hE1"));
PS.setDouble(2, hot.get("hW1"));
PS.setDouble(3, hot.get("hE2"));
PS.setDouble(4, hot.get("hW2"));
PS.setDouble(5, hot.get("hE3"));
PS.setDouble(6, hot.get("hW3"));
PS.setDouble(7, hot.get("hE4"));
PS.setDouble(8, hot.get("hW4"));
COUNT+=PS.executeUpdate();
Q="UPDATE usages SET E1=?,W1=?,E2=?,W2=?,E3=?,W3=?,E4=?,W4=? WHERE dataid='"+ID+"'";
PS= this.con.prepareStatement(Q);
PS.setDouble(1, USGs.get("E1"));
PS.setDouble(2, USGs.get("W1"));
PS.setDouble(3, USGs.get("E2"));
PS.setDouble(4, USGs.get("W2"));
PS.setDouble(5, USGs.get("E3"));
PS.setDouble(6, USGs.get("W3"));
PS.setDouble(7, USGs.get("E4"));
PS.setDouble(8, USGs.get("W4"));
COUNT+=PS.executeUpdate();
PS.close();
}finally{
return COUNT;
}
問題是什么?
謝謝
你應該檢查三點:
是否所有資源如PreparedStatement
, ResultSet
等在發出下一個語句之前都已關閉? 同時在最后正確關閉Connection
。
你的自動提交狀態是什么? 使用getAutoCommit()
在Connection
上檢查它。 如果為false,則必須手動發出Connection.commit()
。 這是一個因數據庫而異的設置,最常被錯誤省略。
檢查數據庫本身的設置:sqlite是一個嵌入式數據庫,有時它們具有奇怪的配置默認值,便於啟動或單元測試。 例如:“僅在內存中啟動”或“在啟動時截斷所有表”。 別笑,我見過那樣的東西! 檢查文檔。
您正在創建多個中間PreparedStatements並丟棄它們。 在移動到下一個查詢之前,您很可能需要關閉它們。
你在finally中有一個return語句,它吞沒了所有異常。 不要那樣做 。 如果你將回報移到最后,你會看到問題所在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.