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; }}