Used in conjunction with Linkedhashmap and ArrayList to return a similar recordset's ArrayList

Source: Internet
Author: User

Using the combination of Linkedhashmap and ArrayList, the SQL query statement in the incoming method is returned with a ArrayList containing multiple linkedhashmap. Similar to returning a query's recordset.

Linkedhashmap is a linked HashMap, where the order of key-value pairs is the order of insertion.

Getconn () method is a connection database method.

Querying Data ******************************//
/**
*
* @param selectsql: SQL statement to query
* @return: ArrayList data set containing HashMap
*/

public static ArrayList getdatalist (String selectsql)
{
Arraylist<linkedhashmap <string, object>> al = new Arraylist<linkedhashmap <String, Object>> () ;
try {

Getconn ()//Connect to the database
stmt = Conn.createstatement ();
rs = Stmt.executequery (selectsql);
RSMD = Rs.getmetadata ();
while (Rs.next ())
{
hashmap<string, object> map = new hashmap<string, object> ();
Linkedhashmap map = new Linkedhashmap ();/one time per loop/instance of a map.
for (int i=1; I<=rsmd.getcolumncount (); i++)
{
Map.put (Rsmd.getcolumnname (i), Rs.getobject (Rsmd.getcolumnname (i)));
}
Al.add (map);//Add Map to ArrayList.
}
catch (Exception e) {e.printstacktrace ();}
Finally
{
try {
if (!conn.isclosed ())//if not closed successively
{
Rs.close ();
Stmt.close ();
Conn.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
}

Return al;
}

Take the value in the ArrayList.

Because a ArrayList is returned and the ArrayList contains Linkedhashmap, the ArrayList collection is traversed once and the linkedhashmap contained in each traversal is removed. For map, you can pass keyset ( method to remove all the keywords in the Linkedhashmap, the following code is implemented:

for (int i=0;i<list.size (); i++)
{
Map = (Linkedhashmap) list.get (i);
for (int j=0;j<map.keyset (). Size (); j + +)
{
System.out.println (Map.get (Map.keyset (). ToArray () [j]));
}
}

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.