我在下面有一些代码用于查询数据库并将数据添加到列表中,
Query q= session.createQuery("select tally_receipt_prefix, tally_receipt_no, tally_head, tally_amount from Tally_table where tally_system_date='"+fmtd_date+"' and tally_dbcr_indicator='DB' and tally_mode='Ca' order by tally_head,tally_receipt_prefix,tally_receipt_no");
payincash = new ArrayList();
for(Iterator it=q.iterate(); it.hasNext(); )
{
Object[] row= (Object[]) it.next();
payincash.add((String)row[0]);
payincash.add((String)row[1]);
payincash.add((String)row[2]);
payincash.add((String)row[3]);
}
System.out.println("cash list in dao: "+payincash);
返回的列表类似于 [prefix1, no1, head1, amt1, prefix2, no2, head2, amt2,]。我正在尝试在 jsp 中制作收据
头1
前缀 1/no1 amt1
prefix2/no2 amt 2
头3
前缀 3/no3 amt3
所以看起来我想在收据 - jsp 文件中按头列对所有记录进行分组。我该怎么做?任何帮助完全赞赏。请原谅我的英语。
编辑:这是我尝试过的,
查询 q= session.createQuery("selecttally_receipt_prefix,tally_receipt_no,tally_head,tally_amount from Tally_table where tally_system_date='"+fmtd_date+"' and tally_dbcr_indicator='DB' and tally_mode='Ca' order by tally_head,tally_receipt_prefix,tally_receipt_no"); System.out.println("查询"+q);
List heads=new ArrayList();
for(Iterator it=q.iterate(); it.hasNext(); )
{
Object[] row= (Object[]) it.next();
payincash1=new LinkedHashMap<String, List>();
heads.add((String)row[2]);
List tails = null;
tails=new ArrayList();
tails.add((String)row[0]);
tails.add((String)row[1]);
tails.add((String)row[3]);
System.out.println("heads in dao from iter 1: "+heads);
System.out.println("tails in dao from iter1 on: "+tails);
if(heads.contains((String)row[2])) // for head in temp list
{
System.out.println("in first if");
if(payincash1.containsKey((String)row[2]))
{
System.out.println("map if repeat: "+payincash1);
payincash1.put((String)row[2],tails);
}
}
else
{
System.out.println("map if not repeat: "+payincash1);
payincash1.put((String)row[2], tails);
}
}