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>
4

2 回答 2

1

问题是当您遇到异常时您没有关闭Connectionand 。ResultSet所以数据库已经用尽了所有打开的游标

于 2012-11-30T07:48:41.180 回答
0

您需要关闭您打开的所有内容,这意味着语句、结果集。你这样做是finally为了try/catch确保事情正确关闭。

当您关闭语句时,链接到该语句的结果集也将关闭。

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 的文档(此处为 Play2)有数据库的东西,以避免直接使用ResultSets 和Statements,处理更高的结构,如域对象,框架将为您完成其余的工作。

于 2012-11-30T07:52:04.400 回答