0

The question is NOT answered in the linked one!

I want to feed an IN claubse with a List or Set of Strings:

private static final ItemPreparedStatementSetter<MyItem> PREPARED_SETTER = (item, ps) -> {
    ps.setObject(1, item.mylist); //being a List<String> or Set<String>
};

PreparedStatement ps = con.prepareStatement("SELECT * FROM mytable where somefield IN (?)");
PREPARED_SETTER.setValues(item, ps);
ps.addBatch();
ps.executeBatch();
    

Result:

Caused by: java.sql.SQLException: Invalid utf8 character string: 'ACED00'
    at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.readErrorPacket(AbstractQueryProtocol.java:1683)
    at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.readPacket(AbstractQueryProtocol.java:1545)
    at org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol.getResult(AbstractQueryProtocol.java:1508)
    at org.mariadb.jdbc.internal.protocol.AsyncMultiRead.call(AsyncMultiRead.java:132)
    ... 5 more

The invalid AC ED 00 hint means there is a problem with the object (item.mylist) during serialization.

Question: how can I properly pass a List or Set into the IN clause?

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • I suggest you indicate why you can't use the answer from the linked question if you want this re-opened. – BlueMonkMN Mar 17 '21 at 14:51
  • @BlueMonkMN because the linked question is about (and shows a solution for) `JdbcTemplate`, which is a totally different class than `PreparedStatement` in my question. Even if the answer to my question would be "*it's not possible with PreparedStatement*", even then this would be a valid answer, and in no case a duplicate. – membersound Mar 17 '21 at 15:43
  • The non-spring answer is you have to construct the query with a ? placeholder for each member of the set that IN uses. You can’t directly pass in a collection. – Nathan Hughes Mar 17 '21 at 17:01

0 Answers0