2

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

Ariyan
  • 14,760
  • 31
  • 112
  • 175

3 Answers3

3

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.

A.H.
  • 63,967
  • 15
  • 92
  • 126
  • 1
    yes after jtahlborn's answer I closed all `PreparedStatement`s but it had no effect! `AutoCommit` is `true` !! the strange thing is update is done and it is there while program is not closed and it is lost when i close/re-open program!!!! – Ariyan Sep 20 '11 at 19:06
  • 1
    Hi and thanks for help; closing just after update fixed the problem. but with this technique i need to do lots of open/close on the database. previously i put close statement in `finalize` method but it seems java doesn't call finalize of that object when it is closing! – Ariyan Sep 20 '11 at 19:55
1

you are creating multiple intermediate PreparedStatements and discarding them. you most likely need to close them before moving to the next query.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
1

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.

Community
  • 1
  • 1
mkadunc
  • 696
  • 6
  • 12
  • Hi , Thanks ;I replaced `return` and now exceptions says `database locked` while my application is the only program that uses DB!? – Ariyan Sep 20 '11 at 18:37
  • 1
    I closed IDE and re-opened it and now there is no exception! but problem is still there!! – Ariyan Sep 20 '11 at 18:44