Posted on 2007-07-29 13:09
碉堡 阅读(466)
评论(4) 编辑 收藏 引用 所属分类:
J2EE学习笔记
package servlets;
import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Pagination
{
private String strPage="";//page参数变量
private int curPages;//page参数的内部值
private int m_rows;//设置每页要显示的页数
private int pages;//总页数
//取得test.jsp里的test.jsp?page=<%=curPage-1%>或是test.jsp?page=<%=curPage+1%>的值给变量strPage
public String strPage(HttpServletRequest request,String page)
{
try
{
strPage=request.getParameter(page); //request对象取得page的值
}
catch(Exception e)
{
System.out.println("delcolumn" + e.getMessage());
}
return strPage;//返回这个值
}
/**
* method 'curPages'
* @param page
* @return int
*/
// 页面数
public int curPages(String page)
{
try
{
if(strPage == null)//默认没有就设置为第一页
{
curPages=1;
}
else
{
curPages=Integer.parseInt(strPage);//取得strPage整数值
if(curPages<1)//如果小于1,同样返回是第一页
curPages=1;
}
}
catch(Exception e)
{
System.out.println("curPages");
}
return curPages;//返回页面数
}
/**
* set the value of rows
*/
//设置每页要显示的记录数
public void setRows(int rows)
{
m_rows=rows;
}
/**
* method 'getPages'
* @param rowcounts
* @return int
*/
//取得页数
public int getPages(int rowcounts)
{
int test;//变量
test=rowcounts%m_rows;//取得余数
if(test==0)
pages=rowcounts/m_rows;//每页显示的整数
else
pages=rowcounts/m_rows+1;//不是的话就加1
return pages;//返回页数
}
/**
*method 'getPagesSet'
*@param rs, curPages
*@return ResultSet
*/
//结果集的返回
public ResultSet getPageSet(ResultSet rs,int curPages)
{
if(curPages==1)
{
return rs;//如果是一页的话,就返回RS
}
else
{
int i=1;
try
{
while(rs.next())
{
i=i+1;
if(i>((curPages-1)*m_rows))
break;//退出
}
return rs;//从退出开始将结果集返回
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
return rs;
}
}
--------------------------------------------------------
package data;
import java.sql.*;
public class DBConnect {
String driver ="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=fakir08";
String username="sa";
String password="spring";
Connection conn;
ResultSet rs;
public DBConnect()
{
try
{
Class.forName(driver);//创建数据库连接驱动
}
catch(ClassNotFoundException e)
{
System.out.println("jdbc:"+ e.getMessage());
}
}
public void executeUpdate(String sql)throws Exception
{
//sql=new String(sql.getBytes("BG2312"),"ISO8859_1");
try
{
conn=DriverManager.getConnection(url,username,password);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
//stmt.close();
//conn.close();
}
catch(SQLException eX)
{
System.out.println("jdbc:"+ eX.getMessage());
}
}
public ResultSet executeQurey(String sql)throws Exception
{
rs=null;
//sql=new String(sql.getBytes("BG2312"),"ISO8859_1");
try
{
conn=DriverManager.getConnection(url,username,password);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
//stmt.close();
//conn.close();
}
catch(SQLException eX)
{
System.out.println("jdbc:"+ eX.getMessage());
}
return rs;
}
}
--------------------------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="servlets.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:useBean id="m_pages" scope="page" class="servlets.Pagination"/>
<jsp:useBean id="sql" scope="page" class="data.DBConnect"/>
<%
Pagination d=new Pagination();
int curPages=d.curPages(d.strPage(request,"page"));
d.setRows(4);
ResultSet rs_count=sql.executeQurey("select count(*) as t from employee");
rs_count.next();
int resultcouts=rs_count.getInt("t");
int totalPages=d.getPages(resultcouts);
ResultSet rs =d.getPageSet(sql.executeQurey("select * from employee"),curPages);
%>
<p>分类表</p>
<table border="1">
<tr>
<td>1</td>
<td>2</td>
</tr>
<% int i=1;%>
<% while(rs.next()){%>
<tr>
<td><%= rs.getString("first_name")%></td>
<td><%= rs.getString("last_name")%></td>
</tr>
<%
i=i+1;
if(i>4)
break;
}
%>
</table>
<p align="center">
<td colspan="5" align="right" valign="middle">共 <font color="red"><%= resultcouts %></font>条记录 共 <font color="red"><%= totalPages %></font>页</td>
<%if(curPages==1) {%> <a href="CH9/test.jsp?page=<%= curPages%>">首页</a><%}%>
<%if(curPages>1) {%> <a href="CH9/test.jsp?page=<%= curPages-1%>">上一页</a><%}%>
<%if(curPages<totalPages) {%> <a href="CH9/test.jsp?page=<%= curPages+1%>">下一页</a>
<% }else {%> <a href="CH9/test.jsp?page=<%= totalPages%>">尾页</a><%}%>
</p>
</body>
</html>