简体   繁体   English

JDBC不在sqlite数据库中存储更新

[英]JDBC doesn't store update in sqlite database

I wrote an application that inserts some data in a SQlite database using JDBC and have the ability of Read/Update that data. 我编写了一个应用程序,它使用JDBC在SQlite数据库中插入一些数据,并具有读取/更新该数据的能力。
The problem is when i change data by application it updates data and while program is open reading those data gives new (updated) data. 问题是当我通过应用程序更改数据时它会更新数据,而当程序打开时,读取这些数据会产生新的(更新的)数据。
But when I exit the program and re-run it all of those changes are lost and data is reset to when program opened for the first time! 但是当我退出程序并重新运行它时,所有这些更改都会丢失,并且数据会在第一次打开程序时重置为!
I connect like this: 我这样连接:

Class.forName("org.sqlite.JDBC");
con=DriverManager.getConnection("jdbc:sqlite:archive.sqlite");

and then update four tables like this: 然后像这样更新四个表:

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;
}

What is the problem? 问题是什么?
Thanks 谢谢

There are three points you should check: 你应该检查三点:

  • are all resources like PreparedStatement , ResultSet etc. closed before issuing the next statements? 是否所有资源如PreparedStatementResultSet等在发出下一个语句之前都已关闭? Also close the Connection properly at the end. 同时在最后正确关闭Connection

  • What is your autocommit status? 你的自动提交状态是什么? Check it on Connection with getAutoCommit() . 使用getAutoCommit()Connection上检查它。 If it is false, you must issue a Connection.commit() by hand. 如果为false,则必须手动发出Connection.commit() This is a setting which varies between databases is is most often omitted by error. 这是一个因数据库而异的设置,最常被错误省略。

  • Check the setup of the database itself: sqlite is an embedded database and sometimes these have strange configuration defaults for easy startup or unit-testing. 检查数据库本身的设置:sqlite是一个嵌入式数据库,有时它们具有奇怪的配置默认值,便于启动或单元测试。 For example: "start me in memory only" or "truncate all table at startup". 例如:“仅在内存中启动”或“在启动时截断所有表”。 Don't laugh, I have seen things like that! 别笑,我见过那样的东西! Check the Documentation for that. 检查文档。

you are creating multiple intermediate PreparedStatements and discarding them. 您正在创建多个中间PreparedStatements并丢弃它们。 you most likely need to close them before moving to the next query. 在移动到下一个查询之前,您很可能需要关闭它们。

You have a return statement in finally , which swallows all the exceptions. 你在finally中有一个return语句,它吞没了所有异常。 Don't do that . 不要那样做 If you move the return outside of finally, you'll see where the problem is. 如果你将回报移到最后,你会看到问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM