jsp分页显示记录的问题!困扰我三天的问题!!【用了三种方法,结果都是空指针!why??】急用!!明天结贴,请各位帮帮忙!!谢谢!!!

stars_of_leo 2003-08-28 07:56:51
同题!

以下是源码:
package beans;

public class PageHandle
{
private final int PAGE_SIZE = 16;//页面大小,每页记录数
private int page = 1;//第几页
private int pageCount = 1;//总页数
private int rowCount = 1;//总记录数

public PageHandle()
{

}

public int getPAGE_SIZE()
{
return PAGE_SIZE;
}

public void setPageCount(int r)
{//计算总页数
pageCount = (PAGE_SIZE + rowCount -1) / PAGE_SIZE;
}
public int getPageCount()
{
return pageCount;
}
public void setPage(int p)
{
page = p;
}
public int getPage()
{
return page;
}
public void setRowCount(int c)
{
rowCount = c;
}
public int getRowCount()
{
return rowCount;
}
}
...全文
360 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wkliangsdqd 2003-09-18
  • 打赏
  • 举报
回复
学习积极,真的有不少的好想法!
noratong 2003-08-29
  • 打赏
  • 举报
回复
看一下别人的编的代码和你的有什么不用????
这里有别人编好的代码,也是分页显示的:
http://www.cnjsp.org/topic/read.jsp
stars_of_leo 2003-08-29
  • 打赏
  • 举报
回复
to bruni(不如你):
我的代码总共只有不到200行,所以我也很郁闷,哪儿找那第280行去?
to LingFengNB(凌枫):
我的数据库连接是没有问题的,因为在加分页显示功能前程序运行一切正常。
to lmh7607(海风):
我的数据库使用的oracle,那个方法是没有问题的。我这样用是因为我们在别人的
平台上作二次开发,他们超牛,把resultset的很多方法都重写过,功能全封死了,只能
用sql语言实现。:( 还有谢谢你的提醒,以后我一定多多注意!^_^b
to xinshou1979330(Success.net):
多谢兄台!:)
xinshou1979330 2003-08-29
  • 打赏
  • 举报
回复
************java part****************
package msg;

public class SplitPager {
private int totalRecords = 0;//总记录数
private int pageSize = 0;//每页显示条数
private int currentPage = 1;//当前页号
private int start = 0;//每页显示的开始记录的编号
private int end = 0;//每页显示的的最后记录的编号

public SplitPager() {
}

//根据总记录数,每页显示条数,及当前页号构造分页器
public SplitPager(int totalRecords, int pageSize, int currentPage) {
this.totalRecords = totalRecords;
this.pageSize = pageSize;
if(currentPage <= 0)
this.currentPage = 1;
else
this.currentPage = currentPage;

this.start = (this.currentPage-1)*pageSize;//设置当前页的开始记录编号

if(currentPage*pageSize >= totalRecords)
this.end = totalRecords;
else
this.end = currentPage*pageSize;//设置当前页的最后记录编号
}

//获得当前页的开始记录编号
public int getStartIndex(){
return start;
}

//获得当前页的最后记录编号
public int getEndIndex(){
return end;
}

/**
*@para pageName 当前页的文件名
*@para paraName 显示分页的参数,类似page =1 ,page =2
*@para othersNameAndParaPairs 可能要用到的其它参数 ,类似abc=a&bcd=b等
*@return 返回html字符串
*/
public String splitPage( String pageName,
String paraName,
String othersNameAndParaPairs){
int pageCount;
String firstPageUrl, lastPageUrl, prePageUrl, nextPageUrl,pageStatus,recordStatus;
recordStatus = "每页"+Integer.toString(pageSize)+"条/共"+Integer.toString(totalRecords)+"条";

if(totalRecords < 1){
pageStatus = "第1页/共1页";
firstPageUrl ="第一页";
prePageUrl = "上一页";
nextPageUrl = "下一页";
lastPageUrl = "最后一页";
return recordStatus+" "+pageStatus+" "+firstPageUrl+" "+prePageUrl+" "+nextPageUrl+" "+lastPageUrl;
}

if(othersNameAndParaPairs==null)
othersNameAndParaPairs ="";
else
othersNameAndParaPairs ="&"+othersNameAndParaPairs;

pageCount = (totalRecords % pageSize > 0) ? (totalRecords / pageSize + 1) :
(totalRecords / pageSize);
pageStatus = "第" + Integer.toString(currentPage) + "页/共" +
Integer.toString(pageCount) + "页";
if (currentPage == 1) { //当前就是第一页,则第一页和前一页无连接
firstPageUrl = "第一页";
prePageUrl = "上一页";
}
else {
firstPageUrl = "<a href=\""+pageName+"?"+paraName+"=1"+othersNameAndParaPairs+"\""+">第一页</a>";
prePageUrl = "<a href=\""+pageName+"?"+paraName+"="+
Integer.toString(currentPage - 1)+othersNameAndParaPairs+"\""+">上一页</a>";
}
if (currentPage == pageCount) { //当前就是最后一页,则最后一页和后一页无连接
lastPageUrl = "最后一页";
nextPageUrl = "下一页";
}
else {
lastPageUrl = "<a href=\""+pageName+"?"+paraName+"="+
Integer.toString(pageCount) +othersNameAndParaPairs+"\""+">最后一页</a>";
nextPageUrl = "<a href=\""+pageName+"?"+paraName+"="+
Integer.toString(currentPage + 1) +othersNameAndParaPairs+"\""+">下一页</a>";
}

//构造跳转列表的字符串
StringBuffer select = new StringBuffer();
select.append("<select onChange=\"location.href= this.options[this.options.selectedIndex].value\">\n");
for(int i=1; i<=pageCount;i++){
select.append("<option value=\"");
select.append(pageName).append("?").append(paraName).append("=");
select.append(i).append(othersNameAndParaPairs).append("\"");
if(i==currentPage) select.append("selected");
select.append(">").append(i).append("</option>");
}
select.append("</select>");
return recordStatus+" "+pageStatus+" "+firstPageUrl+" "+prePageUrl+" "+nextPageUrl+" "+lastPageUrl+" 跳转至 "+ select+" 页";
}

//单元测试
public static void main(String arsg[]) {
SplitPager sp = new SplitPager(100,20,2);
System.out.println(sp.splitPage("list.jsp","page","&name=good"));

}

}
********************jsp part**********************
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import = "msg.SplitPager"%>
<html>
<head>test</head>
<body>
<%
//构造测试数据
int[] record = new int[100];
for(int i=0; i<record.length; i++)
record[i] = i;

