servlet中doPost()方法:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");

final String sql = "select * from BookList where isbn=?";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String address = "unknownBook.jsp";

BookBean book = new BookBean();
try{
conn = DBUtil.getConnection();
stmt = conn.prepareStatement(sql);
stmt.setString(1, request.getParameter("isbn"));
rs = stmt.executeQuery();

if(rs != null){
rs.next();
System.out.print((rs==null));
book.setIsbn(rs.getString("isbn"));
book.setTitle(rs.getString("title"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("author"));
book.setPrice(rs.getInt("price"));
address = "displayBook.jsp";
}

}catch(SQLException e){
e.printStackTrace();
}
DBUtil.closeResultSet(rs);
DBUtil.closeConnection(conn);
request.setAttribute("bookBean", book);
request.getRequestDispatcher(address).forward(request, response);
}
在执行到"book.setIsbn(rs.getString",就出现异常“用尽的 Resultset”,此时在数据库里已近手动插入了三组数据,rs的值也不为空,不知道为什么原因。请各位朋友帮帮忙!谢谢!

解决方案 »

  1.   

    public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException { 
    this.doPost(request,response); 关键字THIS 没有写!

    inal String sql = "select * from BookList where isbn=?"; 
    Connection conn = null; 
    PreparedStatement stmt = null; 
    ResultSet rs = null; 
    String address = "unknownBook.jsp"; BookBean book = new BookBean(); 
    try{ 
    conn = DBUtil.getConnection(); 
    stmt = conn.prepareStatement(sql); 
    stmt.setString(1, request.getParameter("isbn")); 
    rs = stmt.executeQuery(); if(rs != null){ 
    rs.next(); 
    System.out.print((rs==null)); 
    book.setIsbn(rs.getString("isbn")); 
    book.setTitle(rs.getString("title")); 
    book.setAuthor(rs.getString("author")); 
    book.setPublisher(rs.getString("author")); 
    book.setPrice(rs.getInt("price")); 
    address = "displayBook.jsp"; 
    } }catch(SQLException e){ 
    e.printStackTrace(); 

    DBUtil.closeResultSet(rs); 
    DBUtil.closeConnection(conn); 
    request.setAttribute("bookBean", book); 
    request.getRequestDispatcher(address).forward(request, response); 

    这里的代码姑且语法上是对的,但是 位置不是写在这里的,应该写在你定义的另外一个类中,比如定义一个Dao类,把这些写在dao类的方法里面  在servlet中调用这个方法 得到相关的实例,然后再用  request  response 来接受喝发送
      

  2.   

    你的写法
    if(rs != null){ 
    rs.next(); 
    我想这样写比较好。如果你的结果集没有任何记录呢?
    if(rs!=null && rs.next()){
      

  3.   

    也许rs不为null,但可能没有记录,所以3楼的建议应试试~~
      

  4.   

    代码的封装先不说了,不封装没有什么问题。
    主要解决出现异常原因:
    首先你要确认你的数据库中的数据有没有where isbn=“isbn” 的数据
    if(rs.next() != null){ 
       book.setIsbn(rs.getString("isbn")); 
       book.setTitle(rs.getString("title")); 
       book.setAuthor(rs.getString("author")); 
       book.setPublisher(rs.getString("author")); 
       book.setPrice(rs.getInt("price")); 
       address = "displayBook.jsp"; 
       
      //这里做一下测试看代码是否执行到这里,同时检查数据
      System.out.println(rs.getString("author")); 

    楼主试试
      

  5.   


    next()方法返回值是boolean类型的。
    boolean java.sql.ResultSet.next() throws SQLException
      

  6.   

    把  if(rs != null){
         rs.next(); 
    换成while(rs.next()){
    ......
    }试试看
      

  7.   

    ResultSet因为即使是没有数据,也不为空。
      

  8.   

    不好意思啊!失误
    if(rs.next()){ 
      book.setIsbn(rs.getString("isbn")); 
      book.setTitle(rs.getString("title")); 
      book.setAuthor(rs.getString("author")); 
      book.setPublisher(rs.getString("author")); 
      book.setPrice(rs.getInt("price")); 
      address = "displayBook.jsp"; 
      
      //这里做一下测试看代码是否执行到这里,同时检查数据 
      System.out.println(rs.getString("author")); 

    这样就可以了,为什么不用while()是因为楼主在sql语句中确认返回的唯一一条数据
      

  9.   

    if(rs != null && rs.next()){
         ...... 
    }
      

  10.   

    BookBean book = new BookBean(); 
    try{ 
    conn = DBUtil.getConnection(); 
    stmt = conn.prepareStatement(sql); 
    stmt.setString(1, request.getParameter("isbn")); 
    rs = stmt.executeQuery(); if(rs != null){ 
    rs.next(); 
    System.out.print((rs==null)); 
    book.setIsbn(rs.getString("isbn")); 
    book.setTitle(rs.getString("title")); 
    book.setAuthor(rs.getString("author")); 
    book.setPublisher(rs.getString("author")); 
    book.setPrice(rs.getInt("price")); 
    address = "displayBook.jsp"; 

    各位朋友,已经知道为什么出现“用尽的 Resultset”结果集rs并有得到数据,所以在调用next()方法时,移向下一个数据,就不能取到数据。感谢各位朋友给的建议。
      

  11.   


    我觉得判断这么写“rs!=null && rs.next()”是正确的,但是不明白“也许rs不为null,但可能没有记录”我不明白,因为我没有碰到过这样的例子,不知道能不能举个例子呢?还有我觉得这里是while循环而不应该是if判断这样:while(rs != null){ 
    rs.next(); 
    System.out.print((rs==null)); 
    book.setIsbn(rs.getString("isbn")); 
    book.setTitle(rs.getString("title")); 
    book.setAuthor(rs.getString("author")); 
    book.setPublisher(rs.getString("author")); 
    book.setPrice(rs.getInt("price")); 
    address = "displayBook.jsp"; 

    或者while(rs != null&&rs.next()){ 
    System.out.print((rs==null)); 
    book.setIsbn(rs.getString("isbn")); 
    book.setTitle(rs.getString("title")); 
    book.setAuthor(rs.getString("author")); 
    book.setPublisher(rs.getString("author")); 
    book.setPrice(rs.getInt("price")); 
    address = "displayBook.jsp";