例外——java.sql。SQLException:关闭ResultSet后不允许进行操作

时间:2021-03-20 22:52:12

I am getting an exception like as

我有个例外

java.sql.SQLException: Before start of result set when calling getInt() method  

My code is as follows:::

我的代码如下:

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

Exception comes after line

异常后线

//========================== resource_id is an integer value

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

1 个解决方案

#1


3  

This line

这条线

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

must be replaced with?

必须更换?

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

#1


3  

This line

这条线

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

must be replaced with?

必须更换?

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