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]));
}
}