Turn the JDBC ResultSet result set into a list

Source: Internet
Author: User

Private list<map<string, object>> List = new arraylist<map<string,object>> ();p ublic String Queryall () {Connection conn = null; Statement sta = null; ResultSet rs = null;try {class.forname ("Com.mysql.jdbc.Driver"); conn = Drivermanager.getconnection ("jdbc:mysql:// Localhost:3306/easyui "," root "," root "); sta = Conn.createstatement (); rs = Sta.executequery (" SELECT * from E_user "); ResultSetMetaData MD = Rs.getmetadata (); Get the result set structure information, meta data int columnCount = Md.getcolumncount ();   Gets the number of columns while (Rs.next ()) {map<string,object> rowdata = new hashmap<string,object> (), for (int i = 1; I <= Co Lumncount; i++) {rowdata.put (Md.getcolumnname (i), rs.getobject (i));} List.add (RowData);}} catch (ClassNotFoundException e) {e.printstacktrace ();} catch (SQLException e) {e.printstacktrace ();} Return "Success";}


ResultSet Brief Introduction:
ResultSet includes all rows that conform to the conditions in the SQL statement, and it passes through a set of Get methods (these get methods can access different columns in the current row)

Provides access to the data in these rows.

The Resultset.next method is used to move to the next line in the ResultSet so that the next line becomes the current row.

The Rs.getobject (i) above is the data value for this row

ResultSetMetaData Brief Introduction:
The Resultsetmeta object can be obtained by using ResultSet's GetMetaData method, and ResultSetMetaData stores resultset metadata.

The so-called metadata in English is interpreted as "data about data", literal translation into Chinese is "the relevant information" or "descriptive data",

is actually the data describing and explaining the meaning. Taking the metadata of result as an example, resultset is present in tabular form, so GetMetaData

Includes information that must be available for tables such as the field name, type, and number of data. There are several methods in the ResultSetMetaData class.

ResultSetMetaData Rsmd=rs.getmetadata ();

1. Getcolumcount () method

Returns the number of all fields

2. Getcolumname () method

Gets the name of the field based on the index value of the field.

3. Getcolumtype () method

Get an exponential type field from a field



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

To change the JDBC ResultSet result set to list

Related Article

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.