Question

Excerpt from code

PreparedStatement preparedStatement =  connection.prepareStatement("SELECT * FROM sch.tab1 where col1 like lower ( 'ABZ' ) ");
preparedStatement.executeQuery();

The above code executes successfully.

But when i try to execute this

PreparedStatement preparedStatement =  connection.prepareStatement("SELECT * FROM sch.tab1 where col1 like lower ( ? ) ");
preparedStatement.setString ( myValue );
preparedStatement.executeQuery();

It throws an exception."STRING TO BE PREPARED CONTAINS INVALID USE OF PARAMETER MARKERS"

What could be the problem here?


Answer found, see the comments

Was it helpful?

Solution

I think Carlos is on to something. Try

SELECT * FROM sch.tab1 where col1 like lower ( '' + ? )

or whatever passes for string concatenation operator in your version of SQL. Forcing a string context might get you past the error. May require extra parentheses.

OTHER TIPS

I suspect the problem is that you can't apply functions directly to parameters. Is there any particular reason why you want the lower casing to be performed at the database rather than in your code? (I can think of some potential reasons, admittedly.) Unless you really need to do this, I'd just change the SQL to:

SELECT * FROM sch.tab1 where col1 like ?

and call toLower() in Java, preferably specifying the appropriate locale in which to perform the lower-casing.

For reference: I ran into the same problem while using the NORMALIZE_STRING function:

SELECT NORMALIZE_STRING(?, NFKD) FROM sysibm.sysdummy1

Error message:

THE DATA TYPE, LENGTH, OR VALUE OF ARGUMENT 1 OF NORMALIZE_STRING IS INVALID. SQLCODE=-171, SQLSTATE=42815, DRIVER=4.13.111

Using the following statement solved the problem (CONCAT). Thanks to Paul Chernoch!

SELECT search_normalize(NORMALIZE_STRING(? CONCAT G'', NFKD)) FROM sysibm.sysdummy1

Note the "G" prefix for Unicode compatibility.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top