回 帖 发 新 帖 刷新版面

主题:菜鸟求救啊 ~~在线等

本人刚刚学JSP,昨天碰到一个问题,就是我想在一个页面内显示10条数据,是做成一个表格形式,多于10条就可以页面上下翻,而且可以显示当前第几页,各位大哥帮帮忙啊,我用的是SQLSERVER数据库

回复列表 (共1个回复)

沙发

这是一个分页的问题,下面是一个简单的显示,界面你再修改修改,试试可以不。


<%

int intPageCount; //初始页数
int intPageSize; //初始页面大小
int intRowCount; //初始记录数
int intPage; //初始页号
int i, j;
String pageno = request.getParameter("page");
if (pageno == null) {
    intPage = 1;
}
else {
    intPage = Integer.parseInt(pageno);
    if (intPage < 1) {
      intPage = 1;
    }
}

Connection conn1 = DBConnection.getConnection();
ResultSet rs1 = null;
String sql1 = "select count(*) from test";
Statement stmt1 = conn1.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs1 = stmt1.executeQuery(sql1);
rs1.next();
intRowCount = rs1.getInt(1);
rs1.close();
stmt1.close();

intPageSize = 10;
intPageCount = (intRowCount + intPageSize - 1) / intPageSize;
if (intPage > intPageCount) {
intPage = intPageCount;
}
%>

<%
Connection conn2 = DBConnection.getConnection();
ResultSet rs2 = null;
String sql2 = "select bianhao,mingcheng,miaoshu from test";
Statement stmt2 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs2 = stmt2.executeQuery(sql2);
i = (intPage - 1)*intPageSize;
for (j = 0; j < i; j++) {
rs2.next();
}

%>
//此表显示数据
<table>
<tr>
<td>编号</td>
<td>名称</td>
<td>描述</td>
</tr>
<%while i<intPageSize && rs2.next(){%>
<tr>
<td><%=rs2.getString("bianhao")%></td>
<td><%=rs2.getString("mingcheng")%></td>
<td><%=rs2.getString("miaoshu")%></td>
</tr>
<%
    }
    rs2.close();
    stmt2.close();
    conn2.close();
%>
</table>

//此表作用分页
<table>
<tr>
<td>
页次:<%=intPage%>  <%=intPageCount%>  每页 <%=intPageSize%> 数据项共 <%=intRowCount%>  个
</td>
<td>
<a href="test.jsp?page=1">9</a>
<a href="test.jsp?page=<%=(intPage-1)%>">7</a>
<%
j = intPageCount;
if (j > 6) j = 6;
for (i = 1; i <= j; i++) {
%>
<a href="alarm.jsp?page=<%=(i)%>[
<%=i%>            ]
</a>
<%
}
conn1.close();
%>
<a title=下一页 href="test.jsp?page=<%=(intPage+1)%>">8</a>
<a title=最后页 href="test.jsp?page=<%=intPageCount%>">:</a>
</td>
</tr>
</table>

有些其他的简单代码你添加一下就可以
呵呵

我来回复

您尚未登录,请登录后再回复。点此登录或注册