2012-11-30 82 views
1

我正在從數據庫獲取所有數據並將結果集存儲到列表中。但無法獲取所有數據。我想將數據存儲在下拉列表中。我的代碼是波紋管。無法從結果集中的數據庫獲取所有數據

public static void updateChallan(){ 
    ChallanNumber pd=null; 
    int i=0; 
    String customerName=""; 
    List<ChallanNumber> challanList= new ArrayList<ChallanNumber>(); 
    Connection con = DB.getConnection(); 
    try 
    { 
    String st="select CHALLAN_NUMBER,CUSTOMER_CODE,CHALLAN_DATE from DELIVERY_CHALLAN_DETAILS order by CHALLAN_NUMBER"; 
    Statement stmt=con.createStatement(); 
    ResultSet rs=stmt.executeQuery(st); 
    while(rs.next()) 
    { 
     String stCustName="select CUSTOMER_NAME from CUSTOMER_DETAILS where CUSTOMER_CODE='"+rs.getString(2)+"'"; 
     Statement stmtCustName=con.createStatement(); 
     ResultSet rsCustName=stmtCustName.executeQuery(stCustName); 
     while(rsCustName.next()){ 
      customerName=rsCustName.getString(1); 
     } 

     customerName=rsCustName.getString(1); 
     //System.out.println(customerName +" "+i); 
     pd=new ChallanNumber(rs.getString(1),customerName,rs.getString(3)); 
     challanList.add(i,pd); 
     i++; 
    } 
    } 
    catch(Exception e) 
    { 
     //e.printStackTrace(); 
    } 
    render(challanList); 
} 

Dropdownlish代碼在下面。

<select name="challanNumber" id="challanNumber"> 
       <option value="selected" selected="selected">ChallanNumber-CustomerCode- Date</option> 

       #{list challanList, as:'cl'} 

       <option value="${cl.challanNumber}">${cl.challanNumber}(${cl.customercode}-${cl.challanDate})</option> 

       #{/list} 



      </select> 
+1

如果發生異常並且您沒有注意到它,因爲您只是放棄它而該怎麼辦。 – Alex

+0

好的。實際上我在數據庫中有344個值,但只顯示了299個值。 ORA = 01000的例外情況是:最大打開遊標超過。 @Alex –

+0

這是因爲你沒有關閉你在while循環中打開的語句。請參閱下面的不同答案。 – Alex

回答

0

您需要關閉您打開的所有內容,這意味着語句,結果集。您可以在try/catchfinally部分執行此操作,以確保東西正確關閉。

當您關閉語句時,鏈接到該語句的結果集也被關閉。

public static void updateChallan() throws Exception { 
    ChallanNumber pd = null; 
    int i=0; 
    String customerName = ""; 
    List<ChallanNumber> challanList= new ArrayList<ChallanNumber>(); 
    Connection con = DB.getConnection(); 
    Statement stmt = null; 
    try { 
     String st = "select CHALLAN_NUMBER,CUSTOMER_CODE,CHALLAN_DATE from DELIVERY_CHALLAN_DETAILS order by CHALLAN_NUMBER"; 
     stmt = con.createStatement(); 
     ResultSet rs = stmt.executeQuery(st); 
     while (rs.next()) { 
      String stCustName = "select CUSTOMER_NAME from CUSTOMER_DETAILS where CUSTOMER_CODE='" + rs.getString(2) + "'"; 
      Statement stmtCustName = con.createStatement(); 
      try { 
       ResultSet rsCustName = stmtCustName.executeQuery(stCustName); 
       while (rsCustName.next()){ 
        customerName = rsCustName.getString(1); 
       } 
      } finally { 
       if (stmtCustName != null) 
        stmtCustName.close(); 
      } 

      customerName = rsCustName.getString(1); 
      //System.out.println(customerName +" "+i); 
      pd = new ChallanNumber(rs.getString(1), customerName, rs.getString(3)); 
      challanList.add(i, pd); 
      i++; 
     } 
    } catch(Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (stmt != null) 
      stmt.close(); 
    } 
    render(challanList); 
} 

此外,你應該閱讀PlayFramework的文檔(here for Play2)有數據庫的東西,以避免使用ResultSet S和Statement小號直接,處理像域對象更高的結構,框架將完成剩下的爲您服務。

1

的問題是,你是不關閉ConnectionResultSet當你得到一個異常。所以數據庫用盡了所有打開的遊標

+0

絕不會忽略異常,尤其是當您使用**系統資源**時,例如** DB連接或套接字**。 –

+0

可以請你幫我關閉結果集或連接。 @Narendra Pathai –

+0

'catch(Exception e) {0e.printStackTrace(); con.close(); rs.close(); }' –