%>

<table width="100%" border="1" align="center">
<%
//分页显示
int currentPage=1;//当前页号,初始值
if(request.getParameter("page")!=null){//更改页号时,从参数中获得当前页号
currentPage= Integer.parseInt(request.getParameter("page"));
}
//初始化分页器,每页显示10条
SplitPager sp = new SplitPager(record.length, 10, currentPage);
//分页显示列表
for(int i=sp.getStartIndex(); i<sp.getEndIndex(); i++){%>

<tr>
<td><%=record[i]%></td>
</tr>
<%}%>


<%--分页,设置参数,并输出html--%>
<%=sp.splitPage("test.jsp","page",null)%>
</table>
</body>
</html>

这是前两天在这里找到的一个朋友写的源码,借以为楼主参考
lmh7607 2003-08-28
  • 打赏
  • 举报
回复
select count(*), * from mytable
这语句能通过吗???,在sqlserver里是不可以的.

另外想劝告你一句:
你的代码实在是太乱了,如果你是在公司,想想以后别人怎么维护吧。



wsstar 2003-08-28
  • 打赏
  • 举报
回复
gz
xinshou1979330 2003-08-28
  • 打赏
  • 举报
回复
帮楼主顶
我也在研究此问题不过是在 JAVA里
由JSP调用
LingFengNB 2003-08-28
  • 打赏
  • 举报
回复
我觉得你是数据库根本没有连接上,空指针应该是指的你的连接为空,你可以先单独试一下你的数据库是否连接成功,这样问题就好解决,一般的句子不会出什么问题,连接这里很容易出问题,建议你写个简单的查询语句,检查你的数据库是否连接成功
bruni 2003-08-28
  • 打赏
  • 举报
回复
(_0002fwt_0002faviation_0005f_00036_00033_00031_0002fcims_0002fobjmanage_0002fjsp_0002fShowByPage_0002ejspShowByPage_jsp_0.java:280)
Open this java file, and go to line 280.
Hope you can solve this problem soon.
stars_of_leo 2003-08-28
  • 打赏
  • 举报
回复
以下是错误提示:
Error: 500
Location: jsp/ShowByPage.jsp
Internal Servlet Error:

javax.servlet.ServletException
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:459)
at wt.aviation_0005f_00036_00033_00031.cims.objmanage.jsp._0002fwt_0002faviation_0005f_00036_00033_00031_0002fcims_0002fobjmanage_0002fjsp_0002fShowByPage_0002ejspShowByPage_jsp_0._jspService(_0002fwt_0002faviation_0005f_00036_00033_00031_0002fcims_0002fobjmanage_0002fjsp_0002fShowByPage_0002ejspShowByPage_jsp_0.java:297)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:177)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:391)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at org.apache.tomcat.service.connector.Ajp13ConnectionHandler.processConnection(Ajp13ConnectionHandler.java:160)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:484)

Root cause:
java.lang.NullPointerException
at wt.aviation_0005f_00036_00033_00031.cims.objmanage.jsp._0002fwt_0002faviation_0005f_00036_00033_00031_0002fcims_0002fobjmanage_0002fjsp_0002fShowByPage_0002ejspShowByPage_jsp_0._jspService(_0002fwt_0002faviation_0005f_00036_00033_00031_0002fcims_0002fobjmanage_0002fjsp_0002fShowByPage_0002ejspShowByPage_jsp_0.java:280)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:177)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:391)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:404)
at org.apache.tomcat.core.Handler.service(Handler.java:286)
at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)
at org.apache.tomcat.service.connector.Ajp13ConnectionHandler.processConnection(Ajp13ConnectionHandler.java:160)
at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
at java.lang.Thread.run(Thread.java:484)


