难以将数据从oracle数据库提取到javabean和jsp

时间:2011-08-17 17:55:01

标签: java sql oracle jsp

我有一个javabean名称FanBean,下面有这个函数从oracle数据库中提取数据

 public java.util.Vector getList() {

        java.util.Vector myFanList = new java.util.Vector();
        StringBuffer sql = new StringBuffer();

        DbUtil db = null;
        ResultSet rset = null;

            db = new DbUtil();

            sql.append(" SELECT id_no, name");
                if ((this.getSelectedFanType() != null)&& (this.getSelectedFanType().equals("1"))){
                    sql.append("  from my_fan ");
                }

                if ((this.getSelectedFanType() != null)&& (this.getSelectedFanType().equals("2"))){
                    sql.append("  from my_fan ");
                    sql.append("  where id_no = '");
                    sql.append(this.getSearchVar());
                    sql.append("'");
                }

                if ((this.getSelectedFanType() != null)&& (this.getSelectedFanType().equals("3"))){
                    sql.append("  from my_fan ");
                    sql.append("  where name = '");
                    sql.append(this.getSearchVar());
                    sql.append("'");
                }



            if (this.getSelectedSort() != null) {
                if(this.getSelectedSort().equals("1"))
                    sql.append(" ORDER BY id_no");
                else if(this.getSelectedSort().equals("2"))
                    sql.append(" ORDER BY name");
                else
                    sql.append(" ORDER BY id_no");
            } else
                sql.append(" ORDER BY id_no");

            rset =  db.executeQuery(sql.toString(),true);

            if (rset != null) {
                while(rset.next()) {
                    FanBean myFan = new FanBean();
                    myFan.setIdNo(rset.getString(1).trim());
                    myFan.setName(rset.getString(2));


                myFanList.addElement(myFan);
                Enumeration e = myFanList.elements();
                while (e.hasMoreElements()) {
                FanBean nextFan = (FanBean) e.nextElement();
                this.addDtlItem(nextFan.getIdNo(), nextFan.getName());
                }
                }
            }

        }

    return myFanList;
    }

我有这个函数,它将数据添加到FanItemBean java bean

public void addDtlItem(String idNo, String name )
    {
        FanItemBean detailItem = new FanItemBean(idNo, name);
        _dtlListArray.add(detailItem);

    }

最后在jsp我拉这样的数据

 <%
    Collection dtlItems = fanBean.getDetailItems();
    Iterator it = dtlItems.iterator();
    int i = 0;
    String colorStr = null;


    while(it.hasNext()){
        FanItemBean fanDtlItem = (FanItemBean) it.next();

    %>

        <TR>
            <TD><%= fanDtlItem.getIdNo()%></TD>
            <TD><%= fanDtlItem.getName()%></TD>
        </TR>
<% 
  i++;
} %>

有人可以告诉我在这个循环中我做错了什么,我没有在jsp页面中获取任何数据。谢谢

1 个答案:

答案 0 :(得分:0)

这段代码似乎有很多问题。

  1. _ dltListArray在哪里定义并初始化?

  2. rs.next()的while循环似乎不正确。您正在将该元素添加到列表中并立即迭代枚举,而不是等待while()循环的其余部分完成。这是正确的代码。

      if (rset != null) {
            while(rset.next()) {
                FanBean myFan = new FanBean();
                myFan.setIdNo(rset.getString(1).trim());
                myFan.setName(rset.getString(2));
    
    
                myFanList.addElement(myFan);
           } // FINISH ADDING ALL RESULTS TO YOUR LIST BEFORE ITERATING OVER THE ENUMERATION
    
           Enumeration e = myFanList.elements();
           while (e.hasMoreElements()) {
             FanBean nextFan = (FanBean) e.nextElement();
             this.addDtlItem(nextFan.getIdNo(), nextFan.getName());
           }
        }
    
  3. 此外,您的FanBean和FanItemBean看起来完全相同。为什么不直接将它添加到列表中而不是通过中间Vector?

  4. 使用Java 1.5和泛型的任何特定限制?

  5. 另外,你的jsp在哪里调用getList()方法?