一个利用JavaBean分页显示列表的例子

在Tomcat6下测试,采用连接池,数据库驱动为SqlServer2000

连接数据库
package mybook;

import java.sql.*;
import javax.sql.*;
import javax.naming.*;

public class ConnectDB {
	
	private Connection conn=null;
	
	public ConnectDB(){}
	
	public Connection getConnection() throws Exception {
        if (conn == null) {
               Context initContext = new InitialContext();
               Context envContext = (Context) initContext.lookup("java:comp/env");
               DataSource ds = (DataSource) envContext.lookup("jdbc/mybook");
               conn = ds.getConnection();
        }
        return conn;
    }

}


分页显示
package mybook;

import java.sql.*;
import java.util.*;

public class ShowList {

	public ShowList() {
	};

	public ArrayList<Object> Show(int CurPage) {
		int PageSize;
		PageSize = 20;
		if (CurPage < 1)
			CurPage = 1;

		ArrayList<Object> list = new ArrayList<Object>(PageSize + 1);

		mybook.ConnectDB db = new mybook.ConnectDB();
		String sql = "select * from SNList";

		try {
			Connection conn = db.getConnection();
			PreparedStatement ps = conn
					.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
							ResultSet.CONCUR_UPDATABLE);

			ResultSet rs = ps.executeQuery();

			// 求得总页数
			rs.last();
			long Pages, Rows;
			Rows = rs.getRow();
			if (Rows % PageSize == 0) {
				Pages = Rows / PageSize;
			} else {
				Pages = Rows / PageSize + 1;
			}
			// 把页数添加到List中
			list.add(Pages);

			// 移动记录集光标
			rs.beforeFirst();
			if (CurPage > 1)
				rs.absolute((CurPage - 1) * PageSize);

			int count = 1;
			while (rs.next()) {
				HashMap<Object, Object> hmap = new HashMap<Object, Object>();
				ResultSetMetaData rsmd = rs.getMetaData();
				int colSize = rsmd.getColumnCount();
				for (int i = 1; i <= colSize; i++) {
					hmap.put(rsmd.getColumnName(i).toLowerCase(),
							rs.getObject(i));
				}
				list.add(hmap);
				count++;
				if (count > PageSize)
					break;
			}
			rs.close();
			ps.close();
			conn.close();
		} catch (Exception ex) {
			list = null;
		}
		return list;
	}
}


WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
	<display-name>My Web Application</display-name>
	<description>
		A application for test.
	</description>
   
	<filter>
		<filter-name>Set Character Encoding</filter-name>
		<filter-class>filters.SetCharacterEncodingFilter</filter-class>
		<init-param>
		<param-name>encoding</param-name>
		<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>Set Character Encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping> 
	
	<resource-ref>
	  <description>DB Connection</description>
	  <res-ref-name>jdbc/mybook</res-ref-name>
	  <res-type>javax.sql.DataSource</res-type>
	  <res-auth>Container</res-auth>
	</resource-ref> 
</web-app>


META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true" crossContext="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<Resource 
	 name="jdbc/mybook" 
	 auth="Container"
	 type="javax.sql.DataSource"
	 driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
	 url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=FTXSDATA_2009"
	 username="sa"
	 password=""
	 maxActive="200"
	 maxIdle="30"
	 maxWait="10000" />
</Context>


showlist.jsp
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*,java.text.SimpleDateFormat,java.util.Locale,java.text.DecimalFormat"%>
<jsp:useBean id="show" scope="request" class="mybook.ShowList" />

<link rel="stylesheet" type="text/css" href="style.css">

<title>发货通知单列表</title>
<%
	String curpage1=null;
	if (request.getParameter("page")==null || request.getParameter("page").trim()=="") curpage1="1";
	else curpage1=request.getParameter("page");

	int curPage=Integer.parseInt(curpage1);
	curPage=curPage>0?curPage:1;

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.SIMPLIFIED_CHINESE);
	DecimalFormat decFmt = new DecimalFormat("##,##0.00");
	
	ArrayList<Object> list = show.Show(curPage);
	long Pages=Long.parseLong(list.get(0).toString());
%>

<div align="center"><h3>发货通知单列表</h3></div>

<table border="1" class="tableList" id="tbl1" cellspacing="0" bordercolor="#AAAAAA" style="border-collapse:collapse;" align="center">
<thead>
	<th width="80">单据号</th>
	<th width="80">合同号</th>
	<th width="250">客户名称</th>
	<th width="80">产品名称</th>
	<th width="80">数量</th>
	<th width="80">备注</th>
	<th>&nbsp;</th>
</thead>
<tfoot align="center">
	<tr>
	<td colspan="7" align="center">
		<a href="show.jsp?page=1">首页</a>
		<a href="show.jsp?page=<%=curPage>1?curPage-1:1%>">上页</a>
		<a href="show.jsp?page=<%=curPage<Pages?curPage+1:Pages%>">下页</a>
		<a href="show.jsp?page=<%=Pages%>">末页</a>
	</td>
	</tr>
</tfoot>
<tbody>
<%
	for (int i=1;i<list.size();i++) {
		HashMap<Object,Object> hmap=(HashMap<Object,Object>)list.get(i);
%>
<tr>
	<td align="center"><%=hmap.get("ccode")%></td>
	<td align="center"><%=hmap.get("csocode")%></td>
	<td align="center"><%=hmap.get("ccusname")%></td>
	<td align="center"><%=hmap.get("cinvname")%></td>
	<td align="right"><%=decFmt.format(hmap.get("inum"))%></td>
	<td align="center"><%=hmap.get("cmaker")%></td>
	<td align="center"><input type="checkbox" name="Select" value="<%=hmap.get("icusid")%>"></td>
</tr>
<%
	}
%>
</tbody>
</table>

<!--[if lt IE 7]>
	<script type="text/javascript" src="ie7.js"></script>
<![endif]-->

你可能感兴趣的:(java,sql,Web,xml,IE)