Database Locked Sqlite/Java error

Asked

Viewed 356 times

4

I have the following problem: in an Insert that I am trying to accomplish, I don’t know if it is an error in the database or something in the java programming, but every time I try to perform an input of the client table returns me the error:

java.sql.Sqlexception: database locked

Until then everything was ok in Insert but I had to redo the bank from there started the problem I reviewed the code however it seems to be all correct, follows below Insert code:

public class clienteDAO {   
    public void Create(cliente c){    
       Connection conn = javaConnect.ConnectDb();

       PreparedStatement pstmt = null;

        try {
            pstmt = conn.prepareStatement("INSERT INTO cliente (cliente_nome,cliente_rg,cliente_cpf,end_rua,end_numero,end_bairro,end_cidade,end_estado,end_cep,telefone_celular, telefone_residencial, telefone_extra, cliente_email) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
            pstmt.setString(1, c.getNome());
            pstmt.setString(2, c.getRg());
            pstmt.setString(3, c.getCpf());
            pstmt.setString(4, c.getRua());
            pstmt.setString(5, c.getNumero());
            pstmt.setString(6, c.getBairro());
            pstmt.setString(7, c.getCidade());
            pstmt.setString(8, c.getEstado());
            pstmt.setString(9, c.getCep());
            pstmt.setString(10, c.getTelefone1());
            pstmt.setString(11, c.getTelefone2());
            pstmt.setString(12, c.getTelefone3());
            pstmt.setString(13, c.getEmail());


            pstmt.executeUpdate();


            JOptionPane.showMessageDialog(null, "Cadastro Realizado!");

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"Erro ao salvar: " + ex);
        }
    }

I tried to find the error tbm in creating the table but also could not identify any error, follows code I used to create the table:

CREATE TABLE cliente (
id_cliente integer PRIMARY KEY AUTOINCREMENT NOT NULL,
cliente_cpf varchar(20),
cliente_nome varchar(60),
end_rua varchar(40),
cliente_rg varchar(20),
end_bairro varchar(50),
end_estado varchar(40),
end_cidade varchar(40),
end_cep varchar(20),
end_numero varchar(10),
telefone_contato1 varchar(12),
telefone_contato2 varchar(12),
telefone_contato3 varchar(12),
cliente_email varchar(30)
)

Well, now I have no idea if the error is in the database or in Java programming.

I’ve reviewed the get/set codes but they appear to be correct as well.

  • 1

    Most likely is dropping it open somewhere else that has exclusivity.

  • You refer to connection to the bank ?

  • That’s right.....

1 answer

3


Are you opening the Connection and the PreparedStatement but you’re not closing them properly. If they are dropped and forgotten open, they are likely to end up causing errors like the one you are experiencing.

The solution is use the Try-with-Resources.

Here’s your revised code:

public class ClienteDAO {
    private static final String SQL_INSERT =
            "INSERT INTO cliente (cliente_nome,cliente_rg,cliente_cpf,end_rua,end_numero,end_bairro,end_cidade,end_estado,end_cep,telefone_celular, telefone_residencial, telefone_extra, cliente_email) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";

    public void create(Cliente c) {
       try (
           Connection conn = JavaConnect.connectDb();
           PreparedStatement pstmt = conn.prepareStatement(SQL_INSERT);
       ) {
            pstmt.setString(1, c.getNome());
            pstmt.setString(2, c.getRg());
            pstmt.setString(3, c.getCpf());
            pstmt.setString(4, c.getRua());
            pstmt.setString(5, c.getNumero());
            pstmt.setString(6, c.getBairro());
            pstmt.setString(7, c.getCidade());
            pstmt.setString(8, c.getEstado());
            pstmt.setString(9, c.getCep());
            pstmt.setString(10, c.getTelefone1());
            pstmt.setString(11, c.getTelefone2());
            pstmt.setString(12, c.getTelefone3());
            pstmt.setString(13, c.getEmail());

            pstmt.executeUpdate();

            JOptionPane.showMessageDialog(null, "Cadastro Realizado!");    
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro ao salvar: " + ex);
        }
    }

Ah, and note that I changed the name of the method to create and the name of the classes for Cliente and ClienteDAO to leave him according to the conventions of the Java language. Also note that for this reason, I used JavaConnect.connectDb(); instead of javaConnect.ConnectDb();.

  • I really understood the situation and I realized that the error is in my login where I do not use this Resource, thank you, served of great help, I will try to learn to use the Try-with-Resources.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.