java - fetching records from database based on variable number of inputs from from -


i have form 8 fields , based on values entered in them have have fetch records database. problem out of 8 fields user may fill number of fields , in order example user may fill fields 1,4 , 6 or may fill 1 , 7 or may fill of them (of course has fill @ least 1 field)... how write query work number , order of input parameters? , because query used in reporting(ireport) not allowed write code , has sql query. ideas

thanks

there number of ways this. have blog post doing in microsoft sql (t-sql) stored procedure @ http://code.scottshipp.com/2013/03/29/tutorial-stored-procedures-with-truly-optional-parameters/ want more complex and/or not using ms sql server. may have write query fragments yourself. suggestion following:

  1. once form submitted, loop through various fields in form, , check if values empty or not.
  2. for not empty, add string containing appropriate corresponding query fragment collections object, arraylist. not include "and" or "or" directly in string. if need keep track of whether query fragment gets "and"ed or "or"ed other query fragments, track in separate collections object. "query fragment" i'm talking show in "where __" portion of sql query. instance, search someone's last name. query fragment add string says "lastname='" + lastnamefield.value() + "'".
  3. once have iterated through various fields in form , have final collections object full of query fragments, construct final sql statement it. iterate through collections object (arraylist in example) , connect each 1 appropriate "and" or "or". arraylist has 3 fragments "firstname='joe'", "middlename='q.'", "lastname='public'". use stringbuilder keep adding these fragments final clause: "firstname='joe' , middlename='q.' , lastname='public'"...you may want change these "like" style query wildcard characters.
  4. you have need create final select statement. issue database , retrieve results!

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -