源代码:http://yuncode.net/code/c_505f0f9d25b3924

 

未经过调试,请谅解。

 

 

001 <%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>

 

002 <%@ page language="java" import="java.sql.*"%>
003   
004 <script language="javascript">
005     function newwin(url) {
006         var
007   
008         newwin = window
009                 .open(
010                         url,
011                         "newwin",
012                         "toolbar=no,location=no,directories=no,status=no,menubar=no, scrollbars=yes,resizable=yes,width=600,height=450");
013         newwin.focus();
014         return false;
015     }
016 </script>
017 <script LANGUAGE="javascript">
018     function submit10() {
019         self.location.replace("fenye.jsp")
020     }
021 </script>
022 <%
023     //变量声明 
024     java.sql.Connection sqlCon; //数据库连接对象 
025     java.sql.Statement sqlStmt; //SQL语句对象 
026     java.sql.ResultSet sqlRst; //结果集对象 
027     java.lang.String strCon; //数据库连接字符串 
028     java.lang.String strSQL; //SQL语句 
029     int intPageSize; //一页显示的记录数 
030     int intRowCount; //记录总数 
031     int intPageCount; //总页数 
032     int intPage; //待显示页码 
033     java.lang.String strPage;
034     int i;
035     //设置一页显示的记录数 
036     intPageSize = 10;
037     //取得待显示页码 
038     strPage = request.getParameter("page");
039     if (strPage == null) {//表明在QueryString中没有page这一个参数,此时显示第一页数据 
040         intPage = 1;
041     } else {//将字符串转换成整型 
042         intPage = java.lang.Integer.parseInt(strPage);
043         if (intPage < 1)
044             intPage = 1;
045     }
046     //装载JDBC驱动程序 
047     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
048     //设置数据库连接字符串 
049     strCon = "jdbc:sqlserver://127.0.0.1;databaseName=dbname";
050     //连接数据库 
051     sqlCon = java.sql.DriverManager.getConnection(strCon, "sa",
052             "12345678");
053     //创建一个可以滚动的只读的SQL语句对象 
054     sqlStmt = sqlCon.createStatement(
055             java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
056             java.sql.ResultSet.CONCUR_READ_ONLY);//准备SQL语句 
057     strSQL = "select * from etsa";
058     //执行SQL语句并获取结果集 
059     sqlRst = sqlStmt.executeQuery(strSQL);
060     //获取记录总数 
061     sqlRst.last();//??光标在最后一行 
062     intRowCount = sqlRst.getRow();//获得当前行号 
063     //记算总页数 
064     intPageCount = (intRowCount + intPageSize - 1) / intPageSize;
065     //调整待显示的页码 
066     if (intPage > intPageCount)
067         intPage = intPageCount;
068 %>
069 <html>
070     <head>
071         <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
072         <title>分頁</title>
073     </head>
074     <body>
075         <form method="POST" action="">
076             第<%=intPage%>页 共<%=intPageCount%>页
077   
078             <%
079             if (intPage < intPageCount) {
080         %>
081             <a href="fenye.jsp?page=<%=intPage + 1%>">下一页</a>
082             <%
083                 }
084             %>
085             <%
086                 if (intPage > 1) {
087             %>
088             <a href="fenye.jsp?page=<%=intPage - 1%>">上一页</a>
089             <%
090                 }
091             %>
092             转到第:
093             <input type="text" name="page" size="8">
094             
095             <input type="submit" value="GO" name="cndok">
096         </form>
097         <table border="1" cellspacing="0" cellpadding="0">
098   
099             <%
100                 if (intPageCount > 0) {
101                     //将记录指针定位到待显示页的第一条记录上 
102                     sqlRst.absolute((intPage - 1) * intPageSize + 1);
103                     //显示数据 
104                     i = 0;
105                     String user_id, user_name;
106                     while (i < intPageSize && !sqlRst.isAfterLast()) {
107                         user_id = sqlRst.getString(1);
108                         user_name = sqlRst.getString(2);
109             %>
110             <tr>
111                 <td><%=user_id%></td>
112                 <td><%=user_name%></td>
113   
114             </tr>
115             <%
116                 sqlRst.next();
117                         i++;
118                     }
119                 }
120             %>
121         </table>
122   
123   
124     </body>
125 </html>
126 <%
127     //关闭结果集 
128     sqlRst.close();
129     //关闭SQL语句对象 
130     sqlStmt.close();
131     //关闭数据库 
132     sqlCon.close();
133 %>