Simplified Multiple Condition Judgment Query (1)

Source: Internet
Author: User

Simplifies the judgment of multiple if conditional statements, so that the judgment of multiple conditions query is simpler, no longer for the condition is empty and worry, here is only a template, specific examples also need to mediate their own

One: Create a list collection and load the list data

List listsql = new ArrayList ();
String sql = "";
Listsql.add ("Cxusers");
Listsql.add (txthphm==null "": TXTHPHM)//judge whether the resulting content is empty, empty will be marked as empty characters, on the other hand, is the foreground page passed the value of
listsql.add (txthpzl==null? ") : TXTHPZL);
Listsql.add (txtwfsjs==null "": TXTWFSJS);
Listsql.add (txtwfswje==null "": txtwfswje);
		
Listsql.add (zhxxcx==null "": ZHXXCX);//Comprehensive information Mark
Listsql.add (txtwfdd==null? "": TXTWFDD);//illegal location
mark Listsql.add (xq==null "": XQ);/district Mark
listsql.add (txetjdry==null? "": txetjdry);/Proofing Staff Mark


Two: Getzhxxsql method, used to execute conditional SQL statements

Public String getzhxxsql (List list) {//list StringBuffer sql = new StringBuffer (); if ("Cxusers". Equals (List.get (0). toString ())) {sql.append ("SELECT count (*) from nlv_violation t where 1=1 and T.JDZT =
			' 2 ' and t.jllx= ' 1 '); if (List.size () >1) {if (! "").
			Equals (List.get (1). ToString ())) {//If not NULL, executes this statement instead of Sql.append ("and t.hphm like '%" +list.get (1). ToString () + "%"); } if (List.size () >2) {if (!).
			Equals (List.get (2). ToString ())) {Sql.append ("and T.hpzl= '" +list.get (2). ToString () + "'"); } if (List.size () >3) {if (!). Equals (List.get (3). ToString ())) {Sql.append ("and To_char (T.WFSJ, ' yyyy-mm-dd ') >= '" +list.get (3). ToString () + "'")
				; } if (List.size () >4) {if (!). Equals (List.get (4). ToString ())) {Sql.append ("and To_char (T.WFSJ, ' yyyy-mm-dd ') <= '" +list.get (4). ToString () + "'")
				; } if (List.size () >5) {if (!). Equals (List.get (5). toString ())) {Sql.append ("and t.sjly= '" +list.get (5). TostrING () + "'"); } if (List.size () >6) {if (!).
				Equals (List.get (6). ToString ())) {Sql.append ("and t.wfdd like '%" +list.get (6). ToString () + "%"); } if (List.size () >7) {if (!).
				Equals (List.get (7). ToString ())) {Sql.append ("and t.cjjg= '" +list.get (7). ToString () + "'"); } if (List.size () >8) {if (!).
					Equals (List.get (8). ToString ())) {Sql.append ("and T.jdr= '" +list.get (8). ToString () + "'");
}} return sql.tostring (); }

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.