Convert JDBC ResultSet result set to List

Source: Internet
Author: User

Private List <Map <String, Object> list = new ArrayList <Map <String, Object> (); public 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.exe cuteQuery ("select * from e_user"); ResultSetMetaData md = rs. getMetaData (); // get the result set structure information, metadata int columnCount = md. getColumnCount (); // get the number of columns while (rs. next () {Map <String, Object> rowData = new HashMap <String, Object> (); for (int I = 1; I <= columnCount; I ++) {rowData. put (md. getColumnName (I), rs. getObject (I);} list. add (rowData) ;}} catch (ClassNotFoundException e) {e. printStackTrace ();} catch (SQLException e) {e. printStackTrace ();} return "success ";}


Introduction to ResultSet:
The ResultSet contains all rows that meet the conditions in the SQL statement and uses 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 row in the ResultSet to make the next row the current row.

The above rs. getObject (I) is to obtain the data value of this row.

Introduction to ResultSetMetaData:
The getMetaData method of ResultSet can be used to obtain the ResultSetMeta object, while ResultSetMetaData stores the MetaData of ResultSet.

The so-called MetaData is interpreted as "Data about Data" in English. The literal translation is "Data related to Data" or "Data describing Data ",

It is actually the data that describes and explains the meaning. Taking the MetaData of Result as an example, the ResultSet exists in the form of a table, so getMetaData

It includes the field names, types, numbers, and other required information of the data table. There are several methods in the ResultSetMetaData class.

ResultSetMetaData rsmd = rs. getMetaData ();

1. getColumCount () method

Returns the number of all fields.

2. getColumName () method

Obtain the field name based on the field index value.

3. getColumType () method

Obtains the field type based on the field index value.



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.