<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="Login"%>
<%@ page import="PageNumber"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="pagenumber" class="PageNumber" scope="session"></jsp:useBean>
<jsp:useBean id="login" class="Login" scope="session"></jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head><body>
<%@ include file="head.txt"%>
<p>图书目录:
<%! Connection con=null;
    public void showList(ResultSet rs,javax.servlet.jsp.JspWriter out,int n,String buybook)
{
 try
   {
         
     out.println("<Table Border>");
 out.print("<tr>");
 out.print("<th width=50>"+"<font size=1>"+"id"+"</font>");
 out.print("<th width=50>"+"<font size=1>"+"订购号"+"</font>");
 out.print("<th width=70>"+"<font size=1>"+"书名"+"</font>");
 out.print("<th width=50>"+"<font size=1>"+"作者"+"</font>");
 out.print("<th width=50>"+"<font size=1>"+"出版社"+"</font>");
 out.print("<th width=50>"+"<font size=1>"+"出版时间"+"</font>");
 out.print("<th width=50>"+"<font size=1>"+"价钱"+"</font>");
 out.print("<th width=50>"+"<font size=1>"+"分类"+"</font>");
 out.print("<th width=50>"+"<font size=1>"+"添加到定单"+"</font>");
 out.print("</tr>");
 
 for(int i=1;i<=n;i++)
  {
    String id=rs.getString(1);
out.print("<tr>");
out.print("<th width=50>"+"<font size=1>"+id+"</font>");
out.print("<th width=50>"+"<font size=1>"+rs.getString(2)+"</font>");
out.print("<th width=70>"+"<font size=1>"+rs.getString(3)+"</font>");
out.print("<th width=60>"+"<font size=1>"+rs.getString(4)+"</font>");
out.print("<th width=60>"+"<font size=1>"+rs.getString(5)+"</font>");
out.print("<th width=50>"+"<font size=1>"+rs.getString(6)+"</font>");
out.print("<th width=20>"+"<font size=1>"+rs.getString(7)+"</font>");
out.print("<th width=50>"+"<font size=1>"+rs.getString(8)+"</font>");

String s1="<form action="+buybook+" method=get>";
String s2="<input type=hidden name=id value="+id+">";
String s3="<input type=submit value=订购></form>";
String s=s1+s2+s3;
out.print("<td>"+s+"</td>");
out.print("</tr>");
rs.next();
}
out.print("</table>");

}
catch(Exception e1)
{
}
}  
 %> 
<% Statement sql=null;
   ResultSet rs=null;
   int rowCount=0;
           
   if(con==null)
   {   
      try
      {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  }
  catch(ClassNotFoundException e2)
  {
    out.print(e2);
  } 
  try
{
con=DriverManager.getConnection("jdbc:odbc:zhang","scott","tiger");

sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("select * from BOOK");
rs.last();
int number=rs.getRow();
rowCount=number;
pagenumber.setPageSize(3);
pagenumber.setPageCount(rowCount,pagenumber.getPageSize());
out.print("共有"+pagenumber.getPageCount()+"页,");
out.print("每页显示"+pagenumber.getPageSize()+"条记录");
}
catch(SQLException e3)
{
  out.print("e3");
  }
  }
else
{
   synchronized(con)
   {
      try
    {
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs.last();
int number=rs.getRow();
rowCount=number;
pagenumber.setPageSize(3);
pagenumber.setPageCount(rowCount,pagenumber.getPageSize());
out.print("共有"+pagenumber.getPageCount()+"页");
        out.print("每页显示"+pagenumber.getPageSize()+"条记录");
}
catch(SQLException e4)
{
out.print(e4);

}
}
}
%>
<% 
  String str=response.encodeURL("showBookList.jsp");
  String buybook=response.encodeURL("buybook.jsp");
  %>
  <form action="<%=str%>" method="post">
  显示下一页:<input type="hidden" name="a" value="next">
  <input type="submit" value="next">
  </form>
  <form action="<%=str%>" method="post">
  显示上一页:<input type="hidden" name="a" value="previous">
  <input type="submit" value="previous">
  
  </form>
  <form action="<%=str%>" method="post">
  输入欲显示的页<input type="text" name="a" value="1">
  <input type="submit" value="submit">
  
  </form>  
  <% String s=request.getParameter("a");
     if(s==null)
 {
   s="1";
   }
   if(s.equals("next"))
   {
 
      int n=pagenumber.getShowPage();
  n=n+1;
if(n>pagenumber.getPageCount())
{
 n=1;
 }
 pagenumber.setShowPage(n);  out.print("目前显示"+pagenumber.getShowPage()+"页");

 rs.absolute((n-1)*pagenumber.getPageSize()+1);
 
 showList(rs,out,pagenumber.getPageSize(),buybook);

 }
 else if(s.equals("previous"))
 {
   int n=pagenumber.getShowPage();
   n=n-1;
   if(n<=0)
   {
     n=pagenumber.getPageCount();
 }
  pagenumber.setShowPage(n);
  out.print("目前显示第"+pagenumber.getShowPage()+"页");
 rs.absolute((n-1)*pagenumber.getPageSize()+1);
 showList(rs,out,pagenumber.getPageSize(),buybook);
 }
else
{
  int m=Integer.parseInt(s);
  pagenumber.setShowPage(m);
  out.print("目前显示第"+pagenumber.getShowPage()+"页");
  int n=pagenumber.getShowPage();
      rs.absolute((n-1)*pagenumber.getPageSize()+1);
  showList(rs,out,pagenumber.getPageSize(),buybook);
}
%>

</body>
</html>
这是我的原代码。可是打开时正常。可一点下一页就出错。
提示
java.lang.NullPointerException
高手快帮帮我啊。明天交不了这门课要挂啊。

解决方案 »

  1.   

    servlet 写的  怎么不用JSP写啊  直接取值 就行了 简单多了
      

  2.   

    楼上的你没看清楚把。这就是JSP写的。

    楼主是不是包倒错了啊。。好象是包名加类名了。。怎么没有啊。。
      

  3.   

    我大致看了一下,你写的太乱了 ,
    首先
    1、rs=sql.executeQuery("select * from BOOK");
    rs.last();执行完之后你应该把指针从新回到开始rs.first();
    2、 for(int i=1;i<=n;i++)循环,最好不要用。
    应该用while( rs.next()&&i<+n)循环。
    仅供参考。
      

  4.   

    搂主
    这样写你觉得累吗
         out.println("<Table Border>");
     out.print("<tr>");
     out.print("<th width=50>"+"<font size=1>"+"id"+"</font>");
     out.print("<th width=50>"+"<font size=1>"+"订购号"+"</font>");
     out.print("<th width=70>"+"<font size=1>"+"书名"+"</font>");
     out.print("<th width=50>"+"<font size=1>"+"作者"+"</font>");
     out.print("<th width=50>"+"<font size=1>"+"出版社"+"</font>");
     out.print("<th width=50>"+"<font size=1>"+"出版时间"+"</font>");
     out.print("<th width=50>"+"<font size=1>"+"价钱"+"</font>");
     out.print("<th width=50>"+"<font size=1>"+"分类"+"</font>");
     out.print("<th width=50>"+"<font size=1>"+"添加到定单"+"</font>");
     out.print("</tr>");直接写出来不行啊