一个实现分页的小程序的错误

Renjiachun 2004-05-08 11:55:43
本人做了一个实现分也的小程序,老是出现错误,麻烦各位帮我分析一下原因:
源码:
<%@page contentType="text/hmtl;charset=gb2312"%>
<%@page import="java.sql.*"%>
<html>
<head>
<title>
JSP分页实例
</title>
</head>
<body bgcolor="pink">
<%
int i;
int ipage=5;//每页的条数
int curpage=1;//当前页
int allpage;//总页数

String url="jdbc.odbc.Northwind";
String user="sa";
String passWord="";
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
Statement stmt=null;
Connection conn=null;
ResultSet pagers=null;
ResultSetMetaData rsmd=null;
try{
Class.forName(driver).newInstance();
conn=DriverManager.getConnection(url,user,"");
stmt=conn.createStatement();
String sql="select count(ProductsID) as A from products;";
pagers=stmt.executeQuery(sql);
}
catch(ClassNotFoundException e){
out.println("ClassNotFoundException");
}
catch(SQLException e){
out.println("SQLException");
}
catch(Exception e) {
out.println("an unkonwn exception occured");
}
int page1=pagers.getInt("A");//获得查询的记录数
int allCol=page1/ipage;
allpage=(int)Math.ceil((allCol+ipage-1)/ipage);//计算出总的页数
curpage=(int)Integer.parseInt(request.getParameter("gotopage"));
if(curpage>allpage)
{
curpage=1;
}
int spage=(curpage-1)*ipage;//查询的起始记录数
String sql2="select * from products where ProductID between"+spage+"and"+curpage*ipage;
ResultSet rs1=stmt.executeQuery(sql);
%>
<table width="740" border="2" cellspacing="2" cellpadding="2" align="center">
<tr>
<th>ProductID</th>
<th>ProductName</th>
<th>SupplierID</th>
<th>CategoryID</th>
</tr>

<%
while(rs1.next()){
int ProductID=rs1.getInt("ProductID");
String ProductName=rs1.getString("ProductName");
int SupplierID=rs1.getInt("SupplierID");
int CategoryID=rs1.getInt(" CategoryID");

%>
<tr>
<td><%=ProductID%></td>
<td><%=ProductName%></td>
<td><%=SupplierID%></td>
<td><%=CategoryID%></td>
</tr>

<%
}
%>
</table>
共<%=allpage%>页
共<%=ipage%>条记录

当前是<%=curpage%>页
last page <A:href="page.jsp?curpage=<%=(curpage-1)%>"></A>
next page <A:href="page.jsp?curpage=<%=(curpage+1)%>"></A>

<form name="form1" method="get" action="page.jsp">
go to
<input type="text" name="gotopage" value="" size=5>
</form>

</body>
</html>
错误如下:
java.lang.NullPointerException
at org.apache.jsp.page$jsp._jspService(page$jsp.java:99)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:201)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:381)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:473)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:190)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2347)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:468)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1027)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1125)
at java.lang.Thread.run(Thread.java:536)

...全文
17 7 点赞 打赏 收藏 举报
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zjgj 2004-05-11
String sql="select count(ProductsID) as A from products";
是取行数吧?那你ResultSet rs1=stmt.executeQuery(sql); 去得rs1后
while(rs1.next()){
int ProductID=rs1.getInt("ProductID");
String ProductName=rs1.getString("ProductName");
int SupplierID=rs1.getInt("SupplierID");
int CategoryID=rs1.getInt(" CategoryID");
}
%>除了ProductID其他你怎么可能得到呢?
改成这样试试String sql="select count(*) as A from products";
  • 打赏
  • 举报
回复
shadow7788 2004-05-09
对SQL后面没分号,
顺便看下你的数据库连接对没有。看别人的代码还是头痛
  • 打赏
  • 举报
回复
ybsoft 2004-05-09
String sql="select count(ProductsID) as A from products;";


sql语句里面不需要分号
  • 打赏
  • 举报
回复
Renjiachun 2004-05-09
我照着楼上各位改了两处:
一:
String midString=request.getParameter("gotopage");
if(midString==null){
curpage=1;
}
else{
curpage=new Integer(midString).intValue();
}
二:
<%if(curpage>1){%>
last page <A:href="page.jsp?curpage=<%=(curpage-1)%>"></A>
<%
}
%>
<%if(curpage<allpage){%>
next page<A:href="page.jsp?curpage=<%=(curpage+1)%>"></A>
<%
}
%>
结果还是一样的错误

  • 打赏
  • 举报
回复
umbrella1984 2004-05-08
last page <A:href="page.jsp?gotopage=<%=(curpage-1)%>"></A>
next page <A:href="page.jsp?gotopage=<%=(curpage+1)%>"></A>
  • 打赏
  • 举报
回复
edisonkun 2004-05-08
空指针异常,,看看你哪出现了null数据对象
  • 打赏
  • 举报
回复
Vicky020923 2004-05-08
curpage=(int)Integer.parseInt(request.getParameter("gotopage"));

request.getParameter("gotopage");

可能为空

你这错误是某个地方产生空了
  • 打赏
  • 举报
回复
相关推荐
发帖
Web 开发
加入

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2004-05-08 11:55
社区公告
暂无公告