Blog : How to Set IN Clause in JDBC?
May28
Issue: Suppose you have a SQL query like, select * from studebt where student_id in(‘A’,'B’,'C’) ; and value in IN clause may change dynamicallly. How can you set this using JDBC setParameter().. Solution 1 PreparedStatement student = con.prepareStatement(“select * from studebt where student_id in(?,?,?) “); student.setStings(1, “A”); student.setStings(2, “B”); student.setStings(3, “C”); Cons: Number of Data in IN clause may change dynamically, For example in next run you might need 4 student ids in your IN clause If you set as below, will it work PreparedStatement student = con.prepareStatement(“select * from studebt where student_id in(?) “); student.setStings(1, “‘A’,'B’,'C’”); Answer ; No , It will not Its a problem with IN clasue in JDBC …When you code as given above ? will be replaced by ‘<Data yo are giving>’ and it will result as select * from studebt where student_id in(”A’,'B’,'C”)…Which will not give actual result I was getting rid of this issue by just replacing IN clause query dynamically with out using setParameter method. String query = “select * from studebt where student_id in(IN_CLAUSE)”; query.replace(“IN_CLAUSE”, data); ..then execute the query Can you think of some better way of doing it? |
|
|
|
Join Indian Community is USA |
|
Posted in Software /
Software category on May 28 2010, 08:56 AM 1386 Views, 0 Comments, 1 Appreciations, Overall rating: ![]() Tags: Java, Java 1.5, JDBC, Database Post a comment | Appreciate this post | Report abuse | |
Comments |
