2011-08-17 92 views
0

我有低於這個函數從Oracle數據庫拉數據的JavaBean的名稱FanBean有困難從Oracle數據庫中提取數據,JavaBean和JSP

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頁面中獲取任何數據。謝謝

+0

只是檢查,但你確定你的JSP的作品?它看起來像在你的JSP腳本中,你正在爲while循環缺少一個閉合的「}」(假設你直接從你的JSP中拷貝了這個,我確信這會導致一個問題)。 – brent777

+0

哦對不起,我忘了添加它..我有.. – Jack

+0

你在哪裏添加bean到你的請求上下文?你使用servlet嗎? – home

回答

0

這段代碼似乎有很多錯誤。

  1. 哪裏是_ dltListArray定義和初始化?

  2. 您的while循環rs.next()似乎是不正確的。您將元素添加到列表中,並立即迭代枚舉,而不是等待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()方法?