如何遍历使用hashmap映射的列表元素

时间:2013-12-16 13:07:35

标签: jsp jdbc collections resultset

我能够实现我想要的大部分,但在这里我被卡住了......请帮忙。

public class DatabaseHandler {
private static String DB_CLASS ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//private static String DB_USER,DB_PASSWORD, DB_NAME, DB_URL, DB_QUERY_ID;
Statement stmt; //=null;
Connection conn;// = null;
public static Map<Integer, List<String>> allActiveQuery = new HashMap<Integer, List<String>>();

public DatabaseHandler() {

    brattleDb();


}

public  void brattleDb(){
        try {
        Class.forName(DB_CLASS);
        conn = DriverManager.getConnection("jdbc:sqlserver://TOSHIBA-PC;databasename=brattle;user=sa;password=naved");
        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM database_details_master WHERE status = 1");
    int i=0;
    while(rs.next()){
        List<String> row  = new ArrayList<String>();
        row.add(rs.getString(1).toString());
        row.add(rs.getString(2).toString());
        row.add(rs.getString(3).toString());
        row.add(rs.getString(4).toString());
        row.add(rs.getString(5).toString());
        row.add(Integer.toString(rs.getInt(6)));
        row.add(Integer.toString(rs.getInt(7)));
        row.add(rs.getString(8).toString());
        row.add(rs.getString(9).toString());
        row.add(rs.getString(10).toString());
        row.add(rs.getString(11).toString());
        row.add(Integer.toString(rs.getInt(12)));
        row.add(Integer.toString(rs.getInt(13)));
        allActiveQuery.put(i, row);
        i++;
    }
            rs.close();
            conn.close();

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }


}

public void setDynamicConnection(String db_name, String db_user, String db_pass, String db_url){
    try {
        Class.forName(DB_CLASS);
        conn= DriverManager.getConnection("jdbc:sqlserver:"+db_url+";databasename="+db_name+";user="+db_user+";password="+db_pass+"");
        stmt = conn.createStatement();

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}

以上代码从数据库中获取状态为活动的行。

在Jsp上我试图获取所有这样的行:

<%
        // Connection to get active queries

        try{
        DatabaseHandler dbh = new DatabaseHandler();

        for(Map.Entry<Integer, List<String>> entry: dbh.allActiveQuery.entrySet()){
            Integer key = entry.getKey();
            out.println("Query:"+key+"<br><hr>");
                List<String> values  = entry.getValue();
                out.println(values+ "<br> <hr> ");

        }

        }


    catch(Exception e){
        out.print(e.toString());
    }

         %>

结果我得到了这个:

 1. [sa, wms, TOSHIBA-PC, naved, FIFO Query, 23, 35, select CSDETL01.SKUCODE ,SKUMST00.SKUDESC ,CSDETL01.BATCH ,SKUMST00.MRP , CSDETL01.MFGDATE ,CSDETL01.EXPDATE ,sum(CSDETL01.CURQTY) , Datediff(d, CSDETL01.MFGDATE, CSDETL01.EXPDATE) ,Datediff(d, GETDATE(), CSDETL01.EXPDATE) , Datediff(d, GETDATE(), CSDETL01.EXPDATE)*100/Datediff(d, CSDETL01.MFGDATE, CSDETL01.EXPDATE) from CSDETL01, SKUMST00 where SKUMST00.INOWNER =~MCCAIN~ and SKUMST00.whse =~MEH~ and SKUMST00.SKUCODE = CSDETL01.SKUCODE and CSDETL01.STATUS <= ~400~ group by CSDETL01.SKUCODE, CSDETL01.BATCH, CSDETL01.MFGDATE, CSDETL01.EXPDATE, SKUMST00.MRP, SKUMST00.SKUDESC order by CSDETL01.SKUCODE, CSDETL01.BATCH, ka@gmail.com, , , 1, 24] <br>
 2. [sa, wms, TOSHIBA-PC, naved, Partial Pallet Query, 23, 38, Select PALLETNO,SKUCODE, BATCH, MFGDATE, EXPDATE, CURLOCID, DTLM, sum(CURQTY) as SumQTY from CSDETL01 where INOWNER =~MCCAIN~ and whse =~MEH~ and SKUCODE in (~919044~, ~911802~) and STATUS <= ~400~ and LDUNIT = ~PALLET~ group by PALLETNO, SKUCODE, BATCH, MFGDATE, EXPDATE, CURLOCID, DTLM order by PALLETNO, k@yahoo.in, , , 1, 1]
 3. [sa, wms, TOSHIBA-PC, naved, FEFO QUERY, 23, 40, select CSDETL01.SKUCODE ,SKUMST00.SKUDESC ,CSDETL01.BATCH ,SKUMST00.MRP , CSDETL01.MFGDATE ,CSDETL01.EXPDATE ,sum(CSDETL01.CURQTY) , Datediff(d, CSDETL01.MFGDATE, CSDETL01.EXPDATE) ,Datediff(d, GETDATE(), CSDETL01.EXPDATE) , Datediff(d, GETDATE(), CSDETL01.EXPDATE)*100/Datediff(d, CSDETL01.MFGDATE, CSDETL01.EXPDATE) from CSDETL01, SKUMST00 where SKUMST00.INOWNER =~MCCAIN~ and SKUMST00.whse =~MEH~ and SKUMST00.SKUCODE = CSDETL01.SKUCODE and CSDETL01.STATUS <= ~400~ group by CSDETL01.SKUCODE, CSDETL01.BATCH, CSDETL01.MFGDATE, CSDETL01.EXPDATE, SKUMST00.MRP, SKUMST00.SKUDESC order by CSDETL01.SKUCODE, CSDETL01.BATCH, k@gmail.com, , , 1, 1]

我希望将列表的值作为单个元素进行迭代,但我将整个字符串作为值...

我希望输出像这样

1. sa
 wms 
TOSHIBA-PC 
naved
FIFO Query, 23, 35, select CSDETL01.SKUCODE ,SKUMST00.SKUDESC ,CSDETL01.BATCH ,SKUMST00.MRP , CSDETL01.MFGDATE ,CSDETL01.EXPDATE ,sum(CSDETL01.CURQTY) , Datediff(d, CSDETL01.MFGDATE, CSDETL01.EXPDATE) ,Datediff(d, GETDATE(), CSDETL01.EXPDATE) , Datediff(d, GETDATE(), CSDETL01.EXPDATE)*100/Datediff(d, CSDETL01.MFGDATE, CSDETL01.EXPDATE) from CSDETL01, SKUMST00 where SKUMST00.INOWNER =~MCCAIN~ and SKUMST00.whse =~MEH~ and SKUMST00.SKUCODE = CSDETL01.SKUCODE and CSDETL01.STATUS <= ~400~ group by CSDETL01.SKUCODE, CSDETL01.BATCH, CSDETL01.MFGDATE, CSDETL01.EXPDATE, SKUMST00.MRP, SKUMST00.SKUDESC order by CSDETL01.SKUCODE, CSDETL01.BATCH
 ka@gmail.com, 
,//null values 
, //null vaues
1 
24

等等..请帮助我被困在这里..

1 个答案:

答案 0 :(得分:0)

为什么不简单地遍历结果?

    for(Map.Entry<Integer, List<String>> entry: dbh.allActiveQuery.entrySet()) {
        out.println("Query:"+ entry.getKey() +"<br>");
        for (String val : entry.getValue()) {
            out.println(val + "<br>");
        }
        out.println("<hr>");

    }