How to save the results from a query in a database to a list __ database

Source: Internet
Author: User
Tags object object

See a lot of people ask how to save the results from a query in a database as list<map>

I have studied it today, and if there is something wrong, please correct me.

ResultSet how to get in this is no longer elaborated on the Internet a bunch of search.


When we obtain resultset RS, we can convert to ResultSetMetaData objects.
ResultSetMetaData can be used to get an object about the type and property information of a column in a ResultSet object.
Look at the example below
Get ResultSetMetaData Object
ResultSetMetaData Rsmd=rs.getmetadata ();
Gets the number of columns returned in this ResultSet object
int count = Rsmd.getcolumncount ();
while (Rs.next ()) {
Map map=new HashMap ();
for (int i=1;i<count;i++) {
Gets the table directory name of the specified column
String Label=rsmd.getcolumnlabel (i);
Gets the value of the specified column in the current row of this ResultSet object as object in the Java programming language
Object object= Rs.getobject (i);
Match the field names and values in the database to a key value pair in a Map object
Map.put (Label.tolowercase (), object);
}
Put the map object encapsulated in each object into the list
List.add (map);
}
This allows any resultet to generate a list object in this way without considering how to correspond to the fields in the database.

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.