数据查询while(rs.next)错误
学习jsp 写在线书店的查询的时候出错了 不知道怎么办了
这个是现实错误代码org.apache.jasper.JasperException: An exception occurred processing JSP page /search.jsp at line 62
59: myPage.pageId=1;
60: Vector idList=new Vector();
61: getPage="1";
62: while(rs.next())
63: {
64: myPage.recordCount++;
65: if(myPage.recordCount==myPage.pageSize)
源文件代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<SCRIPT language=JavaScirpt src="JSLib.js" type="text/javascript"></SCRIPT>
<SCRIPT language=JavaScript>
function checkform(Frm){
var flag=ture;
if(isInt(Frm.pageId.value))
{
alert("用户名不能为空!");
Frm.pageId.focus();
flag=false;
}
retrun flag;
}
function detail(a)
{
windows.open("detail.jsp?pid="+a,"body","toolbar=no,left=150,top=20,width=480,height=350,meaubar=no,systemMenu=no");
}
function add(a)
{
windows.open("add.jsp?pid="+a,"body","toobar=no,left=150,top=20,width=300,height=50,menubar=no,systemMenu=no");
}
</script>
<%@ page import="java.sql.*" %>
<%@ page import="page.Page"%>
<jsp:useBean id="db" scope="page" class="opendb.opendb"/>
<%
String getPage=request.getParameter("pageId").trim();
if(getPage.compareTo("-1")==0)
{
Page myPage=new Page();
String bookname=request.getParameter("bookname").trim();
String author=request.getParameter("author").trim();
String nameSelect="";
String authorSelect="";
if(bookname.compareTo("")!=0) nameSelect="bookname like'%"+bookname+"%'";
if(author.compareTo("")!=0) authorSelect="author like'%"+author+"%'";
byte[] tmpbyte=nameSelect.getBytes("ISO8859_1");
nameSelect=new String(tmpbyte);
tmpbyte=authorSelect.getBytes("ISO8859_1");
authorSelect=new String(tmpbyte);
if(authorSelect.compareTo("")!=0&&nameSelect.compareTo("")!=0)nameSelect="and"+nameSelect;
String sql="select pid frombook where";
sql=sql+authorSelect+nameSelect+"order by pid desc";
session.putValue("authorSelect", authorSelect);
session.putValue("nameSelect", nameSelect);
ResultSet rs=db.executeQuery(sql);
myPage.recordCount=0;
myPage.pageSize=10;
myPage.pageCount=0;
myPage.pageId=1;
Vector idList=new Vector();
getPage="1";
while(rs.next())
{
myPage.recordCount++;
if(myPage.recordCount==myPage.pageSize)
{
myPage.pageCount++;
idList.addElement(rs.getString("pid"));
myPage.recordCount=0;
}
}
if(myPage.recordCount!=0) myPage.pageCount++;
myPage.recordCount+=(myPage.pageCount-1)*myPage.pageSize;
rs.close();
session.putValue("idList",idList);
session.putValue("myPage",myPage);
}
%>
<head>
<title>用户查询结果</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<style type="text/css">
<!--import"css/jsp.css";-->
</style>
<body bgcolor="#FFFFFF">
<%boolean pageError=false;
Page myPage=(Page)session.getValue("myPage");
if(myPage.recordCount<=0)
{ %>对不起,没有您需要的商品,请扩大查询范围!<%}
else
{
int pageId=myPage.pageId;
if(getPage!=null&&getPage!="") pageId=Integer.parseInt(getPage.trim());
if(pageId<1 ||pageId>myPage.pageCount)
{
pageId=myPage.pageId;
pageError=true;
}
myPage.pageId=pageId;
Vector idList=(Vector)session.getValue("idList");
String getList="";
if(pageId>1)
{
getList=(String) idList.elementAt(pageId-2);
getList="and pid<"+getList;
}
String authorSelect=(String)session.getValue("authorSelect");
String nameSelect=(String)session.getValue("nameSelect");
String sql="select top"+myPage.pageSize+"from book where";
sql=sql+authorSelect+nameSelect+getList+"order by pid desc";
ResultSet rs=db.executeQuery(sql);
%><br>
<table width="540" border="0">
<tr>
<td width="25%"><b><font color="#FF0000">◎</font></b>共有记录<b>
<font color="#FF0000"><%=myPage.recordCount%></font></b>条</td>
<td width="30%"><%if(pageError){%>您输入的页码无效<%}%></td>
<td width="45%">共有<b><font color="#FF0000"><%=myPage.pageCount %>
</font></b>页,现在显示的事第
<b><font color="#FF0000"><%=myPage.pageId %></font></b>页
</td>
</tr>
</table>
<table width="540" border="0">
<tr bgcolor="#ff5d26">
<td width="42">
<div align="center"><font color="#FFFFFF"><b>记录是</b></font></div>
</td>
<td width="133">
<div align="center"><font color="#FFFFFF">书名</font></div>
</td>
<td width="113">
<div align="center"><font color="#FFFFFF"><b>作者</b></font></div>
<td width="126" bgcolor="#ff5d26">
<div align="center"><font color="#FFFFFF"><b>单价</b></font></div>
</td>
<td width ="104" bgcolor="#fff5d26">
<div align="center"><font color="#FFFFFF"><b>放入购物车</b></font></div>
</td>
</tr>
<%
String pid;
String bookname;
String author;
String price;
int wsh=0;
while(rs.next())
{
wsh++;
pid=rs.getString("pid");
bookname=rs.getString("bookname");
author=rs.getString("author");
price=rs.getString("price");
%>
<tr bgcolor="#ffd6c8">
<td width="42" height="13">
<div align="center"><%=wsh+myPage.pageSize*(pageId-1)%></div>
</td>
<td width="133" height="13">
<div align="center"><a href="javascript:detail(<%=pid%>);"><%=bookname%></a></div>
</td>
<td width="133" height="13">
<div align="center"><%=author%></div>
</td>
<td width="126" bgcolor="#ffd6c8" height="13">
<div align="center">¥<%=price%></div>
</td>
<td width="104" bgcolor="#ffd6c8" height="13">
<div align="center"><a href="javascript:add(<%=pid%>);">购买</a></div>
</td>
</tr>
<%}
rs.close();
%>
</table>
<table width="540" border="0">
<tr>
<td width="133"> </td>
<td width="397">
<form name="form1" action="search.jsp" method="post" onsubmit="return checkform(this)">
<%
if(pageId!=1)
{
%>
<a href="search.jsp?pageId=1">第一页</a>|<a href="search,jsp?pageId=<%=pageId-1%>">上一页</a>|<%}else{%>
第一页|上一页|
<%}
if(pageId!=myPage.pageCount)
{%>
<a href="search.jsp?pageId=<%=pageId+1%>">下一页</a>|
<a href="search.jsp?pageId=<%=myPage.pageCount%>">最后一页</a>
<%}else{%>
下一页|最后一个
<%}%>
输入页码 :
<input type="text" name="PageId" size="5">
页数 : <b><font color="#FF0000"><%=myPage.pageId%></font></b>/
<b><font color="#FF0000"><%=myPage.pageCount%></font></b>
</form>
</td>
</tr>
</table>
<%} %>
</body>
</html>
[解决办法]
页面抛出异常后点击右键查看源代码,然后找到62行,如果是while(rs.next())这一行,那很大可能rs是null。
[解决办法]
String sql="select pid frombook where"; from 后面有空格 大哥
[解决办法]
ResultSet rs=db.executeQuery(sql);
下面输出一下
System.out.print(rs);
先看看值是不是为null
[解决办法]
你把你的 sql语句在数据库里面执行一下 看看能不能查出来东西吧
[解决办法]