Tools for JSON sending should be quite common!

Source: Internet
Author: User

 
1/** 2 * convert the SQL-queried resultset object to jsonarray 3 * @ Param Rs resultset object 4 * @ return jsonarray, which contains the names of all resultset columns: content 5 * @ throws exception 6 */7 public static jsonarray formatrstojsonarray (resultset RS) throws exception {8 resultsetmetadata rsmd = Rs. getmetadata (); // get the source data 9 int COUNT = rsmd. getcolumncount (); // get the number of columns 10 jsonarray = new jsonarray (); 11 while (RS. next () {12 jsonobject temp = new jsonobject (); 13 for (INT x = 1; x <= count; X ++) {14 object o = Rs. getObject (x); 15 if (O instanceof Java. util. date) {// if it is a date column 16 temp. put (rsmd. getcolumnname (x), 17 dateutil. formatdate (date) rs. getObject (x), "yyyy-mm-dd"); 18} else {19 // key: column name 20 temp. put (rsmd. getcolumnname (x), RS. getObject (x); 21} 22} 23 jsonarray. add (temp); 24} 25 return jsonarray; 26}

Coupled with a servlet like the class output at the front end, it is also commonly used

Public static void sentajax (httpservletresponse resp, object O) throws exception {resp. setcontenttype ("text/html; charset = UTF-8"); printwriter P = resp. getwriter (); p. println (O. tostring (); p. flush (); // refresh p. close ();}

By the way, record a tool to determine whether the string is null for future use.

  public static boolean isEmpty(String str){          if(str==null || str.equals("")){              return true;          }else{             return false;          }    }        public static boolean isNotEmpty(String str){        if(!("").equals(str) && null!=str){         return true;        }else{         return false;  }}

 

 

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.