user1935987
user1935987

Reputation: 3347

postgresql ERROR: missing FROM-clause entry for table in JAVA query execution

i read the same kind issue here, everywhere there is some problems with query.

I facing this error ERROR: missing FROM-clause entry for table in query, which is executing if i execute it manually, not in JAVA back-end code.

this is manual query, which is works well when i execute it in DBMS:

INSERT INTO goods (condo_id, item_title, item_descr, item_email, item_phone, item_price)
VALUES (3, 'Title', 'bfDEscr', '[email protected]', '54654654654', '4545');

and this is statement in JAVA:

ResultSet rs = st.executeQuery("INSERT INTO goods (condo_id, item_title, item_descr, item_email, item_phone, item_price) VALUES\n" +
                "(item.condo_id, item.title, item.descr, item.email, item.phone, item.price);");

which is doesn't work and on which after execution i got exception with error.

This is Item class. I checked, in JAVA back-end, all values is received well and existed.

public class Item {
    public int condo_id;
    public String title;
    public String descr;
    public String phone;
    public String email;
    public int price;
    }

Upvotes: 0

Views: 1075

Answers (2)

Paizo
Paizo

Reputation: 4204

  1. your query in the java code contains some unwanted characters such as \n and ;
  2. you are not passing any parameter
  3. I would suggest you to use prepared statement to avoid injection

sample based on your code:

Connection con = ...;
PreparedStatement statement = con.prepareStatement("INSERT INTO goods (condo_id, item_title, item_descr, item_email, item_phone, item_price) VALUES ( ?, ?, ?, ?, ?, ?)");
statement.setLong(1, item.condo_id );
statement.setString(2, item.item_title);
statement.setsetString(3, item.item_descr );
//...all other missing bindings
statement.execute();

of course you need to have an instance to item to retrieve the values. I suggest as best practice to define as private the fields in your Item class and access them using getters/setters

Upvotes: 1

user3707125
user3707125

Reputation: 3484

For INSERT, UPDATE, DELETE queries you should use Statement.executeUpdate, for SELECT - Statement.executeQuery, javadoc clearly states it.

Upvotes: 2

Related Questions