淘宝商城
/** * 功能:这是一个数据库连接,一个model类 */package com.xf.model;import java.sql.*;public class ConnDB{ private Connection ct=null; public Connection getConn() { try { Class.forName("com.mysql.jdbc.Driver"); ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/db3","root",""); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return ct; }}
/** * 功能:这是一个model类对应goods表 */package com.xf.model;public class GoodsBean{ private long goodsId; //货物ID private String goodsName; //货物名称 private String goodsIntro; //介绍 private float goodsPrice; //价格 private int goodsNum; //数量 private String publisher; //发行商 private String photo="11.jpg"; //照片 private String type; //类型 public long getGoodsId() { return goodsId; } public void setGoodsId(long goodsId) { this.goodsId = goodsId; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public String getGoodsIntro() { return goodsIntro; } public void setGoodsIntro(String goodsIntro) { this.goodsIntro = goodsIntro; } public float getGoodsPrice() { return goodsPrice; } public void setGoodsPrice(float goodsPrice) { this.goodsPrice = goodsPrice; } public int getGoodsNum() { return goodsNum; } public void setGoodsNum(int goodsNum) { this.goodsNum = goodsNum; } public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } public String getPhoto() { return photo; } public void setPhoto(String photo) { this.photo = photo; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
/** * 功能:也是一个model,业务逻辑,处理和goods表相关的业务逻辑 */package com.xf.model;import java.sql.*;import java.util.ArrayList;public class GoodsBeanBO{ //定义一些要用到的变量 private ResultSet rs=null; private Connection ct=null; private PreparedStatement ps; public int getPageCount(int pageSize) { int pageCount=0; //总页数 int rowCount=0; //一共有多少条记录 try { //链接数据库 ct=new ConnDB().getConn(); //查询语句 String mysql="select count(*) from `goods`"; ps=ct.prepareStatement(mysql); //全部记录放入 rs=ps.executeQuery(); if(rs.next()) //如果查找到了数据 { rowCount=rs.getInt(1); //得到个数 if(rowCount%pageSize != 0) { pageCount=rowCount/pageSize+1; //得到结果四舍五入然后加一 } else { pageCount=rowCount/pageSize; //恰好整除,那么就直接得到结果 } } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { this.close(); } return pageCount; } /** * 功能:得到分页数据,分页显示数据 * @param pageSize 这个是每页面显示地个数 * @author cutter_point * @param pageNow 当前要显示地页面 */ public ArrayList getGoodsByPage(int pageSize, int pageNow) { ArrayList a1=new ArrayList(); //这是要返回的数据 try { //得到链接 ct=new ConnDB().getConn(); //导入mysql语句 String mysql="select * from `goods` limit ?,?"; ps=ct.prepareStatement(mysql); //吧?设置成想要变成的值 ps.setInt(1, pageSize*(pageNow-1)); ps.setInt(2, pageSize); //执行查询,结果放入ResultSet中 rs=ps.executeQuery(); while(rs.next()) //只要取出一行数据,循环下去 { GoodsBean gb=new GoodsBean(); //吧查找到的数据放入到gb中 gb.setGoodsId(rs.getInt(1)); gb.setGoodsName(rs.getString(2)); gb.setGoodsIntro(rs.getString(3)); gb.setGoodsPrice(rs.getFloat(4)); gb.setGoodsNum(rs.getInt(5)); gb.setPublisher(rs.getString(6)); gb.setPhoto(rs.getString(7)); gb.setType(rs.getString(8)); //吧goodsBean的数据放入到ArrayList里面去 a1.add(gb); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { this.close(); } return a1; //吧ArrayList返回上去 } //根据货物id,得到货物具体信息的函数 public GoodsBean getGoodsBean(String id) { GoodsBean gb=new GoodsBean(); try { //建立连接 ct=new ConnDB().getConn(); //存放连接数据库的脚本语句 String mysql="select * from `goods` where `goodsId`=?"; ps=ct.prepareStatement(mysql); //吧id传给第一个? ps.setString(1, id); //吧查询到的数据给rs rs=ps.executeQuery(); if(rs.next()) //只要查找到了数据 { //吧查找到的数据放入到gb中 gb.setGoodsId(rs.getInt(1)); gb.setGoodsName(rs.getString(2)); gb.setGoodsIntro(rs.getString(3)); gb.setGoodsPrice(rs.getFloat(4)); gb.setGoodsNum(rs.getInt(5)); gb.setPublisher(rs.getString(6)); gb.setPhoto(rs.getString(7)); gb.setType(rs.getString(8)); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { //关闭资源 this.close(); } return gb; } //关闭资源 public void close() { try { if(!rs.isClosed()) { rs.close(); rs=null; } if(!ps.isClosed()) { ps.close(); ps=null; } if(!ct.isClosed()) { ct.close(); ct=null; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
/** * 功能:这是一个BO(model),用来处理与购物相关的业务逻辑 * 时间:2014年7月9日14:29:30 * 作者:cutter_point */package com.xf.model;import java.sql.*;import java.util.*;public class MyCartBO{ //定义一个hashmap集合,用于存放书的ID和数量 HashMap<string string> hm=new HashMap<string string>(); //链接数据库的一些变量 private ResultSet rs=null; private Connection ct=null; private PreparedStatement ps=null; private float allPrice=0.0f; //得到这个商品的购买数量 public String getGoodsNumById(String goodsId) { return hm.get(goodsId); } //得到总价 public float getAllPrice() { return this.allPrice; } //1、添加货物 public void addGoods(String goodsId, String goodsNum) { hm.put(goodsId, goodsNum); } //2、删除货物 public void delGoods(String goodsId) { hm.remove(goodsId); } //3、清空货物 public void clear() { hm.clear(); } //4、修改货物数量 public void upGoods(String goodsId, String newNum) { hm.put(goodsId, newNum); } //5、显示购物车 public ArrayList<goodsbean> showMyCart() { ArrayList<goodsbean> a1=new ArrayList<goodsbean>(); try { String mysql="select * from `goods` where `goodsId` in ";//(1,4) //使用迭代器重hm中取出id Iterator<string> it=hm.keySet().iterator(); String sub="("; while(it.hasNext()) //迭代取出数据 { //取出goodsId String goodsId=(String)it.next(); //判断是不是最后一个id if(it.hasNext()) { sub+=goodsId+", "; } else { sub+=goodsId+")"; } } mysql+=sub; //链接数据库进行mysql语句使用 ct=new ConnDB().getConn(); //然后声明要使用MySQL语句了 ps=ct.prepareStatement(mysql); //得到语句执行结果 rs=ps.executeQuery(); //总价清空 this.allPrice=0.0f; while(rs.next()) { GoodsBean gb=new GoodsBean(); //吧查找到的数据放入到gb中 int goodsId=rs.getInt(1); gb.setGoodsId(goodsId); gb.setGoodsName(rs.getString(2)); gb.setGoodsIntro(rs.getString(3)); float unit=rs.getFloat(4); gb.setGoodsPrice(unit); gb.setGoodsNum(rs.getInt(5)); gb.setPublisher(rs.getString(6)); gb.setPhoto(rs.getString(7)); gb.setType(rs.getString(8)); this.allPrice+=unit*Integer.parseInt(this.getGoodsNumById(goodsId+"")); //吧goodsBean的数据放入到ArrayList里面去 a1.add(gb); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { this.close(); } return a1; } //关闭资源 public void close() { try { if(!rs.isClosed()) { rs.close(); rs=null; } if(!ps.isClosed()) { ps.close(); ps=null; } if(!ct.isClosed()) { ct.close(); ct=null; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } }}</string></goodsbean></goodsbean></goodsbean></string></string>
/** * 这是一个model和order表对应 */package com.xf.model;import java.util.Date;public class OrderBean{ private long ordersId; // '订单号' , private long userId; // '那个用户订的' , private Date orderDate; // '订单下的时期' , private String payMode; // '付款方式' , private byte isPayed; //'0表示没有付款,1表示已经付款' , private float totalPrice; //'总价格' , public long getOrdersId() { return ordersId; } public void setOrdersId(long ordersId) { this.ordersId = ordersId; } public long getUserId() { return userId; } public void setUserId(long userId) { this.userId = userId; } public Date getOrderDate() { return orderDate; } public void setOrderDate(Date orderDate) { this.orderDate = orderDate; } public String getPayMode() { return payMode; } public void setPayMode(String payMode) { this.payMode = payMode; } public byte getIsPayed() { return isPayed; } public void setIsPayed(byte isPayed) { this.isPayed = isPayed; } public float getTotalPrice() { return totalPrice; } public void setTotalPrice(float totalPrice) { this.totalPrice = totalPrice; } }
//这是一个model,对order,orderDetail的处理package com.xf.model;import java.sql.*;import java.util.*;public class OrderBeanBO{ //定义数据库相关变量 private ResultSet rs=null; private Connection ct=null; private PreparedStatement ps=null; /** * 生成订单 * @param mbo 用户的购物车 * @param userId 用户ID * @return 返回一个订单详细信息bean【OderInfoBean】 */ public OrderInfoBean addOrder(MyCartBO mcbo, String userId) { OrderInfoBean oifb=new OrderInfoBean(); boolean b=true; try { //得到链接 ct=new ConnDB().getConn(); //带入MySQL代码 String mysql="insert into `orders` (`userId`, `isPayed`, `totalPrice`) values(?,?,?)"; ps=ct.prepareStatement(mysql); ps.setString(1, userId); ps.setByte(2, (byte)0); ps.setFloat(3, mcbo.getAllPrice()); //执行 int a=ps.executeUpdate(); if(a == 1) { //还得取出刚刚添加到orders表的订单号 //取出最后那条就是了 ps=ct.prepareStatement("select max(`ordersId`) from `orders`"); rs=ps.executeQuery(); int orderId=0; if(rs.next()) { orderId=rs.getInt(1); } //orders表天机成功 //添加ordersDetail表 //从购物车中取出所有选购的货物 ArrayList<goodsbean> a1=mcbo.showMyCart(); //循环添加到orderDetail中 //使用批量执行操作数据库 Statement sm=ct.createStatement(); for(int i=0 ; i != a1.size() ; ++i) { GoodsBean gb=(GoodsBean)a1.get(i); String mysql2="insert into `orderDetail` values("+orderId+", "+gb.getGoodsId()+", "+mcbo.getGoodsNumById(gb.getGoodsId()+"")+")"; sm.addBatch(mysql2); } //批量执行 sm.executeBatch(); //顺带把要显示的订单细节 String mysql3="select `ordersId`, `truename`, `address`, `postcode`, `phone`, `totalPrice`, `username`, `email` from " + " `users`, `orders` where `ordersId`=? and `users`.userid= ( select `orders`.userId from " + " `orders` where `ordersId`=? )"; //执行语句 ps=ct.prepareStatement(mysql3); ps.setInt(1, orderId); ps.setInt(2, orderId); rs=ps.executeQuery(); if(rs.next()) { //将rs封装到OrderInfoBean oifb.setOrdersId(rs.getInt(1)); oifb.setTruename(rs.getString(2)); oifb.setAddress(rs.getString(3)); oifb.setPostcode(rs.getString(4)); oifb.setPhone(rs.getString(5)); oifb.setTotalPrice(rs.getFloat(6)); oifb.setUsername(rs.getString(7)); oifb.setEmail(rs.getString(8)); } } } catch (Exception e) { // TODO: handle exception b=false; e.printStackTrace(); } finally { this.close(); } if(b) { return oifb; } else { return null; } } //关闭资源 public void close() { try { if(!rs.isClosed()) { rs.close(); rs=null; } if(!ps.isClosed()) { ps.close(); ps=null; } if(!ct.isClosed()) { ct.close(); ct=null; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }</goodsbean>
package com.xf.model;public class OrderDetailBean{ private long orderIid; private long goodsId; private int nums; public long getOrderIid() { return orderIid; } public void setOrderIid(long orderIid) { this.orderIid = orderIid; } public long getGoodsId() { return goodsId; } public void setGoodsId(long goodsId) { this.goodsId = goodsId; } public int getNums() { return nums; } public void setNums(int nums) { this.nums = nums; } }
/** * 信息最全的bean */package com.xf.model;import java.util.Date;public class OrderInfoBean{ private long userid; // '用户id', private String username; // '用户名', private String truename; // '真实姓名', private String passwd; // '密码', private String email; // '电子邮件', private String phone; // '电话号码', private String address; // '用户地址', private String postcode; // '编邮', private int grade=1; // '户用级别', private long ordersId; // '订单号' , private long userId; // '那个用户订的' , private Date orderDate; // '订单下的时期' , private String payMode; // '付款方式' , private byte isPayed; //'0表示没有付款,1表示已经付款' , private float totalPrice; //'总价格' , public long getUserid() { return userid; } public void setUserid(long userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getTruename() { return truename; } public void setTruename(String truename) { this.truename = truename; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPostcode() { return postcode; } public void setPostcode(String postcode) { this.postcode = postcode; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public long getOrdersId() { return ordersId; } public void setOrdersId(long ordersId) { this.ordersId = ordersId; } public long getUserId() { return userId; } public void setUserId(long userId) { this.userId = userId; } public Date getOrderDate() { return orderDate; } public void setOrderDate(Date orderDate) { this.orderDate = orderDate; } public String getPayMode() { return payMode; } public void setPayMode(String payMode) { this.payMode = payMode; } public byte getIsPayed() { return isPayed; } public void setIsPayed(byte isPayed) { this.isPayed = isPayed; } public float getTotalPrice() { return totalPrice; } public void setTotalPrice(float totalPrice) { this.totalPrice = totalPrice; } }
package com.xf.model;public class UsersBean{ private long userid; // '用户id', private String username; // '用户名', private String truename; // '真实姓名', private String passwd; // '密码', private String email; // '电子邮件', private String phone; // '电话号码', private String address; // '用户地址', private String postcode; // '编邮', private int grade=1; // '户用级别', public long getUserid() { return userid; } public void setUserid(long userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getTruename() { return truename; } public void setTruename(String truename) { this.truename = truename; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPostcode() { return postcode; } public void setPostcode(String postcode) { this.postcode = postcode; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; }}
/** * 功能:处理有关users表的逻辑业务 * 时间:2014年7月10日15:22:10 * 作者:cutter_point */package com.xf.model;import java.sql.*;import com.xf.model.*;public class UsersBeanBo{ //连接数据库的一些变量 private Connection ct=null; private ResultSet rs=null; private PreparedStatement ps=null; /** * 更具用户名返回用户的全部信息 * @param u * @return UsersBean */ public UsersBean getUserBean(String u) { UsersBean ub=new UsersBean(); try { //连接 ct=new ConnDB().getConn(); //导入MySQL语句 String mysql="select * from `users` where username=? limit 0,1"; ps=ct.prepareStatement(mysql); ps.setString(1, u); //执行语句 rs=ps.executeQuery(); if(rs.next()) { ub.setUserid(rs.getLong(1)); ub.setUsername(rs.getString(2)); ub.setTruename(rs.getString(3)); ub.setPasswd(rs.getString(4)); ub.setEmail(rs.getString(5)); ub.setPhone(rs.getString(6)); ub.setAddress(rs.getString(7)); ub.setPostcode(rs.getString(8)); ub.setGrade(rs.getInt(9)); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { this.close(); } return ub; } /** * @author xiaofeng * @param u 账号 * @param p 密码 */ public boolean checkUser(String u, String p) { boolean b=false; try { //连接数据库 String mysql="select `passwd` from `users` where `username`=?"; //建立连接 ct=new ConnDB().getConn(); //导入语句 ps=ct.prepareStatement(mysql); ps.setString(1, u); //执行语句 rs=ps.executeQuery(); if(rs.next()) { //取出数据库密码和p比较 String dbPasswd=rs.getString(1); if(dbPasswd.equals(p)) { b=true; } } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { this.close(); } return b; } /** * @author xiaofeng * @param 关闭资源 */ public void close() { try { if(!rs.isClosed()) { rs.close(); rs=null; } if(!ps.isClosed()) { ps.close(); ps=null; } if(!ct.isClosed()) { ct.close(); ct=null; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }