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.