Work with a variable number of parameters in a prepared state

I am creating a search form for my application.
In it, the user selects the fields that should be used to filter data.
numeric fields are variables, so I don’t know how much ?should be in the place of the SQL query sentence.
How can I use preparedStatementwith a variable number of conditions in where where?

thank

+5
source share
2 answers

PrepardStatements does not support variable number conditions. What some frameworks do, they cache every PreparedStatement on a map where the key is a request.

, , , PreparedStatement, , ( ), , .

+3

, StringBuilder (StringBuffer, env.) conciate/append .

like

StringBuilder query = new StringBuilder("Select id, name from Student ");


if(args >0)
{
    query.append(" where "); //and add more args.

,

PrepareStatement(query.toString());
+4

All Articles