parameter validation in PreparedStatement
I am working on a java app that utilizes PreparedStatement.
SELECT FIELD1, FIELD2 FROM MYTABLE WHERE (FIELD1='firstFieldValue' OR FIELD1='' or 'firstFieldValue'='');
firstFieldValue is a parameter. And it is necessary to check that its value is empty in SQL.
I created a prepared statement with the following parametrized SQL:
SELECT FIELD1, FIELD2 FROM MYTABLE WHERE (FIELD1=? OR FIELD1='' or ?='');
and I set values as follows:
preparedS开发者_开发问答tatementInstance.setString(1, this.firstFieldValue);
preparedStatementInstance.setString(2, this.firstFieldValue);
This way does not work. Execution fails.
I think the prepared statement SQL is wrong.
How can I fix the SQL for prepared statement for this case?
Just check it in java.
if firstFieldValue.equals("")
then SELECT FIELD1, FIELD2 FROM MYTABLE
else
SELECT FIELD1, FIELD2 FROM MYTABLE WHERE (FIELD1=? OR FIELD1='');
preparedStatementInstance.setString(1, this.firstFieldValue);
there is no 'i think it is wrong' in executing sql's, either it is correct and it executes the query or the sql is wrong and you get an sqlexception
If it fails you should provide the forum with the stacktrace so we dont have to mindread.
what do you mean with 'empty'? empty as a empty string or as null in the db
精彩评论