int dipage=1;//当前页码默认值
String pages=request.getParameter("dipage");
if(pages==null)
{
pages="1";
}
try
{
dipage=Integer.parseInt(pages);
}
catch(Exception e)
{
dipage=1;
}
int countRecord=0;//纪录条数
int countPageRecord=0; //每页记录数
int countPage=0;//总页数
countPageRecord=15;//设置每页记录数
rs.last();
countRecord=rs.getRow();
if(countRecord/countPageRecord==0)
countPage=countRecord/countPageRecord;
else
countPage=countRecord/countPageRecord+1;
if((dipage-1)*countPageRecord==0)
rs.beforeFirst();
else
rs.absolute((dipage-1)*countPageRecord);
if(rs.next()==false)
{
out.print("没有符合条件的");
out.print(sql);
} else{
rs.beforeFirst();
out.print("<table width=96% border=1 align=center cellpadding=0 cellspacing=0 bordercolorlight=#215594 bordercolordark=#FFFFFF>");
out.print("<tr>");
out.print("<td width=18% height=25 background=images/BG2.gif>学号</td>");
out.print("<td width=15% height=25 background=images/BG2.gif>姓名</td>");
out.print("<td width=35% height=25 background=images/BG2.gif>学院</td>");
out.print("<td width=12% height=25 background=images/BG2.gif>是否投保</td>");
out.print("<td width=20% height=25 background=images/BG2.gif>查看</td>");
out.print("</tr>");
int i=0;
String no="";
while(rs.next()){
no=rs.getString("number");
out.print("<tr>");
out.print("<td height=25>"+no+"</td>");
out.print("<td height=25>"+rs.getString(4)+"</td>");
out.print("<td height=25>"+rs.getString(1)+"</td>");
out.print("<td height=25>"+rs.getString(8)+"</td>");
out.print("<td height=25>");
out.print("<a href=teacher_find_2.jsp?number="+no+">详细信息</a>");
out.print("</td>");
out.print("</tr>");
i++;
if(i>=countPageRecord) break;
}
out.print("<tr><td colspan=8 align=center>");
out.print("共"+countRecord+"条记录,共"+countPage+"页,当前第"+dipage+"页,每页"+countPageRecord+"条记录,");
if(dipage==1);//首页
else//当前不是首页
{
out.print("<a href=teacher_find_fy.jsp?dipage=1>首页</a>,");
out.print("<a href=teacher_find_fy.jsp?dipage="+(dipage-1)+">上一页</a>,");
}
if(dipage==countPage);//是末页
else
{
out.print("<a href=teacher_find_fy.jsp?dipage="+(dipage+1)+">下一页</a>,");
out.print("<a href=teacher_find_fy.jsp?dipage="+countPage+">末页</a>,");
}
String pages=request.getParameter("dipage");
if(pages==null)
{
pages="1";
}
try
{
dipage=Integer.parseInt(pages);
}
catch(Exception e)
{
dipage=1;
}
int countRecord=0;//纪录条数
int countPageRecord=0; //每页记录数
int countPage=0;//总页数
countPageRecord=15;//设置每页记录数
rs.last();
countRecord=rs.getRow();
if(countRecord/countPageRecord==0)
countPage=countRecord/countPageRecord;
else
countPage=countRecord/countPageRecord+1;
if((dipage-1)*countPageRecord==0)
rs.beforeFirst();
else
rs.absolute((dipage-1)*countPageRecord);
if(rs.next()==false)
{
out.print("没有符合条件的");
out.print(sql);
} else{
rs.beforeFirst();
out.print("<table width=96% border=1 align=center cellpadding=0 cellspacing=0 bordercolorlight=#215594 bordercolordark=#FFFFFF>");
out.print("<tr>");
out.print("<td width=18% height=25 background=images/BG2.gif>学号</td>");
out.print("<td width=15% height=25 background=images/BG2.gif>姓名</td>");
out.print("<td width=35% height=25 background=images/BG2.gif>学院</td>");
out.print("<td width=12% height=25 background=images/BG2.gif>是否投保</td>");
out.print("<td width=20% height=25 background=images/BG2.gif>查看</td>");
out.print("</tr>");
int i=0;
String no="";
while(rs.next()){
no=rs.getString("number");
out.print("<tr>");
out.print("<td height=25>"+no+"</td>");
out.print("<td height=25>"+rs.getString(4)+"</td>");
out.print("<td height=25>"+rs.getString(1)+"</td>");
out.print("<td height=25>"+rs.getString(8)+"</td>");
out.print("<td height=25>");
out.print("<a href=teacher_find_2.jsp?number="+no+">详细信息</a>");
out.print("</td>");
out.print("</tr>");
i++;
if(i>=countPageRecord) break;
}
out.print("<tr><td colspan=8 align=center>");
out.print("共"+countRecord+"条记录,共"+countPage+"页,当前第"+dipage+"页,每页"+countPageRecord+"条记录,");
if(dipage==1);//首页
else//当前不是首页
{
out.print("<a href=teacher_find_fy.jsp?dipage=1>首页</a>,");
out.print("<a href=teacher_find_fy.jsp?dipage="+(dipage-1)+">上一页</a>,");
}
if(dipage==countPage);//是末页
else
{
out.print("<a href=teacher_find_fy.jsp?dipage="+(dipage+1)+">下一页</a>,");
out.print("<a href=teacher_find_fy.jsp?dipage="+countPage+">末页</a>,");
}
{
dipage=1;
}
问题在这里,肯定是出错了catch(Exception e)
{
dipage=1;
out.print(e....);
}
int countRecord=0;//纪录条数
int countPageRecord=0; //每页记录数
int countPage=0;//总页数
countPageRecord=15;//设置每页记录数
rs.last();
countRecord=rs.getRow();
if(countRecord/countPageRecord==0)
countPage=countRecord/countPageRecord;
else
countPage=countRecord/countPageRecord+1;
String receive_page=request.getParameter("id");
int dipage=1;//当前页码数默认为1
if(receive_page==null)
{
dipage=1;
}
else
{
dipage=Integer.parseInt(receive_page);
if(dipage<=0)
dipage=1;
}
if(dipage>countPage)
{
dipage=countPage;
}
if((dipage-1)*countPageRecord==0)
rs.beforeFirst();
else
rs.absolute((dipage-1)*countPageRecord+1);
/*if(rs.next()==false)
{
out.print("没有符合条件的");
out.print(sql);
}else{ */
out.print("<table width=96% border=1 align=center cellpadding=0 cellspacing=0 bordercolorlight=#215594 bordercolordark=#FFFFFF>");
out.print("<tr>");
out.print("<td width=18% height=25 background=images/BG2.gif>学号</td>");
out.print("<td width=15% height=25 background=images/BG2.gif>姓名</td>");
out.print("<td width=35% height=25 background=images/BG2.gif>学院</td>");
out.print("<td width=12% height=25 background=images/BG2.gif>是否投保</td>");
out.print("<td width=20% height=25 background=images/BG2.gif>查看</td>");
out.print("<td>"+receive_page+"</td>");
out.print("</tr>");
int i=0;
String no="";
while(rs.next()){
no=rs.getString("number");
out.print("<tr>");
out.print("<td height=25>"+no+"</td>");
out.print("<td height=25>"+rs.getString(4)+"</td>");
out.print("<td height=25>"+rs.getString(1)+"</td>");
out.print("<td height=25>"+rs.getString(8)+"</td>");
out.print("<td height=25>");
out.print("<a href=teacher_find_2.jsp?number="+no+">详细信息</a>");
out.print("</td>");
out.print("</tr>");
i++;
if(i>=countPageRecord) break;
}
out.print("<tr><td colspan=8 align=center>");
out.print("共"+countRecord+"条记录,共"+countPage+"页,当前第"+dipage+"页,每页"+countPageRecord+"条记录,");
if(dipage==1);//首页
else//当前不是首页
{
out.print("<a href=teacher_find_fy.jsp?id=1>首页</a>,");
out.print("<a href=teacher_find_fy.jsp?id="+(dipage-1)+">上一页</a>,");
}
if(dipage==countPage);//是末页
else
{
out.print("<a href=teacher_find_fy.jsp?id="+(dipage+1)+">下一页</a>,");
out.print("<a href=teacher_find_fy.jsp?id="+countPage+">末页</a>,");
}
{
out.print("没有符合条件的");
out.print(sql);
}else{
rs.beforeFirst();
这里错了 rs.beforeFirst()是把指针再移到第一行 你把指针再移到第一行干什么? 这样当然出现空指针异常了 把上面整段去掉就可以了还有
if(countRecord/countPageRecord==0)
countPage=countRecord/countPageRecord;
else
countPage=countRecord/countPageRecord+1;
可以改成 countPage=(countRecord+countPageRecord-1)/countPageRecord;
这样更简洁点
完成代码:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %><%!
public String codeToString(String str)
{//处理中文字符串的函数。
String s=str;
try
{
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
}
catch(Exception e)
{
return s;
}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<form id="form1" name="form1" method="post" action="Untitled-1.jsp">
<label>
<input type="text" name="textfield" />
<input type="submit" name="Submit" value="提交" />
</label>
</form>
<p> </p>
<p>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
out.print(e);
}
try{
String url = "jdbc:odbc:bx";
Connection conn = DriverManager.getConnection(url,"","");
%>
<%
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String s=codeToString(request.getParameter("textfield"));
String sql="select * from student_info where name like'%"+s.trim()+"%'";
ResultSet rs = stmt.executeQuery(sql);
int countRecord=0;//纪录条数
int countPageRecord=0; //每页记录数
int countPage=0;//总页数
countPageRecord=15;//设置每页记录数
rs.last();
countRecord=rs.getRow();
if(countRecord%countPageRecord==0)
countPage=countRecord/countPageRecord;
else
countPage=countRecord/countPageRecord+1;
String receive_page=request.getParameter("id");
int dipage=1;//当前页码数默认为1
if(receive_page==null)
{
dipage=1;
}
else
{
dipage=Integer.parseInt(receive_page);
if(dipage<=0)
dipage=1;
}
if(dipage>countPage)
{
dipage=countPage;
}
if((dipage-1)*countPageRecord==0)
rs.beforeFirst();
else
rs.absolute((dipage-1)*countPageRecord+1);
/*if(rs.next()==false)
{
out.print("没有符合条件的");
out.print(sql);
}else{ */
out.print("<table width=96% border=1 align=center cellpadding=0 cellspacing=0 bordercolorlight=#215594 bordercolordark=#FFFFFF>");
out.print("<tr>");
out.print("<td width=18% height=25 background=images/BG2.gif>学号</td>");
out.print("<td width=15% height=25 background=images/BG2.gif>姓名</td>");
out.print("<td width=35% height=25 background=images/BG2.gif>学院</td>");
out.print("<td width=12% height=25 background=images/BG2.gif>是否投保</td>");
out.print("<td width=20% height=25 background=images/BG2.gif>查看</td>");
out.print("<td>"+dipage+"</td>");
out.print("</tr>");
int i=0;
String no="";
while(rs.next()){
no=rs.getString("number");
out.print("<tr>");
out.print("<td height=25>"+no+"</td>");
out.print("<td height=25>"+rs.getString(4)+"</td>");
out.print("<td height=25>"+rs.getString(1)+"</td>");
out.print("<td height=25>"+rs.getString(8)+"</td>");
out.print("<td height=25>");
out.print("<a href=teacher_find_2.jsp?number="+no+">详细信息</a>");
out.print("</td>");
out.print("</tr>");
i++;
if(i>=countPageRecord) break;
}
out.print("<tr><td colspan=8 align=center>");
out.print("共"+countRecord+"条记录,共"+countPage+"页,当前第"+dipage+"页,每页"+countPageRecord+"条记录,");
if(dipage==1);//首页
else//当前不是首页
{
out.print("<a href=Untitled-1.jsp?id=1>首页</a>");
out.print("<a href=Untitled-1.jsp?id="+(dipage-1)+">上一页</a>");
}
if(dipage==countPage);//是末页
else
{
out.print("<a href=Untitled-1.jsp?id="+(dipage+1)+">下一页</a>");
out.print("<a href=Untitled-1.jsp?id="+countPage+">末页</a>");
} out.print("</td></tr>");
out.print("</table>");
%>
<%
conn.close();
}
catch(Exception ex){
out.print(ex);
}
%>
</p>
</body>
</html>
当我点击下一页时错误提示:java.lang.NullPointerException
out.print("<a href=Untitled-1.jsp?id="+(dipage-1)+">上一页</a>");
}
if(dipage==countPage);//是末页
else
{
out.print("<a href=Untitled-1.jsp?id="+(dipage+1)+">下一页</a>");
out.print("<a href=Untitled-1.jsp?id="+countPage+">末页</a>");
把id改为dipage
out.print("<a href=Untitled-1.jsp?id="+(dipage+1)+"&&textfield=<%=s%>>下一页</a>");
out.print("<a href=Untitled-1.jsp?id="+countPage+"textfield=<%=s%>>末页</a>");
所有的翻页都改成这样,应该就没问题了