我的平台是
第一步
這沒什麼難的
CREATEDATABASEpage;
usepage;
CREATETABLE `product` (
`id` varchar(
`sortid` varchar(
`name` varchar(
`price` doubleNOTNULL
`saleprice` doubleNOTNULL
`descript` text NOTNULL
`contents` text NOTNULL
`saledate` varchar(
`salecount` int(
`image` text
PRIMARYKEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf
第二步
創建一個項目
下面設置web
web
<?xml version=
<web
<servlet>
<servlet
<servlet
<init
<param
<param
</init
<init
<param
<param
</init
<init
<param
<param
</init
<load
</servlet>
<servlet
<servlet
<url
</servlet
<welcome
<welcome
</welcome
</web
struts
<?xml version=
<!DOCTYPE struts
<struts
<data
<form
<global
<global
<action
<action
attribute=
input=
name=
path=
scope=
type=
<forward name=
</action>
</action
<message
</struts
第三步
在SRC下創建 dao
在dbtool包裡主要放訪問JDBC數據庫的連接類等
DBConnection
package com
import java
import java
import java
import java
import java
/**
* 這是一個連接數據的單模式
* @author 樹下無影
*
*/
public class DBConnection {
private static DBConnection instance;
private String driver;
private String url;
private String user;
private String password;
private DBConnection() throws Exception{
InputStream in=getClass()
Properties prop=new Properties();
prop
driver=prop
url=prop
user=prop
password=prop
try{
Class
}catch(Exception e)
{
System
throw e;
}
System
}
public static DBConnection getInstance(){
try{
if(instance==null){
instance=new DBConnection();
}
return instance;
}catch(Exception e){
System
return null;
}
}
public Connection getConnection()throws SQLException{
Connection con;
try{
con=DriverManager
}catch(SQLException e){
System
throw e;
}
return con;
}
public void closeConnection(Connection con){
if(con!=null){
try{
con
}catch(SQLException e)
{
System
}
}
}
}
這裡用一個配置文件
database
driver=org
url=jdbc:mysql://localhost:
user=root
password=
下面是我用的數據庫增刪改查的Bean
package com
import java
/**
* 這是一個連接數據庫具有增刪改查的Bean
* @author 樹下無影
*
*/
public class DBbusiness {
/*
* 定義連接參數等
*/
Connection conn = null;
PreparedStatement psps = null;
ResultSet rs = null;
public DBbusiness (){
}
/*
* 定義公用的Connection
*/
public Connection getConn() {
try {
DBConnection db=DBConnection
Connection conx = db
return conx;
} catch (Exception e) {
System
}
return null;
}
/*
* 獲取數據(查詢)方法
*/
public ResultSet getData(String sql) {
try {
conn = getConn();
psps = conn
rs = psps
} catch (Exception e) {
System
}
return rs;
}
/*
* 定義插入數據和更新的方法
*/
public boolean insert(String sql) {
try {
conn = getConn();
psps = conn
psps
return true;
} catch (Exception e) {
System
}
return false;
}
/*
* 定義創建數據庫和表的方法
*/
public boolean create(String sql) {
try {
conn = getConn();
psps = conn
psps
return true;
} catch (Exception e) {
}
return false;
}
/*
* 定義關閉連接的方法
*/
public void allClose() {
try {
if (rs != null)
rs
if (psps != null)
psps
if (conn != null)
{
DBConnection db=DBConnection
db
}
} catch (Exception e) {
System
}
}
}
第四步
在vo包裡
Product
package com
public class Product {
String id;
String sortid;
String name;
String price;
String saleprice;
String descript;
String contents;
String saledate;
String salecount;
String image;
public Product(){}
public Product(String id
String saleprice
String saledate
this
this
this
this
this
this
ntents=contents;
this
this
this
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
ntents = contents;
}
public String getDescript() {
return descript;
}
public void setDescript(String descript) {
this
}
public String getId() {
return id;
}
public void setId(String id) {
this
}
public String getImage() {
return image;
}
public void setImage(String image) {
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this
}
public String getSalecount() {
return salecount;
}
public void setSalecount(String salecount) {
this
}
public String getSaledate() {
return saledate;
}
public void setSaledate(String saledate) {
this
}
public String getSaleprice() {
return saleprice;
}
public void setSaleprice(String saleprice) {
this
}
public String getSortid() {
return sortid;
}
public void setSortid(String sortid) {
this
}
}
第五步
PageDao
package com
import java
import java
public interface PageDao {
public int getCount(String counSql);
public ArrayList getProduct(String sql);
}
創建接口好後
如下
package com
import java
import java
import java
import com
import com
/**
* 這是接口的實現類
* @author 樹下無影
*
*/
public class PageDaoImpl implements PageDao {
/*
* 獲取數據行數
* @see com
*/
public int getCount(String counSql){
int result=
DBbusiness db=new DBbusiness();
ResultSet rs= db
try {
rs
result=rs
/*while(rs
result=rs
}*/
} catch (SQLException e) {
// TODO Auto
System
}finally{
db
}
return result;
}
/*
* 讀取數據表
*/
public ArrayList getProduct(String sql){
ArrayList arrayList=new ArrayList();
DBbusiness db=new DBbusiness();
ResultSet rs=db
try {
while(rs
String id=rs
String sortid=rs
String name=rs
String price=rs
String saleprice=rs
String descript=rs
String contents=rs
String saledate=rs
String salecount=rs
String image=rs
Product productForm=new Product( id
saleprice
saledate
arrayList
}
} catch (SQLException e) {
// TODO Auto
System
}finally{
db
}
return arrayList;
}
}
第六步
這個類沒什麼解釋
PageDaoFactory
package com
import com
import com
public class PageDaoFactory {
public static PageDao getPageDaoIntanse(){
return new PageDaoImpl();
}
}
第七步
呵呵
在dbtool包裡創建如下類
PageBean
package com
import com
public class PageBean {
/**
* 這是一個分頁的類
* 所在在這裡主要是處理出這樣的指令
* 並獲得相應的頁數信息
*MySql語句如下:select * from test limit
*select * from test limit
*/
int curr; //當前頁
int count; //總頁數
int size; //每頁顯示數據數
int rows=
boolean last; // 是否是最後一頁
/**
* 構造器
* @param counSql
*/
public PageBean(String counSql) {
if (this
this
}
this
this
unt = (int) Math
this
}
public PageBean(String counSql
if (this
this
}
this
this
unt = (int) Math
this
}
public PageBean(String counSql
if (this
this
}
this
this
unt = (int) Math
this
}
/**
* 頁面指令處理及返回相應的查詢SQL語句
*/
public String pageDeal(String pageDo
String str =
//首頁
if (pageDo
setCurr(
str +=
}
//尾頁
if (pageDo
setCurr(getCount());
str +=
str +=
}
//下一頁
if (pageDo
if(getCurr()<getCount()){
str +=
str +=
setCurr(getCurr() +
}else{
setCurr(getCount());
str +=
str +=
}
}
//上一頁
if (pageDo
setCurr(getCurr()
str +=
str +=
}
return sql + str;
}
public static void main(String[] args) {
}
//返回總頁數
public int getCount() {
return (count ==
}
//設置總頁數
public void setCount(int count) {
unt = count;
}
//返回當前頁
public int getCurr() {
return (curr ==
}
//設置當前頁
public void setCurr(int curr) {
this
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this
}
public int getSize() {
return size;
}
public void setSize(int size) {
this
}
/**
* 如果是最後一頁的返回true
* @return
*/
public boolean isLast() {
return (curr==count)?true:false;
}
public void setLast(boolean last) {
this
}
}
這個類寫了很多的注釋
當前頁的定義
Action 裡通過調用pageDeal(
好了
第八步
在struts
package com
import java
import javax
import javax
import org
import org
import org
import org
import com
import com
import com
public class ProductShowAction extends Action {
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
ArrayList aList = new ArrayList();
/*
* 定義頁面傳過來的動作
*/
String pageDo = request
/*
* 定義獲取頁面傳過來的當前頁getCurr
*/
int getCurr;
String curr_page = request
if (curr_page == null || curr_page
getCurr =
} else {
getCurr = Integer
System
}
/*
* 實例化PageBean對象
* PageBean有幾個構造器
* getCurr是傳送一個當前頁給PageBean的構造器
*
*/
PageBean pb = new PageBean(
// 定義查詢數據庫的SQL語句
String sql;
sql = pb
// 定義ArrayList獲取數據庫所查詢得到的數據
aList = PageDaoFactory
// 把值傳給客戶端
request
request
return mapping
}
}
這個Action裡也寫了好多的注釋
步驟主要是
第九步
由於後台傳回來的是一個ArrayList的數據表
還返回一個PageBean的對象
所以要是想弄不同的
看我的Jsp頁面
<%@ page language=
<%@ taglib uri=
<%@ taglib uri=
<%@ taglib uri=
<%@ taglib uri=
<% if(session
{//這裡只是一個跳轉
session
response
}
%>
<html>
<head>
<title>JSP for ProductShowForm form</title>
</head>
<body><font color=
這裡僅演示分頁
<table width=
<tr>
<th>
商品名稱
</th>
<th>
價格
</th>
<th>
商品描述
</th>
<th>
商品詳細信息
</th>
<th >
上架日期
</th>
<th colspan=
操作
</th>
</tr>
<logic:present name=
<logic:iterate id=
type=
<tr>
<td>
<bean:write name=
</td>
<td>
<bean:write name=
</td>
<td>
<bean:write name=
</td>
<%
<bean:write name=
</td>
<bean:write name=
</td>
<td>
<html:link action=
paramId=
修改</html:link>
</td>
<td>
<html:link action=
paramId=
刪除</html:link>
</td>
</tr>
</logic:iterate>
</logic:present>
</table>
<logic:present name=
第<bean:write name=
<bean:write name=
<html:link action=
paramId=
首頁</html:link>
<logic:notEqual name=
<html:link action=
paramId=
上一頁</html:link>
</logic:notEqual>
<logic:equal name=
<html:link action=
paramId=
下一頁</html:link>
</logic:equal>
<html:link action=
paramId=
尾頁</html:link>
</logic:present>
</body>
</html>
總結
這個分頁看起來很簡單
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28426.html