How the backend uses get or post data from the database to be packaged into JSON:
Need to download the JSON jar package, here are: http://download.csdn.net/detail/zhangy0329/607838
Connection ct =NULL; Statement SM=NULL; ResultSet RS=NULL; Try{class.forname ("Com.mysql.jdbc.Driver");//connect to MySQL databasect = drivermanager.getconnection ("jdbc:mysql://localhost:3306/database name? useunicode=true&characterencoding=utf-8", "User name", "Password"); SM=ct.createstatement (); RS= Sm.executequery ("Select *from table Name"); Jsonarray Jsondata= Jsonarray.fromobject (Convertlist (RS));//Go to the list format and then convert to JSON formatSystem.out.println (jsondata.tostring ()); PrintWriter out= Response.getwriter ();//passing JSON data to the front endOut.print (Jsondata); } Catch(Exception ex) {//Error code}
data from the database is turned into Li St's Operation function:
Private StaticList convertlist (ResultSet rs)throwsSQLException {List List=NewArrayList (); ResultSetMetaData MD=Rs.getmetadata (); intColumnCount =Md.getcolumncount (); while(Rs.next ()) {Map RowData=NewHashMap (); for(inti = 1; I <= ColumnCount; i++) {rowdata.put (Md.getcolumnname (i), rs.getobject (i)); } list.add (RowData); } returnlist; }
Data from the database is packaged into JSON and then passed to the front end