People, 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 id
key must be incremental. In java I did the following method to 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;
}
I put null
, because it is not important since the key will be incremental. But when I run, the eclipse returns me that the Primary key id
can not be null
.
Does anyone know what I do?