How to set query conditions dynamically in JDBC

Source: Internet
Author: User
Tags getdate

Looking at JDBC today, we find that there is a snippet of code that can reduce the number of repetitive methods of writing queries, as follows:

1      Publiclist<goddess> query (list<map<string, object>> params)throwsexception{2List<goddess> result=NewArraylist<goddess>();3         4Connection conn=dbutil.getconnection ();5StringBuilder sb=NewStringBuilder ();6Sb.append ("select * from imooc_goddess where 1=1");7         8         if(params!=NULL&&params.size () >0){9              for(inti = 0; I < params.size (); i++) {TenMap<string, object> map=Params.get (i); OneSb.append ("and" +map.get ("name") + "" +map.get ("RelA") + "" +map.get ("value") + ""); A             } -         } -          thePreparedStatement ptmt=conn.preparestatement (sb.tostring ()); -          - System.out.println (sb.tostring ()); -ResultSet rs=ptmt.executequery (); +          -Goddess G=NULL; +          while(Rs.next ()) { Ag=Newgoddess (); atG.setid (Rs.getint ("id")); -G.setuser_name (rs.getstring ("user_name")); -G.setage (Rs.getint ("Age")); -G.setsex (Rs.getint ("Sex"))); -G.setbirthday (rs.getdate ("Birthday")); -G.setemail (rs.getstring ("email")); inG.setmobile (Rs.getstring ("mobile")); -G.setcreate_date (Rs.getdate ("Create_date")); toG.setcreate_user (rs.getstring ("Create_user")); +G.setupdate_date (Rs.getdate ("Update_date")); -G.setupdate_user (rs.getstring ("Update_user")); theG.setisdel (Rs.getint ("Isdel")); *              $ Result.add (g);Panax Notoginseng         } -         returnresult; the}

It uses list<map<string,object>> to pass the data.

Map.put (name, "XXX"); Query condition name

Map.put (rel, "XXX"); The conditional relationship of a query is greater than, less than, or equal to

Map.put (Value, "XXX"); condition value of the query

Use list can store more than one map, reduce for a particular function to write a query method, enhance the code to take!

Thank you-jdbc the girl across the school to see the course

How to set query conditions dynamically in JDBC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.