stars_of_leo 2003-08-28
  • 打赏
  • 举报
回复
<!--
bcwti
Copyright (c) 2003 Doraemon, all rights reserved.
ecwti
-->
<%@ page import="java.sql.*" %>
<%@ page import="java.io.* "%>
<%@ page import="oracle.sql.*" %>
<%@ page import="oracle.jdbc.oracore.Util" %>
<%@ page import="oracle.jdbc.driver.*" %>
<%@ page import="beans.PageHandle" %>
<jsp:useBean id="handle" class="beans.PageHandle" scope="page" >
</jsp:useBean>
<HTML>
<BODY bgcolor=cyan><Size=1>
<%!
//声明一个共享的连接对象
Connection con = null;
//显示数据库记录的方法
public StringBuffer showList(ResultSet rs, int pageSize)
{
StringBuffer buffer = new StringBuffer();
try
{
buffer.append("<TABLE width=100% border=2>");
buffer.append("<TR>");
buffer.append("<TH width="+"10%"+">" + "序 号" + "</TH>");
buffer.append("<TH width="+"35%"+">" + "姓 名" + "</TH>");
buffer.append("<TH width="+"15%"+">" + "成 绩" + "</TH>");
buffer.append("<TH width="+"40%"+">" + "备 注" + "</TH>");
buffer.append("</TR>");


int no;
String name;
int dis;
String note;

for(int i = 1; i < pageSize; i++)
{
buffer.append("<TR>");
no = rs.getInt("no");
buffer.append("<TD width="+"10%"+">" + no + "</TD>");
name = rs.getString("name");
buffer.append("<TD width="+"35%"+">" + name + "</TD>");
dis = rs.getInt("discribe");
buffer.append("<TD width="+"15%"+">" + dis + "</TD>");
note = rs.getString("note");
buffer.append("<TD width="+"40%"+">" + note + "</TD>");
buffer.append("</TR>");
rs.next();
}
buffer.append("</TABLE>");
rs.close();
return buffer;
}
catch(SQLException showErr)
{
System.out.println("ShowByPage-->showList(): " + showErr);
return new StringBuffer("");
}
}
%>
<%
Statement sql = null;
ResultSet rs = null;
int rowCount = 0;

//第一个进来的用户负责连接数据库
if(con == null)
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
}
catch(SQLException divErr)
{
System.out.println("ShowByPage-->database driver: " + divErr);
}
try
{
con = DriverManager.getConnection("jdbc:oracle:thin:@server:1521:mm", "my", "my");
con.setAutoCommit(false);
System.out.println("ShowPage: success connect");
sql = con.createStatement();
rs = sql.executeQuery("select count(*), * from mytable");
rowCount = rs.getInt("count(*)");
handle.setPageCount(rowCount);
}
catch(SQLException conErr)
{
System.out.println("ShowByPage-->database connection: " + conErr);
}
}
else
{//其他用户同步
synchronized(con)
{
try
{
sql = con.createStatement();
rs = sql.executeQuery("select count(*), * from mytable");
rowCount = rs.getInt("count(*)");
handle.setPageCount(rowCount);
}
catch(SQLException synErr)
{
System.out.println("ShowByPage-->synchronized connection: " + synErr);
}
}
}
%>
<%
String str = response.encodeRedirectURL("ShowByPage.jsp");
%>
<Table>
<TR>
<Form action="<%=str%>" method="post">
<Input Type="hidden" name="change" value="Next">
<Input type=submit value="Next">
</Form>
<Form action="<%=str%>" method="post">
<Input Type="hidden" name="change" value="Previous">
<Input type=submit value="Previous">
</Form>
<Form action="<%=str%>" method="post">
GOTO:<Input Type="text" name="change" value="Next">
<Input type=submit value="Next">
</Form>
</TR>
</Table>
<%
//获取表单提交信息
StringBuffer stringbuffer = new StringBuffer();
String strPage = request.getParameter("change");
if(strPage == null)
{
strPage = "1";
}
if(strPage.equals("Next"))
{//下一页
int intPage = handle.getPage();
intPage = intPage + 1;
if(intPage > handle.getPageCount())
{
intPage = handle.getPageCount();
}
handle.setPage(intPage);
rs.absolute((intPage - 1) * handle.getPAGE_SIZE() + 1);
stringbuffer = showList(rs, handle.getPAGE_SIZE());
}
else if(strPage.equals("Previous"))
{//上一页
int intPage = handle.getPage();
intPage = intPage - 1;
if(intPage < 1)
{
intPage = 1;
}
handle.setPage(intPage);
rs.absolute((intPage - 1) * handle.getPAGE_SIZE() + 1);
stringbuffer = showList(rs, handle.getPAGE_SIZE());
}
else
{//跳转
int gotoPage = Integer.parseInt(strPage);
handle.setPage(gotoPage);
int intPage = handle.getPage();
rs.absolute((intPage - 1) * handle.getPAGE_SIZE() + 1);
stringbuffer = showList(rs, handle.getPAGE_SIZE());
}
%>
<%=stringbuffer%>
</BODY>
</HTML>

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