Data from the database is packaged into JSON and then passed to the front end

Source: Internet
Author: User

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

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.