JSP分页程序出错?高分拜求答案!!
pageCount.jsp
<%
int PageSize=3; //定义每页要显示的记录数目
int ShowPage=1; //定义将要显示的页数
int RowCount=0; //定义总的记录数
int PageCount=0; //定义要显示的总的页数
Connection conn=null; //定义连接对象
Statement stmt=null;
ResultSet rs=null;
//执行数据库与相关数据的初始化
public void jspInit()
{
try
{
//载入驱动程序类别
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//建立数据库连接
conn=DriverManager.getConnection("jdbc:odbc:sa");
//建立Statement对象,并设置游标类型为可前后移动
stmt=conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery("select * from young");
//将游标移至最后一笔记录
rs.last();
RowCount=rs.getRow();
PageCount=((RowCount%PageSize)==0?(RowCount/PageSize):((RowCount/PageSize)+1) );
}
catch (Exception e)
{
System.err.print(e.toString());
}
}
public void jspDestroy()
{
try
{
//rs.close();
stmt.close();
conn.close();
}
catch (Exception ex)
{
System.out.print(ex.toString());
}
}
%>
pageC.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
<%@ include file="pageCount.jsp"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>分页显示</title>
</head>
<body>
<%
String ToPage=request.getParameter("ToPage");
if (ToPage!=null)
{
ShowPage=Integer.parseInt(ToPage);
if(ShowPage>PageCount)
{
ShowPage=PageCount;
}
else if (ShowPage<=0)
{
ShowPage=1;
}
}
rs.absolute((ShowPage-1)*PageSize+1);
%>
<h3>当前在第<%=ShowPage%>页 共<%=PageCount%>页</h3>
<br>
<%
for (int i=1;i<=PageSize;i++)
{
%>
<table width="350" border="0" align="center" cellpadding="0" cellspacing="4">
<tr>
<td><%//=rs.getInt(id)%></td>
<td>用户名:</td>
<td><%=rs.getString("userid")%></td>
<td>密码:</td>
<td><%=rs.getString("pwd")%></td>
<td>电子邮件:</td>
<td><%=rs.getString("email")%></td>
</tr>
</table>
<%
if (!rs.next())
{
break;
}
}
%>
<table>
<tr valign="baseline" align="center">
<%
if (ShowPage!=PageCount)
{
%>
<td width="150">
<a href="pageC.jsp?ToPage="<%=ShowPage+1%>>下一页</a>
</td>
<td width="150">
<a href="pageC.jsp?ToPage="<%=PageCount%>>最后一页</a>
</td>
<%
}
%>
<td width="150">
<form method="post" name="frm1" action="pageC.jsp">
<input type="text" name="ToPage" size="4" value="<%=ShowPage%>">页
</form>
</td>
</tr>
</table>
</body>
</html>
Error:org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 1 in the jsp file: /bbs/bbs/page/pageCount.jsp
Generated servlet error:
C:\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\bbs\bbs\page\pageC_jsp.java:59: illegal start of expression
public void jspInit()
^
1 error
参考答案:pageCount.jsp
中是不是只有方法名而没有类名啊,若没有类最好加一个类
成员名和方法名都用public吧
public class jspinit
{
public int PageSize=3;
..........
public jspinit() //构造函数
{
....................//功能只要加载驱动
}
public ResultSet sqlQuery()
{
try{
.......//完成查询操作
}
catch(Exception e)
{out.println("*******");}
return rs;
}
public void jspDestroy(){}
}
再用到类的页面先建一个类的对象
jspinit classObject = new jspinit();
ResultSet rs = classObject.sqlQuery();
...........................
祝你成功啊.....