How do I put autoincrement

Asked

Viewed 6,979 times

1

In the bank I can only enter if I add a code, but I want to be auto increment, in the bank I use postgre and declared the code as serial

String sql = "INSERT INTO tbl_cliente(codigo,nome, cpf, telefone, endereco, sexo, pagamento, modelo, ano, marca, custoconcerto) VALUES "
            + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    try {
        pst = conecta.prepareStatement(sql);
        pst.setInt(1,"Aqui é o autoincrement");
  • 1

    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?

  • The field must be to serial type for this.

  • 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 )

  • this is my table, in the insert I leave blank? of the code?

  • 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

  • Using postgree to insert I can, the problem is in java

  • 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

Show 2 more comments

2 answers

1


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.

0

Friend the problem is not the command but the table, to leave the auto_increment field it is necessary to create a sequence. As well as other DBMS.

It would look like this to create the table, and when you use the command you do not need to enter the code field.

CREATE TABLE tbl_cliente (codigo integer NOT NULL, "demais campos");
CREATE SEQUENCE seq_codigo
INCREMENT 1
MINVALUE 1
MAXVALUE 9999
START 1
CACHE 1;

ALTER TABLE tbl_cliente ALTER COLUMN codigo SET DEFAULT NEXTVAL("seq_codigo"::regclass);
  • The question says that the field is already auto-increment with SERIAL, then there is no need to create another sequence for a field that already has a.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.