Home > Net >  ArrayList in Java access Array of value in MySQL
ArrayList in Java access Array of value in MySQL

Time:03-23

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]'

CodePudding user response:

String result = address.stream().collect(Collectors.joining("','", "'", "'"));
  • Related