简体   繁体   English

无法从结果集中的数据库中获取所有数据

[英]could not fetch all data from database in resultset

i am fetching all data from database and stored the resultset to a list. 我正在从数据库中获取所有数据,并将结果集存储到列表中。 but could not fetch all data. 但无法获取所有数据。 i want to store the data in a dropdownlist. 我想将数据存储在下拉列表中。 My code is bellow. 我的代码在下面。

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 code is in bellow. 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>

The problem is that you are not closing the Connection and ResultSet when you get an exception. 问题是您遇到异常时没有关闭 ConnectionResultSet And so the database has exhausted all open cursors . 这样数据库就耗尽了所有打开的游标

You need to close everything you open, it means statement, resultsets. 您需要关闭打开的所有内容,这意味着语句和结果集。 You do that in the finally part of your try/catch to ensure things are correctly closed. 您可以在try/catchfinally部分执行此操作,以确保正确关闭内容。

When you close a statement, the resultset linked to that statement is closed too. 当您关闭一条语句时,链接到该语句的结果集也会被关闭。

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);
}

Besides, you should read the docs of PlayFramework ( here for Play2 ) there are database stuff to avoid using ResultSet s and Statement s directly, dealing with higher structures like domain objects, the framework will do the rest for you. 此外,您应该阅读PlayFramework的文档( 此处是Play2的文档),其中有一些数据库内容可避免直接使用ResultSetStatement ,从而处理诸如域对象之类的较高结构,其余的工作将由框架来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM