0
Hello community people, I’m with a problem that I think should be very easy but as I am beginner to me is not rs, I have a Insert to realize that stays in a class called ClienteDAO
follows 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);
}
}}
Notice I didn’t put:
pstmt.executeQuery();
and instead I put
psmt.executeUpdate();
'cause when I put ExecuteQuery
returns me the following error:
"Java.sql.Sqlexception: query does not Return result"
but when I put ExecuteUpdate
it returns another error that is:
"Java.sql.Sqlexception: client.id_client may not be NULL"
namely the update would not be correct, right ? and it became clear to me, now why executeQuery
not work ?
And then I stay, what to do ? rs,the connection with the bank seems to be correct, the way I wrote the INSERT also seems to be correct, and as I am new in the area I don’t 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) by Identity(1,1) but did not solve, the bank I am using is Sqlite.
Read that question and your answers.
– Victor Stafusa