I am running a SELECT
query using JDBC which itself has a few subqueries. One of these subqueries returns a column of data which I then process as an ArrayList
. Due to error handling, I prepare the subquery and run it separately then produce the java String in the form "(item_1, item_2, ... , item_n)"
.
Note the tuple elements are INTEGERS.
My overarching query is in the form of a PreparedStatement
where I look to populate the ?
using my subquery result.
String subqueryResult = "(1, 2, 3)";
// ...
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM table WHERE columnName in ?");
stmt.setString(subqueryResult);
I get the SQL 1064 error which then states that my query looks like WHERE columnName in ''(1, 2, 3)'
.
I understand that the literal quotes are unbalanced, but even if they are balanced we cannot perform an IN
clause on a string.
How would I go about safely populating the PreparedStatement
so that the resulting query looks like SELECT * FROM table WHERE columnName in (1, 2, 3)
?