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.