在java.sql异常关闭ResultSet后不允许执行操作

时间:2018-07-04 13:53:26

标签: java sql jdbc

我正在使用jersey创建简单的rest api,在这里我将从数据库中获取的所有数据分配给变量,然后将这些变量数据作为列表返回,但出现错误。我看过其他示例,但我不明白,这是我做的错:

public class UserDAOImpl implements UserDAO{
public List<User> readUser() {
    List<User> list = new ArrayList();
    User u = new User();
    try {
    System.out.println("inside readuser impl");
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb", "root", "");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select * from user");
        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String address = rs.getString("address");
            u.setId(id);
            u.setName(name);
            u.setAddress(address);
            System.out.println(u);
            list.add(u); //adding user object to list
            con.close();
        }
    } catch (Exception e) {
        System.out.println(e);
    }
    return list; //returning to list for json response
 }
}

这是我的控制器,在获取对象后会给出json响应

@Path("/rest")
public class UserController {

@GET
@Path("/getall")
@Produces(MediaType.APPLICATION_JSON)
public List<User> getAllUser(){
    UserDAO udao = new UserDAOImpl();

    return udao.readUser();
 }

}

1 个答案:

答案 0 :(得分:1)

您将在一次迭代后关闭连接,这将导致在第二次迭代时因javasql异常而关闭ResultSet后不允许操作,将con.close移出循环

public class UserDAOImpl implements UserDAO{
 public List<User> readUser() {
   List<User> list = new ArrayList();
   User u = new User();
   try {
        System.out.println("inside readuser impl");
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = 
        DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb", "root", "");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from user");
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String address = rs.getString("address");
                u.setId(id);
                u.setName(name);
                u.setAddress(address);
                System.out.println(u);
                list.add(u); //adding user object to list
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        con.close();
        return list; //returning to list for json response
     }
    }

甚至您可以对资源使用try,它将自动关闭所有资源

 public class UserDAOImpl implements UserDAO{
    public List<User> readUser() {
        List<User> list = new ArrayList();
        User u = new User();
        try( Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb", "root", ""); 
        Statement stmt = con.createStatement();ResultSet rs = stmt.executeQuery("select * from user");){
        System.out.println("inside readuser impl");
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String address = rs.getString("address");
                u.setId(id);
                u.setName(name);
                u.setAddress(address);
                System.out.println(u);
                list.add(u); //adding user object to list
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return list; //returning to list for json response
        }
     }

我没有测试第二台计算机,因为我的计算机中没有环境