Error DataBase Locked Sqlite / Java

4

I have the following problem: in an insert that I am trying to perform, I do not know if it is an error in the database or something in the programming in java, however every time I try to perform an insert of the client table I get the error: p>

  

java.sql.SQLException: database locked

Until then everything was ok in the insert but I had to redo the database from there started the problem I reviewed the code but everything seems to be correct, follow 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 the creation of the table but also could not identify any error, there is 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 bank or programming in Java.

I also reviewed the get / Set codes but they look correct too.

    
asked by anonymous 24.12.2016 / 18:24

1 answer

3

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

The solution is try-to-resources .

>

Here is your reviewed 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);
        }
    }

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

    
24.12.2016 / 20:51