GoodsDao.java
上传用户:toby828
上传日期:2015-06-26
资源大小:8558k
文件大小:8k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. package com.dao;
  2. import java.sql.*;
  3. import java.util.*;
  4. import com.tool.JDBConnection;
  5. import com.tool.FinalConstants;
  6. import com.domain.GoodsForm;
  7. //对商品表的操作
  8. public class GoodsDao {
  9.   private Connection connection = null; //定义连接的对象
  10.   private PreparedStatement ps = null; //定义预准备的对象
  11.   private JDBConnection jdbc = null; //定义数据库连接对象
  12.   public GoodsDao() {
  13.     jdbc = new JDBConnection();
  14.     connection = jdbc.connection; //利用构造方法取得数据库连接
  15.   }
  16. //根据商品的ID修改购买商品的数量
  17.   public void updateGoodsNumber(int number, Integer id) {
  18.     try {
  19.       ps = connection.prepareStatement(FinalConstants.goods_updateNumber);
  20.       ps.setInt(1, number);
  21.       ps.setInt(2, id.intValue());
  22.       ps.executeUpdate();
  23.       ps.close();
  24.     }
  25.     catch (SQLException ex) {
  26.     }
  27.   }
  28. //设置特价价格的方法
  29.   public void managerPrice(GoodsForm form) {
  30.     try {
  31.       ps = connection.prepareStatement(FinalConstants.goods_updatePirce);
  32.       ps.setFloat(1, form.getFreePrice().floatValue());
  33.       ps.setInt(2, form.getMark().intValue());
  34.       ps.setInt(3, form.getId().intValue());
  35.       ps.executeUpdate();
  36.       ps.close();
  37.     }
  38.     catch (SQLException ex) {
  39.     }
  40.   }
  41.   //以商品的是否特价为条件查询信息
  42.   public List selectMark(Integer mark) {
  43.     List list = new ArrayList();
  44.     GoodsForm goods = null;
  45.     try {
  46.       ps = connection.prepareStatement(FinalConstants.goods_selectMark);
  47.       ps.setInt(1, mark.intValue());
  48.       ResultSet rs = ps.executeQuery();
  49.       while (rs.next()) {
  50.         goods = new GoodsForm();
  51.         goods.setId(Integer.valueOf(rs.getString(1)));
  52.         goods.setBig(Integer.valueOf(rs.getString(2)));
  53.         goods.setSmall(Integer.valueOf(rs.getString(3)));
  54.         goods.setName(rs.getString(4));
  55.         goods.setFrom(rs.getString(5));
  56.         goods.setIntroduce(rs.getString(6));
  57.         goods.setCreaTime(rs.getString(7));
  58.         goods.setNowPrice(Float.valueOf(rs.getString(8)));
  59.         goods.setFreePrice(Float.valueOf(rs.getString(9)));
  60.         goods.setNumber(Integer.valueOf(rs.getString(10)));
  61.         goods.setPriture(rs.getString(11));
  62.         goods.setMark(Integer.valueOf(rs.getString(12)));
  63.         list.add(goods);
  64.       }
  65.     }
  66.     catch (SQLException ex) {
  67.     }
  68.     return list;
  69.   }
  70. //添加操作
  71.   public void insertGoods(GoodsForm form) {
  72.     try {
  73.       ps = connection.prepareStatement(FinalConstants.goods_insert);
  74.       ps.setInt(1, form.getBig().intValue());
  75.       ps.setInt(2, form.getSmall().intValue());
  76.       ps.setString(3, form.getName());
  77.       ps.setString(4, form.getFrom());
  78.       ps.setString(5, form.getIntroduce());
  79.       ps.setFloat(6, form.getNowPrice().floatValue());
  80.       ps.setFloat(7, form.getFreePrice().floatValue());
  81.       ps.setInt(8, 0);
  82.       ps.setString(9, form.getPicture());
  83.       ps.setInt(10, 0);
  84.       ps.executeUpdate();
  85.       ps.close();
  86.     }
  87.     catch (SQLException ex) {
  88.     }
  89.   }
  90. //以商品编号为条件删除信息
  91.   public void deleteGoods(Integer id) {
  92.     try {
  93.       ps = connection.prepareStatement(FinalConstants.goods_delete);
  94.       ps.setInt(1, id.intValue());
  95.       ps.executeUpdate();
  96.       ps.close();
  97.     }
  98.     catch (SQLException ex) {
  99.     }
  100.   }
  101. //以商品的编号为条件查询信息
  102.   public GoodsForm selectOneGoods(Integer id) {
  103.     GoodsForm goods = null;
  104.     try {
  105.       ps = connection.prepareStatement(FinalConstants.goods_selectOne);
  106.       ps.setInt(1, id.intValue());
  107.       ResultSet rs = ps.executeQuery();
  108.       while (rs.next()) {
  109.         goods = new GoodsForm();
  110.         goods.setId(Integer.valueOf(rs.getString(1)));
  111.         goods.setBig(Integer.valueOf(rs.getString(2)));
  112.         goods.setSmall(Integer.valueOf(rs.getString(3)));
  113.         goods.setName(rs.getString(4));
  114.         goods.setFrom(rs.getString(5));
  115.         goods.setIntroduce(rs.getString(6));
  116.         goods.setCreaTime(rs.getString(7));
  117.         goods.setNowPrice(Float.valueOf(rs.getString(8)));
  118.         goods.setFreePrice(Float.valueOf(rs.getString(9)));
  119.         goods.setNumber(Integer.valueOf(rs.getString(10)));
  120.         goods.setPriture(rs.getString(11));
  121.         goods.setMark(Integer.valueOf(rs.getString(12)));
  122.       }
  123.     }
  124.     catch (SQLException ex) {
  125.     }
  126.     return goods;
  127.   }
  128.   //以商品的小类别的编号为条件查询信息
  129.   public List selectSmall(Integer small) {
  130.     List list = new ArrayList();
  131.     GoodsForm goods = null;
  132.     try {
  133.       ps = connection.prepareStatement(FinalConstants.goods_selectSmall);
  134.       ps.setInt(1, small.intValue());
  135.       ResultSet rs = ps.executeQuery();
  136.       while (rs.next()) {
  137.         goods = new GoodsForm();
  138.         goods.setId(Integer.valueOf(rs.getString(1)));
  139.         goods.setBig(Integer.valueOf(rs.getString(2)));
  140.         goods.setSmall(Integer.valueOf(rs.getString(3)));
  141.         goods.setName(rs.getString(4));
  142.         goods.setFrom(rs.getString(5));
  143.         goods.setIntroduce(rs.getString(6));
  144.         goods.setCreaTime(rs.getString(7));
  145.         goods.setNowPrice(Float.valueOf(rs.getString(8)));
  146.         goods.setFreePrice(Float.valueOf(rs.getString(9)));
  147.         goods.setNumber(Integer.valueOf(rs.getString(10)));
  148.         goods.setPriture(rs.getString(11));
  149.         goods.setMark(Integer.valueOf(rs.getString(12)));
  150.         list.add(goods);
  151.       }
  152.     }
  153.     catch (SQLException ex) {
  154.     }
  155.     return list;
  156.   }
  157.   //以商品的大类别的编号为条件查询信息
  158.   public List selectBig(Integer big) {
  159.     List list = new ArrayList();
  160.     GoodsForm goods = null;
  161.     try {
  162.       ps = connection.prepareStatement(FinalConstants.goods_selectBig);
  163.       ps.setInt(1, big.intValue());
  164.       ResultSet rs = ps.executeQuery();
  165.       while (rs.next()) {
  166.         goods = new GoodsForm();
  167.         goods.setId(Integer.valueOf(rs.getString(1)));
  168.         goods.setBig(Integer.valueOf(rs.getString(2)));
  169.         goods.setSmall(Integer.valueOf(rs.getString(3)));
  170.         goods.setName(rs.getString(4));
  171.         goods.setFrom(rs.getString(5));
  172.         goods.setIntroduce(rs.getString(6));
  173.         goods.setCreaTime(rs.getString(7));
  174.         goods.setNowPrice(Float.valueOf(rs.getString(8)));
  175.         goods.setFreePrice(Float.valueOf(rs.getString(9)));
  176.         goods.setNumber(Integer.valueOf(rs.getString(10)));
  177.         goods.setPriture(rs.getString(11));
  178.         goods.setMark(Integer.valueOf(rs.getString(12)));
  179.         list.add(goods);
  180.       }
  181.     }
  182.     catch (SQLException ex) {
  183.     }
  184.     return list;
  185.   }
  186. //全部查询
  187.   public List selectGoods() {
  188.     List list = new ArrayList();
  189.     GoodsForm goods = null;
  190.     try {
  191.       ps = connection.prepareStatement(FinalConstants.goods_select);
  192.       ResultSet rs = ps.executeQuery();
  193.       while (rs.next()) {
  194.         goods = new GoodsForm();
  195.         goods.setId(Integer.valueOf(rs.getString(1)));
  196.         goods.setBig(Integer.valueOf(rs.getString(2)));
  197.         goods.setSmall(Integer.valueOf(rs.getString(3)));
  198.         goods.setName(rs.getString(4));
  199.         goods.setFrom(rs.getString(5));
  200.         goods.setIntroduce(rs.getString(6));
  201.         goods.setCreaTime(rs.getString(7));
  202.         goods.setNowPrice(Float.valueOf(rs.getString(8)));
  203.         goods.setFreePrice(Float.valueOf(rs.getString(9)));
  204.         goods.setNumber(Integer.valueOf(rs.getString(10)));
  205.         goods.setPriture(rs.getString(11));
  206.         goods.setMark(Integer.valueOf(rs.getString(12)));
  207.         list.add(goods);
  208.       }
  209.     }
  210.     catch (SQLException ex) {
  211.     }
  212.     return list;
  213.   }
  214.   //全部查询
  215.   public List selectGoodsNumber() {
  216.     List list = new ArrayList();
  217.     GoodsForm goods = null;
  218.     try {
  219.       ps = connection.prepareStatement(FinalConstants.goods_selectNumber);
  220.       ResultSet rs = ps.executeQuery();
  221.       while (rs.next()) {
  222.         goods = new GoodsForm();
  223.         goods.setId(Integer.valueOf(rs.getString(1)));
  224.         goods.setBig(Integer.valueOf(rs.getString(2)));
  225.         goods.setSmall(Integer.valueOf(rs.getString(3)));
  226.         goods.setName(rs.getString(4));
  227.         goods.setFrom(rs.getString(5));
  228.         goods.setIntroduce(rs.getString(6));
  229.         goods.setCreaTime(rs.getString(7));
  230.         goods.setNowPrice(Float.valueOf(rs.getString(8)));
  231.         goods.setFreePrice(Float.valueOf(rs.getString(9)));
  232.         goods.setNumber(Integer.valueOf(rs.getString(10)));
  233.         goods.setPriture(rs.getString(11));
  234.         goods.setMark(Integer.valueOf(rs.getString(12)));
  235.         list.add(goods);
  236.       }
  237.     }
  238.     catch (SQLException ex) {
  239.     }
  240.     return list;
  241.   }
  242. }