passing object[] to sql query

In my application i have list of category ids.Now i want to get products from database based on product category which will be contains in that list and also product name like some "XYZ".How can achieve this by using IN clause. Is anyone have any idea i tried this by passing way but i get empty list

My Code is :

Object[] cids=final_cats.toArray();
String cds=final_cats.toString();
String query="select fpd from FTextProductDetails ftpd inner join ftpd.fProductDetails fpd WHERE fpd.categoryId in( "+cds+" )and ftpd.productName like ?" ;
Object[] values=new Object[]{'%'+productName+'%'};
List<Object[]> list=HibernatePaginationImpl.findProductsByName(query, values,pageSizeLimit , pageNumber,this.baseDao);

here final_cats have all categories.

Answers


Lets take final_cats to be a List

 int locSize = final_cats.size();
 StringBuilder builder = new StringBuilder();
 Object[] args = new Object[locSize];               
 for( int i = 0; i < locSize; i++ ) {
      builder.append(" ?, " );
      args[i]= final_cats.get(i);
 }
....
String queryString = StringUtils.substringBeforeLast(builder.toString(), ",");      
String query="select fpd from FTextProductDetails ftpd inner join ftpd.fProductDetails fpd WHERE fpd.categoryId in("+ builder.toString() +")and ftpd.productName like ?" ;

Hope this logic helps..


I got solution to my problem.

Here's the code :

Object[] cids=final_cats.toArray();
String cds="";
for(Object o:cids) {

        cds=cds+o+",";
}

cds=cds.substring(0, cds.length()-1);

String query="select fpd from FTextProductDetails ftpd inner join ftpd.fProductDetails fpd WHERE fpd.categoryId in( "+cds+")and ftpd.productName like ?" ;
Object[] values=new Object[]{'%'+productName+'%'};
List<Object[]> list = HibernatePaginationImpl.findProductsByName(query, values,pageSizeLimit , pageNumber,this.baseDao);

Need Your Help

optimize a SQL query for a couchdb view

sql node.js view couchdb cradle

How to optimize this SQL query for a couchdb view ?

SSIS - ForEach Loop over multiple tables

sql sql-server ssis

Problem is this: I have 3 tables. The first table is basically a list of account numbers. Second table contains a list of departments within a company, and the last contains a list of enumerated bu...