Problem in performing a Java language INSERT

0
Hello everyone, I have a problem that I think should be very easy but as I am new to it, it is not rs, I have an insert to perform in a class called ClienteDAO follow the code below:

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) 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.executeUpdate();

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

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

Please note that I did not put:

pstmt.executeQuery();

and instead put

psmt.executeUpdate();

Because when I put ExecuteQuery it returns the following error:

  

"Java.sql.SQLException: query does not return result"

But when I put ExecuteUpdate it returns another error which is:

  

"Java.sql.SQLException: client.id_client may not be NULL"

ie update would not be correct, right? and it became clear to me, now why executeQuery does not work?

And so I am, what to do? rs, the connection to the bank appears to be correct, the way I wrote the INSERT also appears to be correct, and since I am new to the area I do not know what to do.

I'll leave the client table creation script:

CREATE TABLE cliente (
id_cliente integer increment(1,1) PRIMARY KEY NOT NULL,
cliente_cpf varchar(20),
cliente_rg varchar(20),
cliente_nome varchar(60),
end_rua varchar(40),
end_numero varchar(10),
end_bairro varchar(50),
end_cep varchar(20),
end_estado varchar(40),
end_cidade varchar(40)
)

Detail: I tried to replace the increment (1,1) with identity (1,1) but it did not solve, the database I'm using is SQLite.

    
asked by anonymous 23.12.2016 / 20:03

1 answer

2

The executeUpdate() is correct. Java displays the error but apparently the problem is SQL rather than Java.

You tried to insert a record without informing id_perfil , which by default SQL is trying to insert as null. You probably failed to declare this field as AUTO INCREMENT .

A ALTER TABLE (or change the table interactively in the database viewer of your choice) should resolve.

AUTO INCREMENT assigns a numeric value to the field in question automatically, following an increasing order of values. Then you do not have to be forced to include this field in your% of%.

    
23.12.2016 / 20:10