java - Given a series of variables (A, B, C ...), How to form the Sql String Ex "A=? and B=?...." but only taking into account for Non-Empty variables? -
i have need form sql string searching data.
i have long list of variables such as: string id, string fname, string city, int custtype, ....
, table in db have these columns (id, fname, lname, city...)
i want search data based on info provided variables, non-empty variables should used in sql string.
ex1 if variables following:
string id="22"; string fname=""; string lname="tom"; string city=""; int custtype=0;
then
string sql="select * customer "; string where=" id=? , lname=?"; sql+=where; //then call db
ex2 if variables following:
string id=""; string fname=""; string lname="tom"; string city="ny"; string postcode="211";
then
string where=" lname=? , city=? , postcode=?";
if there 1 variable not empty & others empty no need and
, ex string where=" lname=?"
i thinking put varibles list, varibles have int type. don't know best logics forming string that.
can come solution?
you can check stringutils.isblank()
string inputs. int
can't hold ""
there no need check ""
. depending upon can concatenate strings form final query string. "and" , other such kind of logic , can choose have flag
Comments
Post a Comment