I want to use JDBC for preparedStatement
in Java for inserting data into a PostgreSQL database. I have the following DDL
CREATE SEQUENCE serial_no;
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?
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",
2022-09-07T07:27:14