存放在一个结果集的每一行作为数组中的对象 [英] Store each row in a resultset as an object in an array

查看:110
本文介绍了存放在一个结果集的每一行作为数组中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我的问题是,我回来从JDBC查询一个结果,我想每一行存储为数组的对象。当我通过它不仅节省对象的基于阵列的resultset-尝试循环我想这显然是由于事实while循环内,用于环路我试图重新跳汰的循环了几次,但与没有喜悦。
将是伟大的发现越来越传递到数组对象的那么复杂和convul​​uted方式。

 的ResultSet RS = st.executeQuery(查询);
    检查C =新的Check();
    rs.last();
    INT numberOfRows = rs.getRow();
    检查checkArray [] =新的检查[numberOfRows]
    rs.beforeFirst();    的for(int i = 0; I< checkArray.length;我++)
    {
     而(rs.next())
     {
    c.setAmount(rs.getBigDecimal(数量));
    c.setCheckNumber(rs.getString(CHECKNUMBER));
    c.setComments(rs.getString(意见));
    c.setId(rs.getLong(SETID));
    c.setMailTo(rs.getString(电子邮件地址));
    c.setPayTo(rs.getString(PAYTO));
    c.setProcessed(rs.getBoolean(已处理));
    checkArray [I] = C;
}
}


解决方案

除非你的的ResultSet 是一个实例的CachedRowSet ,它将不知道有多少行有,并返回从 .getRow零()

与code的主要问题是,你是的再利用的的的相同的检查对象实例一遍又一遍,这意味着你的数组中的所有元素都是同样的检查对象。您定义为创建新实例的每个的行

更好的负载的所有行成列表,并使用而(resultSet.next())来控制循环

如果你绝对需要一个数组,列表转换为数组在与 List.toArray结束()

在code的缩写版本是:

 列表<检查>名单=新的ArrayList<检查>();而(rs.next()){
    检查C =新的Check();
    //组C领域
    list.add(C);
}检查[] = checkArray list.toArray();

Essentially my problem is that I am returning a resultset from a jdbc query and i want to store each row as an object in an array. when i try to loop through the resultset- it only saves on object in the array- i suppose this is obviously due to the fact that the while loop is within the for loop- i have tried re-jigging the loops a few times but with no joy. Would be great to find a less complex and convuluted way of getting the objects passed into the array.

    ResultSet rs = st.executeQuery(query);
    Check c = new Check();
    rs.last();
    int numberOfRows = rs.getRow(); 
    Check checkArray [] = new Check [numberOfRows]; 
    rs.beforeFirst();

    for (int i = 0; i < checkArray.length; i++)
    {
     while (rs.next())
     {
    c.setAmount(rs.getBigDecimal("AMOUNT"));
    c.setCheckNumber(rs.getString("CHECKNUMBER"));
    c.setComments(rs.getString("COMMENTS"));
    c.setId(rs.getLong("SETID"));
    c.setMailTo(rs.getString("MAILTO"));
    c.setPayTo(rs.getString("PAYTO"));
    c.setProcessed(rs.getBoolean("PROCESSED"));
    checkArray[i]= c;
}
}

解决方案

Unless your ResultSet is an instance of CachedRowSet, it won't know how many rows there are and will return a zero from .getRow()

The main problem with your code is that you are reusing the same Check instance over and over, meaning all elements of your array are the same Check object. You ned to create a new instance for every row

Better load all rows into a List and use while (resultSet.next()) to control to loop.

If you absolutely need an array, convert the List to an array at the end with List.toArray()

An abbreviated version of the code is:

List<Check> list = new ArrayList<Check>();

while (rs.next()) {
    Check c = new Check();
    // set fields of c
    list.add(c);
}

Check[] checkArray = list.toArray();

这篇关于存放在一个结果集的每一行作为数组中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