易买网之分页显示对应当前页扥所有的商品1

来源:互联网 发布:农产品出口数据 编辑:程序博客网 时间:2024/05/21 10:32
package com.entity;


/**
 * 商品类
 * 
 * 
 * 
 */
public class Product {
private int proid;
private String pname;
private double price;
private int store;
private int ptype;
private String img;
private String pinfo;
public Product(int proid, String pname, double price, int store, int ptype,
String img, String pinfo) {
super();
this.proid = proid;
this.pname = pname;
this.price = price;
this.store = store;
this.ptype = ptype;
this.img = img;
this.pinfo = pinfo;
}
public Product() {
super();
}
public int getProid() {
return proid;
}
public void setProid(int proid) {
this.proid = proid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStore() {
return store;
}
public void setStore(int store) {
this.store = store;
}
public int getPtype() {
return ptype;
}
public void setPtype(int ptype) {
this.ptype = ptype;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getPinfo() {
return pinfo;
}
public void setPinfo(String pinfo) {
this.pinfo = pinfo;
}




}

-----------------------------------BaseDao ----------------------------------------

package com.dao;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


import com.mysql.jdbc.Driver;


public class BaseDao {
//四大参数
private static final String DRIVER="com.mysql.jdbc.Driver";


private static final String URL="jdbc:mysql://localhost/db_shoping?useUnicode=true&characterEncoding=UTF-8";






private static final String USERNAME="root";
private static final String PWD="123456";
//获取链接
public static Connection myBase(){
Connection conn=null;
try {
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL, USERNAME, PWD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return conn;

}
//关闭数据库
public static void myClose(Connection conn,PreparedStatement ps,ResultSet rs ){
try {
if(conn!=null){conn.close();}
if(ps!=null){ps.close();}
if(rs!=null){rs.close();}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}
     //增删改的调用方法
public static int chengeBase(String sql,Object[] obj ){
int row=0;
Connection conn=myBase();
PreparedStatement ps=null;
try {
int num=1;
ps=conn.prepareStatement(sql);
for(Object ob:obj){
ps.setObject(num, ob);
num++;
}
row=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
myClose(conn, ps, null);
}
return row;
}

}

---------------------------------------------------------------------

package com.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.entity.Product;


/**
 * 处理商品翻页的Dao层
 * 
 *
 */
public class ProductDao extends BaseDao {
/**
* 获取商品的总条数
*/
public int getCount() {
Connection con =this.myBase();
PreparedStatement pps=null;
ResultSet rs=null;
int a=0;
try {
String sql="SELECT COUNT(1) FROM product";
pps=con.prepareStatement(sql);
rs=pps.executeQuery();
if(rs.next()){
a=rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
this.myClose(con, pps, rs);
}
return a;
}
/**
* 获取当前页面商品数据的集合
*/
public List<Product>l_getList(int dqpage,int pageSize){
Connection con =this.myBase();
PreparedStatement pps=null;
ResultSet rs=null;
List<Product> list =new ArrayList<Product>();
try {
String sql="SELECT * FROM product LIMIT ?,?";
pps=con.prepareStatement(sql);
pps.setInt(1, (dqpage-1)*pageSize);
pps.setInt(2, pageSize);
rs=pps.executeQuery();
while(rs.next()){
Product p =new Product();
p.setProid(rs.getInt(1));
p.setPname(rs.getString(2));
p.setPrice(rs.getDouble(3));
p.setStore(rs.getInt(4));
p.setPtype(rs.getInt(5));
p.setImg(rs.getString(6));
p.setPinfo(rs.getString(7));
list.add(p);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
this.myClose(con, pps, rs);
}
return list;
}
/**

*获取商品详情的商品信息的方法
*入口:商品id
*出口:商品信息
*/
public Product tgetProduct(int proid){
Product pro=null;
PreparedStatement ps=null;
ResultSet rs=null;
Connection conn=this.myBase();
String sql="SELECT * FROM product WHERE proid=?";
try {
ps=conn.prepareStatement(sql);
ps.setInt(1, proid);
rs=ps.executeQuery();
while(rs.next()){
pro=new Product();
pro.setProid(rs.getInt(1));
pro.setPname(rs.getString(2));
pro.setPrice(rs.getDouble(3));
pro.setStore(rs.getInt(4));
pro.setPtype(rs.getInt(5));
pro.setImg(rs.getString(6));
pro.setPinfo(rs.getString(7));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
this.myClose(conn, ps, rs);
}

return pro;

}


}

-------------------------------------------------------------

package com.biz;
/**
 * 处理商品翻页数据
 *
 */


import java.util.List;


import com.dao.ProductDao;
import com.util.PageBean;




public class ProductBiz {
PageBean pd =new PageBean();
public PageBean l_get(int dqpage,int pageSize) {
ProductDao dao=new ProductDao();
pd.setCurrentPage(dqpage);
pd.setPageSize(pageSize);
pd.setTotalNumber(dao.getCount());
pd.setList(dao.l_getList(dqpage, pageSize));

return pd;

}
}

------------------------------------------------------------------------------------------------

package com.util;


import java.util.List;


public class PageBean {

private int currentPage;//当前页
private int pageSize;//一页显示的条数
private int totalNumber;//总条数
private int allPage;//总页数
private List  list;
public PageBean(int currentPage, int pageSize, int totalNumber,
int allPage, List list) {
super();
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalNumber = totalNumber;
this.allPage = allPage;
this.list = list;
}
public PageBean() {
super();
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalNumber() {
return totalNumber;
}
public void setTotalNumber(int totalNumber) {
this.totalNumber = totalNumber;
allPage=totalNumber%pageSize==0?totalNumber/pageSize:totalNumber/pageSize+1;

}
public int getAllPage() {
return allPage;
}
public void setAllPage(int allPage) {
this.allPage = allPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
@Override
public String toString() {
return "PageBean [currentPage=" + currentPage + ", pageSize="
+ pageSize + ", totalNumber=" + totalNumber + ", allPage="
+ allPage + ", list=" + list + "]";
}







}

阅读全文
0 0
原创粉丝点击