java.sql.SQLException:mysql中结果集错误结束后

时间:2019-05-06 07:51:48

标签: java mysql

我试图从数据库中读取数据并将ResultSet存储在ArrayList中。但是,我在打印数组列表时收到错误java.sql.SQLException: After end of result set。我检查了是否没有根据此link两次致电result.next()。有人有更好的方法来存储结果集吗?然后我可以在所需的位置将其打印出来。

Statement stmt=con.createStatement();  
            ResultSet rsVehicle=stmt.executeQuery("select * from vehicle");  
            while(rsVehicle.next()) {  
                vehicleList.add(rsVehicle);
            }
            System.out.println(vehicleList.size());

            for(int i=0;i<vehicleList.size();i++) {
                System.out.println(vehicleList.get(i).getInt(1)+"  "+vehicleList.get(i).getString(2)+"  "+vehicleList.get(i).getString(3)+"  "+
                        vehicleList.get(i).getString(4)+"  "+vehicleList.get(i).getString(5)+"  "+vehicleList.get(i).getString(6)+"  "+
                        vehicleList.get(i).getString(7)+"  "+vehicleList.get(i).getString(8)+"  "+vehicleList.get(i).getString(9)+"  "+
                        vehicleList.get(i).getString(10)+"  "+vehicleList.get(i).getString(11)+"  "+vehicleList.get(i).getString(12)+"  "+
                        vehicleList.get(i).getString(13)+"  "+vehicleList.get(i).getString(14)+"  "+vehicleList.get(i).getString(15));
            }

            PreparedStatement ps;
            ps =con.prepareStatement("SELECT * from job WHERE date = ?");
            ps.setString(1, date);
            ResultSet rsJob=ps.executeQuery();  
            while(rsJob.next()) {  
                jobList.add(rsJob);
            }
            System.out.println(jobList.size());

            for(int i=0;i<jobList.size();i++) {
                System.out.println(jobList.get(i).getInt(1)+"  "+jobList.get(i).getString(2)+"  "+jobList.get(i).getString(3)+"  "+
                        jobList.get(i).getString(4)+"  "+jobList.get(i).getString(5)+"  "+jobList.get(i).getString(6)+"  "+
                        jobList.get(i).getString(7)+"  "+jobList.get(i).getString(8)+"  "+jobList.get(i).getString(9)+"  "+
                        jobList.get(i).getString(10)+"  "+jobList.get(i).getString(11)+"  "+jobList.get(i).getString(12)+"  "+
                        jobList.get(i).getString(13)+"  "+jobList.get(i).getString(14)+"  "+jobList.get(i).getString(15));
            }

1 个答案:

答案 0 :(得分:3)

在您的代码中,将结果集添加到列表中,然后遍历该列表以获取数据,但是正确的流程是:

  • 从结果集中提取数据并将其放在列表中。
  • 可以迭代列表以打印结果之后。

代码应类似于:

// Create a new list of vehicles
List<Vehicle> vehicleList = new ArrayList<>();
Statement stmt=con.createStatement();  
ResultSet rsVehicle=stmt.executeQuery("select * from vehicle");  
while(rsVehicle.next()) {  
    // Create a new vehicle for each record returned from the query
    Vehicle vehicle = new Vehicle();

    // Set to the vehicle each property taken from that record
    vehicle.setBrand(rsVehicle.getString(1));
    ...

    // Add that vehicle to the list
    vehicleList.add(vehicle);
}

...

// Iterate the list and print each field you need
for (Vehicle vehicle: vehicleList) {
   // PRint each vehicle
   System.out.println("Vechicle brand: " + vehicle.getBrand());
}

注意:如果您重新定义Vehicle类的方法toString,则可以直接调用System.out.println(vehicle);