vendredi 25 mars 2016

Execute Update doesn't update and give me database is locked

keep on giving me database is locked. This is a method when I feed my pet. so every time I click the food button it change the value of health and hunger needs to be update each time i execute the update method. but its not working

public boolean update(MyPet pet) throws SQLException { Connection c = null;

     try{


       c = Database.getConnection();
       Statement  statement = c.createStatement();
         String  sql = "UPDATE Player SET Player_Name = "+ pet.getPetName()+" "
                 +",Health = "+ pet.getHealth()+" "
                 + ",Hungry= "+pet.getHunger()+" "
                 + "WHERE ID = '" +pet.getID()+"';";

        statement.executeUpdate(sql);
        statement.close();
        c.commit();

        return true;

     }
 catch (SQLException ex) {
        System.err.println(ex.getMessage());
        ex.printStackTrace(System.out);
        return false;
    }

finally{
         try{
         c.close();}
         catch (SQLException ex) {

         }

    }

} }

public class Database {

public static final String DB_URL = "jdbc:sqlite:test.db";

public static Connection getConnection() {
    Connection connection = null;

    try {

        Class.forName("org.sqlite.JDBC");
        connection = DriverManager.getConnection(DB_URL);
        connection.setAutoCommit(false);
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }

    return connection;
}    

Aucun commentaire:

Enregistrer un commentaire