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

📄 vipdao.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.VipVo;

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

/**
 * 会员Dao
 * @author linfeng
 *
 */
public class VipDao {

  /**
   * 获得会员的全部信息
   * @return vector
   */
  public Vector getVipInfo() {
    Vector vector = new Vector();
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_VIP_INFO);
      set = stmt.executeQuery();
      while (set.next()) {
        int vipId = set.getInt(1);
        String vipName = set.getString(2);
        String vipType = set.getString(3);
        double pricePct = set.getDouble(4);
        Long vipPhone = set.getLong(5);
        VipVo value = new VipVo(vipId, vipName, vipType, pricePct, vipPhone);
        vector.add(value);
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }

    return vector;
  }

  /**
   * 查找数据库中是否存在某一编号的会员
   * @param vipId 会员编号
   * @return flag
   */
  public boolean findVipById(int vipId) {
    Connection con = null;
    PreparedStatement stmt = null;
    boolean flag = false;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_VIP_BY_ID);
      stmt.setInt(1, vipId);
      set = stmt.executeQuery();
      if (set.next()) {
        flag = true;
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return flag;
  }

  /**
   * 获得数据库中存在某种编号的会员
   * @param vipId 会员编号
   * @return v
   */
  public Vector getVipById(int vipId) {
    Vector v = new Vector();
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_VIP_BY_ID);
      stmt.setInt(1, vipId);
      set = stmt.executeQuery();
      while (set.next()) {
        int vip_id = set.getInt("vip_id");
        String vip_name = set.getString("vip_name");
        String vip_type = set.getString("vip_type");
        double price_pct = set.getDouble("price_pct");
        long phone = set.getLong("phone");
        VipVo vo = new VipVo(vip_id, vip_name, vip_type, price_pct, phone);
        v.addElement(vo);
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return v;
  }
  
  /**
   * 将输入的会员信息插入到数据库中
   * @param value VipVo对象
   * @return flag
   * @throws DbException 自定义异常
   */
  public boolean insertVip(VipVo value) throws DbException {
    Connection con = null;
    PreparedStatement stmt = null;
    boolean flag = false;
    try {
      if (findVipById(value.getVip_id())) {
        throw new DbException("数据库中存在相同条件的记录");
      }
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.INSERT_VIP);
      stmt.setInt(1, value.getVip_id());
      stmt.setString(2, value.getVip_name());
      stmt.setString(3, value.getVip_type());
      stmt.setDouble(4, value.getPrice_pct());
      stmt.setLong(5, value.getPhone());

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

  }

  /**
   * 修改会员的信息
   * @param value VipVo对象
   * @return flag
   */
  public boolean updateVip(VipVo value) {
    Connection con = null;
    PreparedStatement stmt = null;
    boolean flag = false;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.UPDATE_VIP);
      stmt.setInt(1, value.getVip_id());
      stmt.setString(2, value.getVip_name());
      stmt.setString(3, value.getVip_type());
      stmt.setDouble(4, value.getPrice_pct());
      stmt.setLong(5, value.getPhone());
      stmt.setInt(6, value.getVip_id());
      int count = stmt.executeUpdate();
      if (count != 0) {
        flag = true;
      }

    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return flag;
  }

  /**
   * 根据输入的会员编号删除会员
   * @param vipId 会员编号
   * @return flag
   */
  public boolean deleteVipById(int vipId) {
    Connection con = null;
    PreparedStatement stmt = null;
    boolean flag = false;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.DELETE_VIP_BY_ID);
      stmt.setInt(1, vipId);
      int count = stmt.executeUpdate();
      if (count != 0) {
        flag = true;
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return flag;
  }

  /**
   * 根据输入的会员姓名删除会员
   * @param vipName 会员姓名
   * @return flag
   */
  public boolean deleteVipByName(String vipName) {
    Connection con = null;
    PreparedStatement stmt = null;
    boolean flag = false;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.DELETE_VIP_BY_NAME);
      stmt.setString(1, vipName);
      int count = stmt.executeUpdate();
      if (count != 0) {
        flag = true;
      }
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return flag;
  }
  
  /**
   * 根据输入的会员编号取得会员姓名
   * @param inputId 会员编号
   * @return vipName 会员姓名
   */
  public String getVipNameById(int inputId){
    String vipName = "";
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_VIP_NAME_BY_ID);
      stmt.setInt(1,inputId);
      set = stmt.executeQuery();
      while (set.next()) {        
       vipName = set.getString(1);                       
      }
      
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return vipName;
  }
  
  /**
   * 根据输入的会员编号取得打折指数
   * @param inputId 会员编号
   * @return vipPct 打折指数
   */
  public double getVipPctById(int inputId){
    double vipPct = 0;
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_VIP_PCT_BY_ID);
      stmt.setInt(1,inputId);
      set = stmt.executeQuery();
      while (set.next()) {        
        vipPct = set.getDouble(1);                       
      }
      
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return vipPct;
  }
  
  /**
   * 根据输入的会员编号取得会员类型
   * @param inputId 会员编号
   * @return vipType 会员类型
   */
  public String getVipTypeById(int inputId){
    String vipType = null;
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_VIP_TYPE_BY_ID);
      stmt.setInt(1,inputId);
      set = stmt.executeQuery();
      while (set.next()) {        
        vipType = set.getString(1);                       
      }
      
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return vipType;
  }
  
   /**
    * 根据输入的会员编号取得会员类型
    * @param inputId 会员编号
    * @return vipPhone 会员电话
    */
  public long getVipPhoneById(int inputId){
    long vipPhone = 0;
    Connection con = null;
    PreparedStatement stmt = null;
    ResultSet set = null;
    try {
      con = new DbConnection().getConnection();
      stmt = con.prepareStatement(SqlConst.SELECT_VIP_PHONE_BY_ID);
      stmt.setInt(1,inputId);
      set = stmt.executeQuery();
      while (set.next()) {        
        vipPhone = set.getLong(1);                       
      }
      
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return vipPhone;
  }
}

⌨️ 快捷键说明

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