jsp+javabean実装ページング


このページングプログラムは3つのファイルtestにしか使用されません.jsp(ページング結果を表示するためのJSPページ)とPagination.JAva(ページングプログラムをカプセル化するためのもの)とDBConnect.JAva(SqlServer 2000データベースに接続するためのJAVAクラス)と、単純なデータベースuserのテーブルusernameと、テスト用のwebパブリケーションサーバはTomcat 5.5.5.20である.そしてDBConnect.JAvaとPagination.JAvaはWEB-INFの下にあるclassesディレクトリの下(注意、なければ新規作成)、データベースはSqlServer 2000を使用する.
1、create database username--------データベースusernameテーブルcreate table usernameを確立する(name varchar(25));---データテーブルusernameを作成するフィールドnameタイプは文字型です(sqlの操作についてはD:学習資料SQL serverレッスン逍湘SQL大全.docのデータベースに関する知識に注意してください.)
2、DBConnect.JAva----------sqlserver 2000データベースへの接続
package net.xiaoxiang;

import java.sql.*;

/**
 *       ---(JDBC)
 * @author   
 */
public class DBConnect
{
	
	//       
	String		drivename	= "com.microsoft.jdbc.sqlserver.SQLServerDriver";
	//     ,    user
	String		URL			= "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=user";
	//                
	String		user		= "sa";
	//                 
	String		password	= "123";
	Connection	conn		= null;
	ResultSet	rs			= null;
	
	/**
	 *             *
	 */
	public DBConnect ( ){
		try
		{
			Class.forName ( drivename );//        
			conn = DriverManager.getConnection ( URL, user, password );//     
		}
		catch ( java.lang.ClassNotFoundException e )
		{
			System.out.println ( "Jdbc_conn():" + e.getMessage ( ) );
		}
		catch ( SQLException ex )
		{
			System.out.println ( "sql.executeUpdate:" + ex.getMessage ( ) );
		}
	}
	
	/**
	 *       
	 * @param sql
	 * @throws Exception
	 */
	public void executeUpdate ( String sql ) throws Exception
	{
		sql = new String ( sql.getBytes ( "GBK" ), "ISO8859_1" );//      
		try
		{
			Statement stmt = conn.createStatement ( );
			stmt.executeUpdate ( sql );
			conn.close ( );
			stmt.close ( );
		}
		catch ( SQLException ex )
		{
			System.out.println ( "sql.executeUpdate:" + ex.getMessage ( ) );
		}
	}
	
	/**
	 *       
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public ResultSet executeQuery ( String sql ) throws Exception
	{
		rs = null;
		sql = new String ( sql.getBytes ( "GBK" ), "ISO8859_1" );//      
		try
		{
			Statement stmt = conn.createStatement ( );//       
			rs = stmt.executeQuery ( sql );//   sql
			// conn.close();//     
			// stmt.close();//     
		}
		catch ( SQLException ex )
		{
			System.out.println ( "sql.executeQuery:" + ex.getMessage ( ) );
		}
		return rs;
	}
	
	/**
	 *   
	 * @param args
	 */
	public static void main ( String args[] )
	{
		try
		{
			ResultSet rs_count = new DBConnect ( )
					.executeQuery ( "select count(*) as t from username" );//          javabean
			rs_count.next ( );
			int resultconts = rs_count.getInt ( "t" );//        
			System.out.print ( resultconts );
		}
		catch ( Exception ex )
		{
			System.out.println ( "sql.executeQuery:ok" + ex.getMessage ( ) );
		}
	}
}
 
 
PaginationでJAvaパッケージのページングクラスはtest.jspに表示
3、Pagination.JAva------ページングされたクラスをカプセル化
package net.xiaoxiang;

import java.sql.*;
import javax.servlet.http.*;

/**
 *       
 * @author   
 */
public class Pagination
{
	
	private String	strPage	= null;	// page    
	private int		current_Pages;	//     
	private int		page_record;	//          
	private int		total_Pages;	//    
	
	/**
	 *   xxx.jsp      xxx.jsp?page=<%=current_Pages-1%>
	 *   page=<%=current_Pages+1%>     strPage
	 * @param request
	 * @param page
	 *                   
	 * @return 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;
	}
	
	/**
	 *           
	 * @param strPage
	 * @return current_Pages(     )
	 */
	public int current_Pages ( String strPage )
	{
		try
		{
			if ( strPage == null )
			{ //            
				current_Pages = 1;
			}
			else
			{
				current_Pages = Integer.parseInt ( strPage );//   strPage    
				if ( current_Pages < 1 ) //     1,        
					current_Pages = 1;
			}
		}
		catch ( Exception e )
		{
			System.out.print ( "current_Pages" );
		}
		return current_Pages;//      
	}
	
	/**
	 * @param page_record
	 *                       
	 */
	public void setPage_record ( int page_record )
	{
		this.page_record = page_record;
	}
	
	/**
	 *      
	 * @param total_record
	 *                (       )
	 * @return total_Pages      
	 */
	public int getTotal_Pages ( int total_record )
	{
		int test;//   
		test = total_record % page_record;//     
		if ( test == 0 )
			total_Pages = total_record / page_record;//        
		else
			total_Pages = total_record / page_record + 1;//        
		return total_Pages;
	}
	
	/**
	 *       
	 * @param rs
	 *               
	 * @param current_Pages
	 *              
	 * @return rs    
	 */
	public ResultSet getPageSet ( ResultSet rs, int current_Pages )
	{
		if ( current_Pages == 1 )
		{
			return rs;//      ,     rs
		}
		else
		{
			int i = 1;
			try
			{
				while ( rs.next ( ) )
				{
					i = i + 1;
					if ( i > ( ( current_Pages - 1 ) * page_record ) )
						break;//   
				}
				return rs;//            
			}
			catch ( Exception e )
			{
				System.out.print ( e.getMessage ( ) );
			}
		}
		return rs;
	}
}

 
 
 
4.test.jsp------ページを表示
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.sql.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>    </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="bean.Pagination"></jsp:useBean>
		<jsp:useBean id="sql" scope="page" class="bean.DBConnect" />
		<%
			int curPages = m_pages.current_Pages ( m_pages.strPage ( request, "page" ) );
			m_pages.setPage_record ( 10 );//      10 
		%>
		<%
		//         javabean
			ResultSet rs_count = sql.executeQuery ( "select count(*) as t from username" );
			rs_count.next ( );
			int resultconts = rs_count.getInt ( "t" );//       
			int totalPages = m_pages.getTotal_Pages ( resultconts );//     
			//           (   ,  )
			ResultSet rs = m_pages.getPageSet ( sql
					.executeQuery ( "select * from username" ), curPages );
		%>
		<p>
			   
		</p>
		<table border="1">
			<tr>
				<td>
					  
				</td>
			</tr>
			<%
				int i = 1;
			%>
			<%
				while ( rs.next ( ) )
				{
			%>
			<tr>
				<!-- <td> <%-- <%=rs.getString("id")%> --%> </td> -->
				<td><%=rs.getString ( "name" )%>
				</td>
			</tr>
			<%
				i = i + 1;
					if ( i > 10 )
						break;
				}
			%>
		</table>
		<p align="center">
			<%
				if ( curPages > 1 )
				{
			%><a href="test.jsp?page=<%=curPages - 1%>">   </a>
			<%
				}
			%>
			<%
				if ( curPages < totalPages )
				{
			%><a href="test.jsp?page=<%=curPages + 1%>">   </a>
			<%
				}
			%>
		</p>

	</body>
</html>