异常 - java.sql.SQLException:ResultSet关闭后不允许操作

时间:2013-02-15 10:00:25

标签: java mysql sqlexception

我得到像

这样的例外
java.sql.SQLException: Before start of result set when calling getInt() method  

我的代码如下:::

public void setdateTOSource(){
   try{
        String q = "SELECT * FROM ipp.resource;";
        ResultSet result = DB_Access.getData(q);
        System.out.println("Size result set"+result.getFetchSize());
        while(result.next()){
            getIpSource().add(result.getString(1)); 
            System.out.println(result.getString(1));

        }
       System.out.println("source size "+getIpSource().size());      
   }
   catch(Exception e){
       System.out.println(e);
   }
}
public void insertTargetToDb(List<String> targetValue){

    try{
        Connection con = DB_Connect.getDataBaseConnection();
        Statement statement1 = con.createStatement();

        int val=0;
        String q1 = "SELECT MAX(area_id) as aa FROM ipp.area;";
        ResultSet resultset = statement1.executeQuery(q1); 
        if(resultset.next()){
            val = resultset.getInt(1);
         };           
        val++;

        String q2 = "INSERT INTO ipp.area VALUES("+val+",'"+getAreaName()+"',1);";
        Statement statement2 = con.createStatement();
        statement1.executeUpdate(q2);
        for( int y=0;y<targetValue.size();y++){

           System.out.println("FS");

           String ip=targetValue.get(y);
           System.out.println("ip" +ip);
           String q3 ="SELECT resource_id FROM ipp.resource r where ip_address like '"+ip+"%';";
           Statement statement3 = con.createStatement();
           ResultSet resultset3 =statement3.executeQuery(q3);

           System.out.println("FS1");
//           ResultSet result1 =DB_Access.getData(q3);
           System.out.println("FS2");
           System.out.println("result set"+resultset3.getFetchSize());
 //====================================================================              
while(resultset3.next()){
                setId(resultset.getInt(1));
          }           
          System.out.println("result :........."+resultset3.getInt("resource_id"));
          System.out.println("FS3");

           String q4 = "INSERT INTO ipp.used_resource VALUES("+val+","+getId()+");";
           Statement statement4= con.createStatement();
           statement4.executeUpdate(q4);
//           DB_Access.setData(q3);
           statement3.close();
           statement4.close();
//           resultset3.close();
        }
        resultset.close();
        statement1.close();
        statement2.close();
        con.close();
        addMessage("Area created Successfully");


    }
    catch(Exception e){
        System.out.println(e);
    }
}

后出现例外情况
  

// ==========================   resource_id是一个整数值

1 个答案:

答案 0 :(得分:3)

这一行

while(resultset3.next()){
                setId(resultset.getInt(1));
          } 

必须替换为?

while(resultset3.next()){
                setId(resultset3.getInt(1));
          }