Search code examples
javasqloracle-databasejdbcwhere-in

Java passing values into IN in sql WHERE clause


I have a sql query like

(sqlQuery)
SELECT *
FROM table1
WHERE to_char(id) in (?)

Then in my prepared statement

preparedStatement ps = new preparedStatemend(sqlQuery)
ps.setString(1,param);

where my param is a string chain looking like param = '12','34', '444'

Now, I thought that when I execute it then everything will be ok, but it's not and I get and error like ORA 01460 uninmplemented or unreasonable conversion.

So, how can I pass my parameter consists of a few valuse into the IN clause in sql query?


Solution

  • You will have to specify a ? for each possible values of the IN clause.

    For example if you have 4 options possible in the IN clause, this :

    to_char(id) in (?)
    

    should so be :

    to_char(id) in (?, ?, ?, ?) 
    

    And you should also set each parameter by incrementing successively the index of the param to set :

    So this :

    ps.setString(1,param);
    

    should be :

    int index = 1;
    for( String s : inValues ) {
       ps.setString(index++, s); 
    }