⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stockdao.java

📁 好的超市源码供大家下载
💻 JAVA
字号:
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import vo.StockProductVo;

import common.jdbc.DbConnection;
import common.jdbc.DbException;
import common.jdbc.SqlConst;

/**
 * 进货Dao
 * @author linfeng
 *
 */
public class StockDao {
  
  /**
   * 检索进货商品的信息
   * @return vector
   */
  public Vector getStockInfo() {
    Vector vector = new Vector();
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_STOCK_INFO);
      set = stmt.executeQuery();
      while (set.next()) {
        int productId = set.getInt(1);
        String productName = set.getString(2);
        String productType = set.getString(3);
        double productPrice = set.getDouble(4);
        int stockNum = set.getInt(5);
        String stockDate = set.getString(6);
        StockProductVo value = new StockProductVo(productId, productName,
            productType, productPrice, stockNum, stockDate);
        vector.addElement(value);
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }

    return vector;
  }

  /**
   * 根据输入的商品编号来取得进货的全部信息
   * @param inputId 商品编号
   * @return v
   */
  public Vector getStockById(int inputId) {
    Vector v = new Vector();
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_STOCK_BY_ID);
      stmt.setInt(1, inputId);
      set = stmt.executeQuery();
      while (set.next()) {
        int proId = set.getInt(1);
        String proName = set.getString(2);
        String proType = set.getString(3);
        double proPrice = set.getDouble(4);
        int stoNum = set.getInt(5);
        String stoDate = set.getString(6);
        StockProductVo vo = new StockProductVo(proId, proName, proType,
            proPrice, stoNum, stoDate);
        v.addElement(vo);
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return v;
  }

  /**
   * 根据输入的商品名称来取得进货的全部信息
   * @param inputName 商品名称
   * @return v
   */
  public Vector getStockByName(String inputName) {
    Vector v = new Vector();
    Connection con = null;
    PreparedStatement stmt = null;
    boolean flag = false;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_STOCK_BY_NAME);
      stmt.setString(1, inputName);
      set = stmt.executeQuery();
      while (set.next()) {
        int proId = set.getInt(1);
        String proName = set.getString(2);
        String proType = set.getString(3);
        double proPrice = set.getDouble(4);
        int stoNum = set.getInt(5);
        String stoDate = set.getString(6);

        StockProductVo vo = new StockProductVo(proId, proName, proType,
            proPrice, stoNum, stoDate);
        v.addElement(vo);
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return v;
  }

  /**
   * 根据输入的商品类型来取得进货的全部信息
   * @param inputType 商品类型
   * @return v
   */
  public Vector getStockByType(String inputType) {
    Vector v = new Vector();
    Connection con = null;
    PreparedStatement stmt = null;
    boolean flag = false;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_STOCK_BY_TYPE);
      stmt.setString(1, inputType);
      set = stmt.executeQuery();
      while (set.next()) {
        int proId = set.getInt(1);
        String proName = set.getString(2);
        String proType = set.getString(3);
        double proPrice = set.getDouble(4);
        int stoNum = set.getInt(5);
        String stoDate = set.getString(6);

        StockProductVo vo = new StockProductVo(proId, proName, proType,
            proPrice, stoNum, stoDate);
        v.addElement(vo);
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return v;
  }

  /**
   * 根据输入的进货日期来取得进货的全部信息
   * @param inputDate 进货日期
   * @return v
   */
  public Vector getStockByDate(String inputDate) {
    Vector v = new Vector();
    Connection con = null;
    PreparedStatement stmt = null;
    boolean flag = false;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_STOCK_BY_DATE);
      stmt.setString(1, inputDate);
      set = stmt.executeQuery();
      while (set.next()) {
        int proId = set.getInt(1);
        String proName = set.getString(2);
        String proType = set.getString(3);
        double proPrice = set.getDouble(4);
        int stoNum = set.getInt(5);
        String stoDate = set.getString(6);

        StockProductVo vo = new StockProductVo(proId, proName, proType,
            proPrice, stoNum, stoDate);
        v.addElement(vo);
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return v;
  }



  /**
   * 将输入的商品进货信息插入到数据库中
   * @param value StockProductVo对象
   * @return flag
   * @throws DbException 自定义异常
   */
  public boolean insertStock(StockProductVo value) throws DbException {
    Connection con = null;
    PreparedStatement stmt = null;
    boolean flag = false;
    try {

      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.INSERT_STOCK);
      stmt.setInt(1, value.getProduct_id());
      stmt.setString(2, value.getProduct_name());
      stmt.setString(3, value.getProduct_type());
      stmt.setDouble(4, value.getProduct_price());
      stmt.setInt(5, value.getStock_num());
      stmt.setString(6, value.getStock_date());

      int count = stmt.executeUpdate();
      if (count != 0) {
        flag = true;
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return flag;

  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -