Illegal operation on empty result set.
java菜鸟,
遇到一个问题请大家指教,感激不尽!
[code=Java][/code]
问题:
javax.servlet.ServletException:
java.sql.SQLException: Illegal operation on empty result set.
源代码如下:
<%@ page language="java" import="java.util.*"
import="java.sql.Connection" import="java.sql.DriverManager"
import="java.sql.Statement" import="java.sql.ResultSet"
pageEncoding="gb2312"%>
<%@ page language="java" contentType="text/html;charset=GBK"%>
<html>
<head>
<title>修改图书信息</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?","root","cxl");
Statement stmt=conn.createStatement();
String id=request.getParameter("id");
ResultSet rs=stmt.executeQuery("select * from bookinfo where id="+id);
rs.next();
%>
<form action="/update.jsp" method="post">
<table>
<caption>
修改图书信息
</caption>
<tr>
<th width="30%">
书名
</th>
<td width="30%">
<input name="book" type="text" value="<%rs.getStrin(2);%>">
</td>
<th width="30%">
价格:
</th>
<td width="30%">
<input name="book" type="text" value="<%rs.getStrin(3);%>">
元
</td>
</tr>
<tr>
<th colspan="2">
<input type="hidden" name="id" value="<%=id%>">
<input type="submit" value="修改">
<input type="reset" value="重置">
</th>
</tr>
</table>
</form>
<%
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
------解决方案--------------------你的查询结果没有记录,所以rs.getString(2)出错了。
------解决方案--------------------
在对rs进行拆分之前,先进行判断,看是否有内容。
------解决方案--------------------while(rs.hasNext()){
//对rs进行拆分
}