0

I'm heaving a problem with SQLite and Java.

I don't know if it's because I'm leaving some open connection (or PreparedStatement/ResultSet) or if it's because I'm handling the Connection in a wrong way but this code is giving me a hard time for the past 2 days.

Im trying to get all clientes registered with the getClientes() method, thats when I got the "database file locked" error.

I've created the above class to create the connection to the SQLite DB:

public class ConnectionFactory
{
    public Connection getConnection()
    {
        Connection c = null;

        try
        {
            System.out.println("Conectando ao banco de dados...");
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:src/lp2/database/rivieraresort.db");
            System.out.println("Conectado com sucesso!");
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return c;
    }
}

And the following one to handle the DB itself

public class RivieraDB
{   
    public Quarto buscarQuarto(int contratoID) throws Exception
    {
        Quarto quartoEncontrado = null;

        Connection c = new ConnectionFactory().getConnection();
        c = new ConnectionFactory().getConnection();
        String query = "SELECT * FROM quartos WHERE contrato_id = ?";
        PreparedStatement pstmt = c.prepareStatement(query);

        pstmt.setInt(1, contratoID);

        ResultSet rs = pstmt.executeQuery();

        while(rs.next())
        {
            String tipo = rs.getString("tipo");
            int camasExtras = rs.getInt("camas_extras");
            double preco = rs.getDouble("preco");

            quartoEncontrado = new Quarto(tipo, camasExtras, preco);
        }

        rs.close();
        pstmt.close();
        c.close();
        return quartoEncontrado;
    }

    public Contrato buscarContrato(int contratoID) throws Exception
    {
        Contrato contratoEncontrado = null;

        Connection c = new ConnectionFactory().getConnection();
        Quarto quartoAssociado = buscarQuarto(contratoID);
        c = new ConnectionFactory().getConnection();
        String query = "SELECT * FROM contratos WHERE id = ?";
        PreparedStatement pstmt = c.prepareStatement(query);

        pstmt.setInt(1, contratoID);

        ResultSet rs = pstmt.executeQuery();

        while(rs.next())
        {
            String inicioContrato = rs.getString("data_inicio");
            int tempo = rs.getInt("tempo");

            contratoEncontrado = new Contrato(quartoAssociado, inicioContrato, tempo);
        }

        rs.close();
        pstmt.close();
        c.close();
        return contratoEncontrado;
    }

    public ArrayList<Cliente> getClientes() throws Exception
    {
        ArrayList<Cliente> clientesEncontrados = new ArrayList<Cliente>();
        Cliente clienteEncontrado = null;
        Connection c = new ConnectionFactory().getConnection();
        String query = "SELECT * FROM clientes";
        PreparedStatement pstmt = c.prepareStatement(query);

        ResultSet rs = pstmt.executeQuery();

        while(rs.next())
        {
            String nome = rs.getString("nome");
            String cpf = rs.getString("cpf");
            String nascimento = rs.getString("nascimento");
            String endereco = rs.getString("endereco");
            String cartao = rs.getString("cartao");
            int contratoID = rs.getInt("contrato_id");
            boolean statusHospedagem = rs.getBoolean("status_hospedagem");

            clienteEncontrado = new Cliente(nome, cpf, nascimento, endereco);
            clienteEncontrado.setCartao(cartao);
            clienteEncontrado.setContrato(buscarContrato(contratoID));
            clienteEncontrado.setEstaHospedado(statusHospedagem);

            clientesEncontrados.add(clienteEncontrado);
        }

        rs.close();
        pstmt.close();
        c.close();
        return clientesEncontrados;
    }
}

The "[SQLITE_BUSY] The database file is locked (database is locked)" error only occurs when I try to use the getClientes() method.

Any ideas?

6
  • Please summarise or us: 1) what is it you are trying to do with the code 2) what you are seeing instead? 3) what triggers that? 4) what happened when you googled for the error message 5) any other research or things you tried to get it working. Commented Feb 9, 2015 at 3:45
  • 1) trying to get all clients registered in a table with the getClientes() method 2) when i try to use the getClientes() method i got "database file is locked" error 3) thats why i came here 4) havent found a similar case Commented Feb 9, 2015 at 4:01
  • These things you've just told me should be part of your original question - can you please edit your question and add them there? (in case people that might scan your question think you just have been incomplete and don't read the comments) Commented Feb 9, 2015 at 4:02
  • Also - please strip out any code that is not directly related to the question (so we don't have to scan through lots of unrelated code). here's a good article on how to get he best people to help you on stack overflow: stackoverflow.com/help/how-to-ask Commented Feb 9, 2015 at 4:03
  • Alright, thanks for the tips. Commented Feb 9, 2015 at 4:09

1 Answer 1

0

I have to close any connections before opening another one.

An answer to this (SQLITE Database is locked in java (IDE NetBeans)) question made me solve my problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.