我的第一个jsp分页代码
jsp文件:pagedata.jsp //显示数据
javabean:DataConn.java //连接数据库
javabean:GetCount.java //获得数据记录数
DataConn.java文件内容:
package aliang_org;
import java.sql.*;
public class DataConn{
public static Connection getDataConn() {
Connection conn = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=your","your","your");
}
catch(Exception ex) {
ex.printStackTrace();
}
return conn;
}
}
GetCount .java文件内容:
package aliang_org;
import java.sql.*;
import java.util.*;
import java.io.*;
public class GetCount {
private Connection conn;
public GetCount()throws Exception{
conn = DataConn.getDataConn();
}
public int getCounter()throws Exception{
int ret = 0;
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select count(*) from UserInfos");
while(rset.next()){
ret = rset.getInt(1);
}
return ret;
}
}
pagedata.jsp 文件内容:
<%@ page contentType="text/html; charset=gb2312" language="java"%>
<%@ page import = "aliang_org.*"%>
<%@ page import = "java.util.*"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.sql.*"%>
<html>
<head>
<title>
jsp/JavaBean分页列出数据
</title>
</head>
<body bgcolor="#ffffff">
<h1>jsp/JavaBean分页列出数据</h1>
<hr />
<jsp:useBean id="dataConnection" class="aliang_org.DataConn" scope="page" />
<jsp:useBean id="getcounter" class="aliang_org.GetCount" scope="page" />
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr align="center" bgcolor="#00FFFF">
<td>ID序号</td>
<td>用户名</td>
<td>密码</td>
<td>电子信箱</td>
<td colspan="2">操作选项</td>
</tr>
<%
int nowPages;//当前页
int pages;//请求页数
int countPerPage = 10 ; //每页显示条数
int pageCount;//总页数
int recordCount = getcounter.getCounter(); //recordCount为总记录数
pageCount = (int)Math.ceil((recordCount + countPerPage-1) / countPerPage);//算出总页数
//得到请求页
if(request.getParameter("pages") == null){
pages = 1;
}else{
pages = new Integer(request.getParameter("pages")).intValue();
}
//得到当前实际页面
if(pages > pageCount){
nowPages = 1;
}else{
nowPages = pages;
}
//获取记录集
Connection conn = dataConnection.getDataConn();
ResultSet rs = conn.createStatement().executeQuery("Select top "+ countPerPage +" * from UserInfos where ID not in (select top "+ (nowPages-1)*countPerPage +" ID from UserInfos order by id desc) order by ID desc");
while(rs.next()){
int ID = rs.getInt("ID");
String username = rs.getString("username");
String password = rs.getString("password");
String email = rs.getString("email");
%>
<tr>
<td align="center"><%= ID %></td>
<td><%= username %></td>
<td><%= password %></td>
<td><%= email %></td>
<td align="center"><a href="modify.jsp?ID=<%= ID %>">修改</a></td>
<td align="center"><a href="delete.jspID=<%= ID %>">删除</a></td>
</tr>
<%
}
//关闭连接
rs.close();
conn.close();
%>
<form method="get">
<tr align="right">
<td colspan="6">共有<font color=red><%= recordCount %></font>条记录 当前<font color=red><%= nowPages %>/<%= pageCount %></font>页
<% if(pageCount > 1){ %>
<% if(pages > 1){%>
<a href="">首页</a>
<%}if(pages < pageCount){%>
<a href="?pages=<%= nowPages+1 %>">下一页</a>
<%}if(pages != 1){%>
<a href="?pages=<%= nowPages - 1 %>">上一页</a>
<%}%>
<a href="?pages=<%= pageCount %>">尾页</a>
<% } %>跳转到
<select name="pages" onChange="javascript:this.form.submit();">
<% for(int i=1;i<=pageCount;i++){%>
<option value="<%= i %>" <% if(nowPages == i){%>selected<% } %>><%= i %></option>
<% } %>
</select>页</td>
</tr>
</form>
</table>
<hr />
© 2004 Aliang.org StartJAVA
</body>
</html>
还有不要忘了把sql-jdbc驱动安装了,并拷贝C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib文件夹到web目录下的WEB-INF下面。
最近刚刚开始搞java,还请各位大虾赐教email:webmaster@aliang.org,呵呵