doflamingo
doflamingo

Reputation: 77

SQL query : Wrong syntax

I'm tring to write a query but I obtain a syntax error. I know that this error is in the query's syntax. This is the query

ResultSet set=statement.executeQuery("Select * from Ombrellone where PosizioneX='"+c.getX()+"',PosizioneY='"+c.getY()+"'" );

Anyone can help me?

Upvotes: 0

Views: 145

Answers (2)

SparkOn
SparkOn

Reputation: 8946

Though the problem in your case was basically because you used comma on your SQL query which is wrong you can use AND or OR for condition fulfillment when using WHERE clause but also I would suggest you to use PreparedStatement over Statement.

String query = "Select * from Ombrellone where PosizioneX = ? and PosizioneY = ?"
PreparedStatement statement = conn.prepareStatement(query);
statement.setString(1,c.getX());
statement.setString(2,c.getY());
ResultSet resultSet = statement.executeQuery();

Refer difference between statement and preparedstatement

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

If you want to have multiple conditions on select, you must use AND, not comma.

ResultSet set=statement.executeQuery("Select * from Ombrellone where PosizioneX='"+c.getX()+"' and PosizioneY='"+c.getY()+"'" );

Side note : Avoid using String concatination with query parameters. They causes SQL injections and try using PreparedStatement.

Upvotes: 5

Related Questions