0

I have a Java bean that has a field like below,

List<String> address = new ArrayList<>();
address.add("Pearl");
address.add("Granby");

I also have a table in MySQL database name building and a column name street of its own. when I use JDBC to connect to MySQL database, how can I use this ArrayList to query in MySQL database like SELECT * FROM building WHERE street IN ("Pearl", "Granby"). Or have any better query to execute this connect. Forgive me for my poor language.

I tried to connect directly and get error like:

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'building.address IN [Pearl, Grandby]'
pmelo
  • 15
  • 5
  • Sorry, but I am not quite sure what you are trying to do. Do you need **all** of the buildings that are on **any** of the streets in your `List`? – S-Flavius Mar 23 '22 at 08:44
  • [This](https://stackoverflow.com/questions/3107044/preparedstatement-with-list-of-parameters-in-a-in-clause) should help. – S-Flavius Mar 23 '22 at 08:46
  • Yes, that's my target. I need to get all that is on any of the streets. – pmelo Mar 23 '22 at 09:05

1 Answers1

0
String result = address.stream().collect(Collectors.joining("','", "'", "'"));
aymcg31
  • 599
  • 3
  • 12