jsp/javabean分頁列出資料
最後更新:2017-02-28
來源:互聯網
上載者:User
js|分頁|資料 我的第一個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,呵呵