user10755408
user10755408

Reputation:

Database is locked in SQLite [Java]

updated

try
        {
            sqlite.setDbPath(dbPath);
            con = sqlite.connect();
            if(!con.isClosed()) 
            {
                String query="SELECT Username,Password FROM Apps WHERE Username ='"+username+"'"; // and Password='"+password+"'";
                ResultSet rs = con.createStatement().executeQuery(query);
                while(rs.next())
                {
                        if(rs.getString("Username").equals(username))//confronto se Username è gia esistente
                        {
                            trovato = true;
                            risultato = "gia' presente";
                        }
                }

                if(trovato==false) {
                    createUsers(username,password,name,surname,email,appname,ip,authorized,token);
                    risultato="inserito";
                }

                if(con!=null)
                {
                    con.close();
                }
                if(sqlite != null)
                {
                    sqlite.close();
                }
                if(rs != null)
                {
                    rs.close();
                }
            }

In try catch block I've open connection with database embedded but in first time i don't close all connection.. With if control the program close all open connection and works well

Upvotes: 1

Views: 181

Answers (2)

user10755408
user10755408

Reputation:

Resolved

I do errors with open and close connection in Main.. With SQLite the connection you have to open and close everytime it's carried out a query of all type(Insert, Delete,Update ecc..)

Upvotes: 0

Joakim Danielson
Joakim Danielson

Reputation: 52013

Update 2

Here is an abbreviated version using try-with-resource instead. Code is simpler and shorter

public String RegisterUser(... ) {
    boolean trovato = false;
    int authorized=0;

    SqliteConnection sqlite = new SqliteConnection();
    String dbPath="C:/Users/l.pellegrini/eclipse-workspace/ClayAPI_dbembedded/claydb.db";
    String query="SELECT Username,Password FROM Apps WHERE Username ='"+username+"' and Password='"+password+"'";

    try (java.sql.Connection con = sqlite.connect();
         Statement statement = con.createStatement();
         Statement updStatement = con.createStatement();
         ) {
        ResultSet rsActiveServices = con.createStatement().executeQuery(query);
        // handle result set as before
    } catch(SQLException e) {
        e.printStackTrace();
        System.out.println("errore" + e);
    }
    if(trovato==false) {
        createUser(username, password, name, surname, appname, email, ip)
    }
    return "username: " + username;
}


private createUser(String username, String password, String name, String surname, String appname, String email, String ip {
    String query1="INSERT INTO Apps (Username,Password,Name,Surname,Email,AppName,Ip,Authorized,Token) VALUES ('" + username + "'," + "'" +password +"','" + name + "','" + surname + "','" +email + "','" + appname + "','"+ip+"','"+authorized+"','"+token+"')";         
    SqliteConnection sqlite = new SqliteConnection();
    String dbPath="C:/Users/l.pellegrini/eclipse-workspace/ClayAPI_dbembedded/claydb.db";

    try (java.sql.Connection con = sqlite.connect();
         Statement statement = con.createStatement();) {
        updStatement.executeUpdate(query1);
    } catch(SQLException e) {
        e.printStackTrace();
        System.out.println("errore" + e);
    }
}

It could very well be that your prepared statement isn't properly closed. Change

ResultSet rsActiveServices = con.createStatement().executeQuery(query);

to

statement = con.createStatement();
ResultSet rsActiveServices = statement.executeQuery(query);

where statement is declared before try

java.sql.Connection con = null;
Statement statement = null;

and then close it in your finally clause

finally
{
    try
    {
        statement.close();
        con.close();
        sqlite.close();
    }

Update 1

I just noticed that your are trying to close your objects twice which is wrong, remove the first set of close calls and only close within finally {} at the end.

Upvotes: 1

Related Questions