2

I am trying to insert data in a table with a PreparedStatement in Java and postgres JDBC. I want to insert into column startdate and column enddate values with a condition that roomnumber is given (?);

PreparedStatement stm = conn.prepareStatement( "INSERT INTO room(startdate,enddate) values(?,?) WHERE roomnumber = ?"))

It gives me an error at WHERE. What is the correct syntax. Many thanks

0

3 Answers 3

6

Use UPDATE to change existing rows, INSERT to create new rows. There are a number of examples in the documentation: https://www.postgresql.org/docs/9.5/static/sql-update.html

E.g.: UPDATE room SET startdate = ?, enddate = ? WHERE roomnumber = ?

Sign up to request clarification or add additional context in comments.

Comments

0

You can use something like that:

insert into room(startdate, enddate) select '2020-01-01'::date, '2020-02-11'::date where (select roomnumber = ? from roomnumber_table);

Also check out this: https://www.postgresql.org/docs/12/queries-with.html

Comments

0

Use Insert Into Select and Not Insert Into Values

INSERT INTO table1(firstname, lastname)
    select 'Sally', 'Brown'
    Where exists (Select 1 from table2 where field1=val1 )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.