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

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

[b]连接数据库[/b]

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;
}

}


[b]分页显示[/b]

package mybook;

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

public class ShowList {

public ShowList() {
};

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

ArrayList list = new ArrayList(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 hmap = new HashMap();
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;
}
}


[b]WEB-INF/web.xml[/b]




My Web Application

A application for test.



Set Character Encoding
filters.SetCharacterEncodingFilter

encoding
UTF-8



Set Character Encoding
/*



DB Connection
jdbc/mybook
javax.sql.DataSource
Container




[b]META-INF/context.xml[/b]




WEB-INF/web.xml


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" />



[b]showlist.jsp[/b]


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*,java.text.SimpleDateFormat,java.util.Locale,java.text.DecimalFormat"%>




发货通知单列表
<%
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 list = show.Show(curPage);
long Pages=Long.parseLong(list.get(0).toString());
%>

发货通知单列表



















<%
for (int i=1;i HashMap hmap=(HashMap)list.get(i);
%>









<%
}
%>

单据号 合同号 客户名称 产品名称 数量 备注  

首页
上页
下页
末页
<%=hmap.get("ccode")%> <%=hmap.get("csocode")%> <%=hmap.get("ccusname")%> <%=hmap.get("cinvname")%> <%=decFmt.format(hmap.get("inum"))%> <%=hmap.get("cmaker")%> ">



你可能感兴趣的:(Windows)