I try to get the product id from the first query and use the id for second query.
So I have something like:
public Collection getProducts(int category_id)
{
ArrayList al = new ArrayList() ;
PreparedStatement s = null;
PreparedStatement s2= null;
ResultSet rs = null;
ResultSet rs2= null;
int prod_id = 0;
try
{
conn = getDatabaseConnection() ;
s = conn.prepareStatement("SELECT product_id, product_name from product_categories where category_id = ?" );
s.setInt(1, category_id) ;
rs = s.executeQuery();
while(rs.next())
{
prod_id = rs.getString(1);
al.add(rs.getString(2));
/* If I comment out the below codes, I will get the result from the first statement */
s2 = conn.prepareStatement("select product_image_path from product_images where product_id = ? );
s2.setInt(1, prodId) ;
rs2 = s2.executeQuery();
al.add(rs2.getString(1));
/* above is to get image path in while loop for each product id */
} // end while loop
} // end try
Catch {
.....
}
return al;
}
But seems the second preparedStatement doens't work within while(rs.next() loop -- I will get nothing when I add the second preparedStatement in while loop -- If I comment it out, I will at least get product name, ids from the first result set.
Please advise how to make it work -- How to use the product_id from the first resultset to get the other information running the second query.
Thank you very much -- Please let me know if you are not clear of my question.
So I have something like:
public Collection getProducts(int category_id)
{
ArrayList al = new ArrayList() ;
PreparedStatement s = null;
PreparedStatement s2= null;
ResultSet rs = null;
ResultSet rs2= null;
int prod_id = 0;
try
{
conn = getDatabaseConnection() ;
s = conn.prepareStatement("SELECT product_id, product_name from product_categories where category_id = ?" );
s.setInt(1, category_id) ;
rs = s.executeQuery();
while(rs.next())
{
prod_id = rs.getString(1);
al.add(rs.getString(2));
/* If I comment out the below codes, I will get the result from the first statement */
s2 = conn.prepareStatement("select product_image_path from product_images where product_id = ? );
s2.setInt(1, prodId) ;
rs2 = s2.executeQuery();
al.add(rs2.getString(1));
/* above is to get image path in while loop for each product id */
} // end while loop
} // end try
Catch {
.....
}
return al;
}
But seems the second preparedStatement doens't work within while(rs.next() loop -- I will get nothing when I add the second preparedStatement in while loop -- If I comment it out, I will at least get product name, ids from the first result set.
Please advise how to make it work -- How to use the product_id from the first resultset to get the other information running the second query.
Thank you very much -- Please let me know if you are not clear of my question.