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

📄 storedao.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 vo.StoreVo;

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

/**
 * 存货Dao
 * @author linfeng
 *
 */
public class StoreDao {
   
   /**
    * 查找数据库中是否存在某一编号的商品
    * @param inputId 商品编号
    * @return flag
    */
   public boolean findStoreById(int inputId) {
   Connection con = null;
   PreparedStatement stmt = null;
   boolean flag = false;
   ResultSet set = null;
   try {
   con = new DbConnection().getConnection();
   stmt = con.prepareStatement(SqlConst.SELECT_STORE_BY_ID);
   stmt.setInt(1, inputId);
   set = stmt.executeQuery();
   if (set.next()) {
   flag = true;
   }
   } catch (SQLException e) {
   System.out.println(e.getMessage());
   }
   return flag;
   }
  
  /**
   * 检索进货商品的信息
   * @return vector
   */
  public Vector getStoreInfo() {
    Vector vector = new Vector();
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_STORE_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);
        StoreVo value = new StoreVo(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 getStoreById(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_STORE_BY_ID);
      stmt.setInt(1,inputId);
      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);
        StoreVo value = new StoreVo(productId, productName,
            productType, productPrice, stockNum, stockDate);
        v.addElement(value);
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }

    return v;
  }

  /**
   * 根据商品编号检索商品信息
   * @param inputName 商品信息
   * @return
   */
  public Vector getStoreByName(String inputName){
    Vector v = new Vector();
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_STORE_BY_NAME);
      stmt.setString(1,"%"+inputName+"%");
      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);
        StoreVo value = new StoreVo(productId, productName,
            productType, productPrice, stockNum, stockDate);
        v.addElement(value);
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }

    return v;
  }

  /**
   * 根据商品类型检索商品信息
   * @param inputType 商品类型
   * @return v
   */
  public Vector getStoreByType(String inputType){
    Vector v = new Vector();
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_STORE_BY_TYPE);
      stmt.setString(1,inputType);
      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);
        StoreVo value = new StoreVo(productId, productName,
            productType, productPrice, stockNum, stockDate);
        v.addElement(value);
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }

    return v;
  }

  /**
   * 根据商品售出时间检索商品信息
   * @param inputDate
   * @return v
   */
  public Vector getStoreByDate(String inputDate){
    Vector v = new Vector();
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_STORE_BY_DATE);
      stmt.setString(1,inputDate);
      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);
        StoreVo value = new StoreVo(productId, productName,
            productType, productPrice, stockNum, stockDate);
        v.addElement(value);
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }

    return v;
  }
  
  /**
   * 根据商品编号修改商品信息
   * @param inputId 商品编号
   * @param saleNum 销售数量
   */
  public void updateStoreById(int inputId ,int saleNum){
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.UPDATE_STORE_BY_ID);
      stmt.setInt(1,saleNum);
      stmt.setInt(2,inputId);
      set = stmt.executeQuery();
      
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    
  }
  
  /**
   * 根据商品编号获得存货商品名称
   * @param inputId 商品编
   * @return productName 商品名称
   */
  public String getStoreNameById(int inputId){
    String productName = "";
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_STORE_NAME_BY_ID);
      stmt.setInt(1,inputId);
      set = stmt.executeQuery();
      while (set.next()) {        
       productName = set.getString(1);                       
      }
      
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return productName;
  }
  
  /**
   * 根据商品编号获得存货商品价格
   * @param inputId 商品编号
   * @return productPrice 商品价格
   */
  public double getStorePriceById(int inputId){
    double productPrice = 0;
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_STORE_PRICE_BY_ID);
      stmt.setInt(1,inputId);
      set = stmt.executeQuery();
      while (set.next()) {        
        productPrice = set.getDouble(1);                       
      }
      
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return productPrice;
  }
  
  /**
   * 根据商品编号获得存货商品类型
   * @param inputId 商品编号
   * @return productType 商品类型
   */
  public String getStoreTypeById(int inputId){
    String productType = null;
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_STORE_TYPE_BY_ID);
      stmt.setInt(1,inputId);
      set = stmt.executeQuery();
      while (set.next()) {        
        productType = set.getString(1);                       
      }
      
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return productType;
  }
  
  /**
   * 根据商品编号获得存货商品存货数量
   * @param inputId 商品编号
   * @return productNum 商品存货数量
   */
  public int getStoreNumById(int inputId){
    int productNum = 0;
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_STORE_NUM_BY_ID);
      stmt.setInt(1,inputId);
      set = stmt.executeQuery();
      while (set.next()) {        
        productNum = set.getInt(1);                       
      }
      
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return productNum;
  }

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

      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.INSERT_STORE);
      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 + -