An auto-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 list of INSERT
take their default values. In the case of a column SERIAL
or BIGSERIAL
, this value is the next number in its sequence.
Java is equal to SQL: simply do not include any value 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);
There is more information in the Postgresql documentation (in English): INSERT
, default values, SERIAL
.
Young man, this has nothing to do with Java, nor with Gui. It is something that is wrong in the database, in the creation of the column. What database are you using? How did you create this table?
– Jéf Bueno
The field must be to serial type for this.
– rray
CREATE TABLE tbl_client ( SERIAL code, name text, Cpf text, phone text, address text, sex text, payment integer, model text, year integer, brand text, custoconcerto float )
– Diego Noceli
this is my table, in the insert I leave blank? of the code?
– Diego Noceli
I believe that the "serial" key is needed in the case, not "msndar" in the Insert. http://www.tutorialspoint.com/postgresql/postgresql_using_autoincrement.htm
– Motta
Using postgree to insert I can, the problem is in java
– Diego Noceli
it asks me to enter a digit for pst.setInt(1,"Here is the autoincrement");, and if I do not put this line, it warns you that ta talking this 1 column
– Diego Noceli