繁体   English   中英

如何在java中的结果集中添加列?

[英]How to add column in resultset in java?

我有桌子员工

select emp_name,emp_add from employee检索列select emp_name,emp_add from employee

while(iResultSet1.next())
            {
                List expRptColWise = new ArrayList();

                for(Integer i=1;i<=iResultSet1.getMetaData().getColumnCount();i++){


                        expRptColWise.add(iResultSet1.getString(i));

                }

                expRptRowWise.add(expRptColWise);

有了上面的片段,我明白了

emp_name | emp_add |
A        | add1    |
B        | add2    |

我想在结果集中添加序列号coloumn,以便得到结果

emp_name | emp_add |Sr_No|
A        | add1    |1    |
B        | add2    |2    |

请指导我如何在结果集或集合对象中动态添加列,这里我使用了ArrayList。

使用以下查询

 select emp_name,emp_add, (ROW_NUMBER() OVER ( ORDER BY emp_name)) AS sr_no from employee

在While循环中设置一个计数器,每次递增并在输出中使用它:

int j = 0;
while(iResultSet1.next())
{
   j++;
   List expRptColWise = new ArrayList();
   for(Integer i=1;i<=iResultSet1.getMetaData().getColumnCount();i++)
   {
      expRptColWise.add(iResultSet1.getString(i));
   }
   expRptColWise.add(j);
   expRptRowWise.add(expRptColWise);
}

我更喜欢DinupKandel的回应,但仅仅因为我们不应该把所有鸡蛋放在同一个篮子里

int row = 0;
while(iResultSet1.next())
{
    List expRptColWise = new ArrayList();
    for(Integer i=1;i<=iResultSet1.getMetaData().getColumnCount();i++){
        expRptColWise.add(iResultSet1.getString(i));
    }
    expRptColWise.add(++row);
    expRptRowWise.add(expRptColWise);
}

可能会为你工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM