开发者

JDBC doesn't store update in sqlite database

开发者 https://www.devze.com 2023-04-06 10:08 出处:网络
I wrote an application that inserts some data in a SQlite database using JDBC and have the ability of Read/Update that data.

I wrote an application that inserts some data in a SQlite database using JDBC and have the ability of Read/Update that data.

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 p开发者_JS百科rogram 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? Also close the Connection properly at the end.

  • What is your autocommit status? Check it on Connection with getAutoCommit(). If it is false, you must issue a Connection.commit() by hand. 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. 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. 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. Don't do that. If you move the return outside of finally, you'll see where the problem is.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号