A self-increment column in PostgreSQL works from the default value, not a special value. Just omit it:
CREATE TABLE tbl_cliente (
codigo SERIAL,
nome TEXT
);
INSERT INTO tbl_cliente (nome) VALUES ('Diego');
Or use the keyword DEFAULT
instead of a value:
INSERT INTO tbl_cliente (codigo, nome) VALUES (DEFAULT, 'Diego');
Columns that do not appear in the INSERT
list are set to their default values. In the case of a column SERIAL
or BIGSERIAL
, this value is the next number in its sequence.
In Java it's the same as in SQL: it simply does not include any values for the column:
String sql = "INSERT INTO tbl_cliente (nome) VALUES (?)"
// ou
String sql = "INSERT INTO tbl_cliente (codigo, nome) VALUES (DEFAULT, ?)"
PreparedStatement pst = algo.prepareStatement(sql);
pst.setString(1, oNome);
More information is available in the PostgreSQL documentation: INSERT
, default values , SERIAL
.