|
Java学习者论坛
<%
//定义四个分页会用到的变量
int pageSize=3;
int pageNow=1;//默认显示第一页
int rowCount=0;//该值从数据库中查询
int pageCount=0;//该值通过pageSize和cowCount得到
//查询得到rowCount;
//1.加载驅动
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
//2.得到链接
Connection ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=spdb2","sa","123456");
//3.创建Statement
Statement sm=ct.createStatement();
//4.查询
ResultSet rs=sm.executeQuery("select count(*) from users ");
//一定要有rs.next()
if(rs.next())
{
rowCount=rs.getInt(1);
}
//计算pageCount,算法很多
if(rowCount%pageSize==0)
{
pageCount=rowCount/pageSize;
}else
{
pageCount=rowCount/pageSize+1;
}
//查询需要显示的记录
rs=sm.executeQuery("select top "+pageSize
+" * from users where userId not in (select top "
pageSize*(pageNow-1)" userId from users) ");
//显示
%>
<table border="1">
<tr><td>用户id</td><td>用户名</td><td>密码</td><td>电邮</td><td>级別</td></tr>
<%
while(rs.next())
{
%>
<tr><td><%=rs.getInt(1) %></td><td><%=rs.getString(2) %></td>
<td><%=rs.getString(3) %></td><td><%=rs.getString(4) %></td><td><%=rs.getInt(5) %></td></tr>
<%
}
%>
</table>
<%
%>
欢迎来到Java学习者论坛,转载请注明地址:http://www.javaxxz.com. |
|