0

After selecting some products a user clicked on proceed button. Now I need to display the selected data on next page. I was successful in getting the id's of selected data using the following code.

String[] array = request.getParameterValues("arrayid");

Now I need to query mysql database using "select * from table where id=?" I can use this query in a loop. But is there any other or a better way to do this?

3 Answers 3

2

Use IN keyword while selecting as select * from table where id IN(comma separated ids)

Sign up to request clarification or add additional context in comments.

Comments

1
String[] array = request.getParameterValues("arrayid");    
String sql = "SELECT * FROM TABLENAME WHERE id IN ?"; 
PreparedStatement ps = con.prepareStatement(sql);
ps.setArray(1,con.createArrayOf("CHAR", array));

1 Comment

Thanks for the reply. I understood this logic. But I get an error 'java.sql.SQLFeatureNotSupportedException' while using the createArrayOf() method. I used another logic to solve my issue. Following link explains that Click Here
0

If your question is how to query for a set of IDs in a table, the other answers correctly refer you to SQL's IN keyword. However if you're asking specifically how to do it in Java, JDBC's PreparedStatement, which is the generally recommended way of executing queries in Java, does not make constructing these IN statements easy. I posted a suggested way to address this issue reasonably cleanly here: Can PreparedStatement.addBatch() be used for SELECT queries?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.