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的值也不为空,不知道为什么原因。请各位朋友帮帮忙!谢谢!
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的值也不为空,不知道为什么原因。请各位朋友帮帮忙!谢谢!
解决方案 »
- JSP做的注册页面怎么才能让第一次输入的密码跟第二次的密码相同才能注册呢
- 在jsp中传值(十万火急)有合适的提示就结贴
- SVN连接问题 Authentication realm:'<https://210.30.12.19:443>Subversion Repositories'
- 谢谢大家帮我解决很多问题,但这个问题很重要,帮帮我如何在关闭浏揽器的时候从HashMap中将一个元素删除呢?
- 关于图片预览问题,IE6好用,IE7不好使,为什么?
- 部署发布一个简单的例子后发生的问题???!!!
- 哪位大哥再帮我看看显示时钟的例子
- 关于用beans访问数据库--驱动程序问题
- 如何通过jsp调用系统命令?
- ssh2中的登录验证发生的错误
- 关于struts 标签的IF的问题
- jsp如何 操作mysql中datetime的数据类型
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 来接受喝发送
if(rs != null){
rs.next();
我想这样写比较好。如果你的结果集没有任何记录呢?
if(rs!=null && rs.next()){
主要解决出现异常原因:
首先你要确认你的数据库中的数据有没有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"));
}
楼主试试
next()方法返回值是boolean类型的。
boolean java.sql.ResultSet.next() throws SQLException
rs.next();
换成while(rs.next()){
......
}试试看
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语句中确认返回的唯一一条数据
......
}
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()方法时,移向下一个数据,就不能取到数据。感谢各位朋友给的建议。
我觉得判断这么写“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";
}