JSP分页的一些问题,运行不报错,但是每一页只显示一条数据,点下一页直接跳到第四条。
intpageSize=3;introwCount=0;intpageCount=0;publicintgetPageCount(){Connectionconn=nul...
int pageSize = 3;
int rowCount = 0;
int pageCount = 0;
public int getPageCount(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = ConnDB.getConn();
stmt= ConnDB.createStatement(conn);
String sql ="select count(*) from users";
rs = ConnDB.executeQuery(stmt, sql);
if(rs.next()){
rowCount=rs.getInt(1);
}
if(rowCount%pageSize==0){
pageCount=rowCount/pageSize;
}else{
pageCount=rowCount/pageSize+1;
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnDB.Close(rs);
ConnDB.Close(stmt);
ConnDB.Close(conn);
}
return pageCount;
}
public ArrayList getUsersByPage(int pageNow){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList al =new ArrayList();
int startPos = pageSize*(pageNow-1);
try{
conn = ConnDB.getConn();
stmt = ConnDB.createStatement(conn);
String sql = "select * from users limit "+startPos+","+ pageSize;
rs = ConnDB.executeQuery(stmt, sql);
if(rs.next()){
UserBean ub = new UserBean();
ub.setUserId(rs.getInt(1));
ub.setUsername(rs.getString(2));
ub.setPassword(rs.getString(3));
ub.setEmail(rs.getString(4));
ub.setGrade(rs.getInt(5));
al.add(ub);
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnDB.Close(rs);
ConnDB.Close(stmt);
ConnDB.Close(conn);
}
return al;
}
这里是一个逻辑处理的代码,
<%
int pageNow = 1;
String s_pageNow=request.getParameter("pageNow");
if(s_pageNow!=null){
pageNow=Integer.parseInt(s_pageNow);
}
UserBeanCl ubc =new UserBeanCl();
ArrayList a= ubc.getUsersByPage(pageNow);
%>
<table border="1">
<tr>
<td>用户id</td>
<td>用户名字</td>
<td>用户密码</td>
<td>用户电子邮件</td>
<td>用户级别</td>
</tr>
<%
for(int i =0;i<a.size();i++){
UserBean ub =(UserBean)a.get(i);
%>
<tr>
<td><%=ub.getUserId()%></td>
<td><%=ub.getUsername()%></td>
<td><%=ub.getPassword()%></td>
<td><%=ub.getEmail()%></td>
<td><%=ub.getGrade()%></td>
</tr>
<%
}
%>
</table>
<%
out.println("<a href=wel.jsp?pageNow="+1+">首页</a>");
if(pageNow!=1){
out.println("<a href=wel.jsp?pageNow="+(pageNow-1)+">上一页</a>");
}
int pageCount = ubc.getPageCount();
for(int i =1;i<=pageCount;i++){
out.println("<a href=wel.jsp?pageNow="+i+">["+i+"]</a>");
}
if(pageNow!=pageCount){
out.println("<a href=wel.jsp?pageNow="+(pageNow+1)+">下一页</a>");
}
out.println("<a href=wel.jsp?pageNow="+pageCount+">尾页</a>");
%>
这里是一个jps页面。 展开
int rowCount = 0;
int pageCount = 0;
public int getPageCount(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = ConnDB.getConn();
stmt= ConnDB.createStatement(conn);
String sql ="select count(*) from users";
rs = ConnDB.executeQuery(stmt, sql);
if(rs.next()){
rowCount=rs.getInt(1);
}
if(rowCount%pageSize==0){
pageCount=rowCount/pageSize;
}else{
pageCount=rowCount/pageSize+1;
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnDB.Close(rs);
ConnDB.Close(stmt);
ConnDB.Close(conn);
}
return pageCount;
}
public ArrayList getUsersByPage(int pageNow){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList al =new ArrayList();
int startPos = pageSize*(pageNow-1);
try{
conn = ConnDB.getConn();
stmt = ConnDB.createStatement(conn);
String sql = "select * from users limit "+startPos+","+ pageSize;
rs = ConnDB.executeQuery(stmt, sql);
if(rs.next()){
UserBean ub = new UserBean();
ub.setUserId(rs.getInt(1));
ub.setUsername(rs.getString(2));
ub.setPassword(rs.getString(3));
ub.setEmail(rs.getString(4));
ub.setGrade(rs.getInt(5));
al.add(ub);
}
}catch(Exception e){
e.printStackTrace();
}finally{
ConnDB.Close(rs);
ConnDB.Close(stmt);
ConnDB.Close(conn);
}
return al;
}
这里是一个逻辑处理的代码,
<%
int pageNow = 1;
String s_pageNow=request.getParameter("pageNow");
if(s_pageNow!=null){
pageNow=Integer.parseInt(s_pageNow);
}
UserBeanCl ubc =new UserBeanCl();
ArrayList a= ubc.getUsersByPage(pageNow);
%>
<table border="1">
<tr>
<td>用户id</td>
<td>用户名字</td>
<td>用户密码</td>
<td>用户电子邮件</td>
<td>用户级别</td>
</tr>
<%
for(int i =0;i<a.size();i++){
UserBean ub =(UserBean)a.get(i);
%>
<tr>
<td><%=ub.getUserId()%></td>
<td><%=ub.getUsername()%></td>
<td><%=ub.getPassword()%></td>
<td><%=ub.getEmail()%></td>
<td><%=ub.getGrade()%></td>
</tr>
<%
}
%>
</table>
<%
out.println("<a href=wel.jsp?pageNow="+1+">首页</a>");
if(pageNow!=1){
out.println("<a href=wel.jsp?pageNow="+(pageNow-1)+">上一页</a>");
}
int pageCount = ubc.getPageCount();
for(int i =1;i<=pageCount;i++){
out.println("<a href=wel.jsp?pageNow="+i+">["+i+"]</a>");
}
if(pageNow!=pageCount){
out.println("<a href=wel.jsp?pageNow="+(pageNow+1)+">下一页</a>");
}
out.println("<a href=wel.jsp?pageNow="+pageCount+">尾页</a>");
%>
这里是一个jps页面。 展开
TableDI
2024-07-18 广告
2024-07-18 广告
**Excel表格中的COUNTIF函数**在Excel表格中,`COUNTIF`函数是一个强大的工具,用于统计满足特定条件的单元格数量。这个函数的基本语法是`=COUNTIF(range, criteria)`,其中`range`表示要统...
点击进入详情页
本回答由TableDI提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询