java - How to extract Object[] and get it into query using preparedstatement setObject()? -
this question has answer here:
- preparedstatement in clause alternatives? 25 answers
might silly question cant query running. code:
arraylist<attraction> getattraction(object[] obj) throws sqlexception { connection connection = facade.getconnection(); arraylist<attraction> attractions = new arraylist<>(); // check obj value system.out.println(java.util.arrays.tostring(obj)); preparedstatement ps = connection.preparestatement("select distinct attra.attractid,attractname,x,y,z,age,weight," + "height,duration,price,status attraction attra " + "inner join attrib_bridge ab on attra.attractid = ab.attractid " + "inner join attribute attri on ab.attributeid = attri.attributeid" + "where attri.attributename in ?"); ps.setstring(1, java.util.arrays.tostring(obj)); resultset rs = ps.executequery(); while(rs.next()){ // initialize attraction object attraction attraction = new attraction(); attraction.setattractionid(rs.getint(1)); attraction.setattractionname(rs.getstring(2)); attraction.setxlocation(rs.getint(3)); attraction.setylocation(rs.getint(4)); attraction.setzlocation(rs.getint(5)); attraction.setminage(rs.getint(6)); attraction.setmaxweight(rs.getint(7)); attraction.setminheight(rs.getint(8)); attraction.setrideduration(rs.getint(9)); attraction.setpriceride(rs.getint(10)); attraction.setstatus(rs.getstring(11)); // add new object to movies attractions.add(attraction); } return attractions; }
and error :
[gentle rides, thrill rides, transport rides] com.mysql.jdbc.exceptions.jdbc4.mysqlsyntaxerrorexception: have error in >your sql syntax; check manual corresponds mysql server version >for right syntax use near 'attri.attributename in '[gentle rides, thrill >rides, transport rides]'' @ line 1
at sun.reflect.nativeconstructoraccessorimpl.newinstance0(native method) @ sun.reflect.nativeconstructoraccessorimpl.newinstance(unknown source) @ sun.reflect.delegatingconstructoraccessorimpl.newinstance(unknown source) @ java.lang.reflect.constructor.newinstance(unknown source) @ com.mysql.jdbc.util.handlenewinstance(util.java:411) @ com.mysql.jdbc.util.getinstance(util.java:386) @ com.mysql.jdbc.sqlerror.createsqlexception(sqlerror.java:1052) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:3609) @ com.mysql.jdbc.mysqlio.checkerrorpacket(mysqlio.java:3541) @ com.mysql.jdbc.mysqlio.sendcommand(mysqlio.java:2002) @ com.mysql.jdbc.mysqlio.sqlquerydirect(mysqlio.java:2163) @ com.mysql.jdbc.connectionimpl.execsql(connectionimpl.java:2624) @ com.mysql.jdbc.preparedstatement.executeinternal(preparedstatement.java:2127) @ com.mysql.jdbc.preparedstatement.executequery(preparedstatement.java:2293) @ com.themeparkg02.rs.control.manager.manageattraction.getattraction(manageattraction.java:123) @ com.themeparkg02.rs.control.manager.facade.getattraction(facade.java:85) @ com.themeparkg02.rs.control.listener.viewattractionlistlistener.run(viewattractionlistlistener.java:32)
i'm dizzy right now. values out query not working. '[' , ']' blocking goal. input guys?
thanks in advance.
note: missed issue of in
clause, answer technically correct, first error in sql missing space. link provided miljen mikic explains how fix in
clause issue.
your error says:
mysqlsyntaxerrorexception: have error in >your sql syntax
you missing space before where
.
updated
for improved readability, prefer writing this:
string sql = "select distinct attra.attractid, attractname, x, y, z, age" + ", weight, height, duration, price, status" + " attraction attra" + " join attrib_bridge ab on attra.attractid = ab.attractid" + " join attribute attri on ab.attributeid = attri.attributeid" + " attri.attributename in ?"; try (preparedstatement ps = connection.preparestatement(sql)) { ps.setstring(1, arrays.tostring(obj)); try (resultset rs = ps.executequery()) { while (rs.next()) { // process row here } } }
Comments
Post a Comment