Home:ALL Converter>What will be the PreparedStatements for the below PostgreSQL table?

What will be the PreparedStatements for the below PostgreSQL table?

Ask Time:2022-09-07T15:20:43         Author:lets_code

Json Formatter

I want to use JDBC for preparedStatement in Java for inserting data into a PostgreSQL database. I have the following DDL


CREATE TABLE distributors (
    did   DECIMAL(3)  DEFAULT NEXTVAL('serial_no'), 
    dname  VARCHAR(40) DEFAULT 'lusofilms'

I want to insert data into this table. Should the PreparedStatement be

INSERT INTO distributors (did,dname) VALUES (?,?);

And if so how do I do insertions in PreparedStatements for default values?

Author:lets_code,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/73631509/what-will-be-the-preparedstatements-for-the-below-postgresql-table
Raphaël Colantonio :

Solution 1\nINSERT INTO distributors (dname) VALUES (?);\n\nuse trigger. Reference document: https://www.postgresql.org/docs/current/sql-createtrigger.html\nSolution 2\nINSERT INTO distributors (did,dname) VALUES (nextval('serial_no'), ?);\n\nSee https://stackoverflow.com/a/21397740/3728901",