Reputation: 3347
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
Reputation: 4204
\n
and ;
prepared statement
to avoid injectionsample 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
Reputation: 3484
For INSERT, UPDATE, DELETE queries you should use Statement.executeUpdate
, for SELECT - Statement.executeQuery
, javadoc clearly states it.
Upvotes: 2