1
Guys, I’m doing a web service with Java + Postgres. I set my table in Postgres as follows:
CREATE TABLE usuario
(
id serial NOT NULL,
nome character varying(40),
idade integer,
CONSTRAINT usuario_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE usuario
OWNER TO postgres;
The key id
need to be incremental. In java I did the following method for insert:
public boolean inserirUsuario(Usuario usuario){
try {
Connection conn= ConectaPgAdmin.obtemConexao();
String queryInserir = "INSERT INTO usuario VALUES**(null,?,?)**";
PreparedStatement ppStm= conn.prepareStatement(queryInserir);
//ppStm.setInt(1, usuario.getId());
ppStm.setString(1, usuario.getNome());
ppStm.setInt(2, usuario.getIdade());
ppStm.executeUpdate();
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
Placed null
, because it is not important since the key will be incremental. However when I run, the lock returns to me that the Primary key id
can’t be null
.
Someone knows what I do?
Thank you! I followed your tip and it really worked! Thank you very much!
– Paula Camargo