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

📄 staffdao.java

📁 网上购物系统
💻 JAVA
字号:
package eshopsys.staff.model;

import java.sql.*;
import java.util.*;
import eshopsys.tools.base.BaseDao;
import eshopsys.tools.base.BaseEntity;
import eshopsys.tools.database.DataBaseTool;
import eshopsys.staff.model.*;

public class StaffDao extends BaseDao {
//////////////////////////////////////////////////////////////////////////
	protected BaseEntity[] pack(ResultSet resultset) throws SQLException {
		 ArrayList result = new ArrayList();
		 StaffEntity[] staffs = null;
		    while (resultset.next()) {
		      StaffEntity staff = new StaffEntity();
		      staff.setStaffId(resultset.getInt("staffid"));
		      staff.setStaffName(resultset.getString("staffname"));
              staff.setStaffSex(resultset.getString("staffsex"));
              staff.setStaffAccount(resultset.getString("staffaccount"));
              staff.setStaffPassword(resultset.getString("staffpassword"));
              staff.setStaffRight(resultset.getString("staffright"));
		      
		      result.add(staff);
		    }
		    if (result.size() > 0) {
		    	staffs= new StaffEntity[result.size()];
		      result.toArray(staffs);
		    }
		    return staffs;

	}
//////////////////////////////////////////////////////////////////////////	
	public void insert(Connection con, StaffEntity staff) throws
    SQLException {
    PreparedStatement ps = null;
    StringBuffer buffer = new StringBuffer();
    try {
    //构造添加sql语句
    buffer.append("INSERT INTO tbl_staff(staffid,staffname,staffsex,staffaccount,staffpassword,staffright)");
    buffer.append(" VALUES(?,?,?,?,?,?)");

    ps = con.prepareStatement(buffer.toString());
    //设置sql语句的参数
    int index = 1;
    int currentId=getMaxPrimaryId("tbl_staff","staffid");
    ps.setInt(index++,currentId);
    ps.setString(index++, staff.getStaffName());
    ps.setString(index++, staff.getStaffSex());
    ps.setString(index++, staff.getStaffAccount());
    ps.setString(index++, staff.getStaffPassword());
    ps.setString(index++, staff.getStaffRight());

    
    //执行sql语句
    ps.executeUpdate();
  }
  catch (Exception e) {
    throw new SQLException("向表(tbl_staff)中插入数据发生异常 : " + e.getMessage());
  }
  finally {
    DataBaseTool.close(ps);
  }
 }
//////////////////////////////////////////////////////////////////////////
	//更新一条会员记录
	  public void update(Connection con, StaffEntity staff) throws     
	 SQLException {
	    PreparedStatement ps = null;
	    StringBuffer buffer = new StringBuffer();
	    try {
	      //构造更新sql语句
	      buffer.append("UPDATE tbl_staff SET ");
	      buffer.append("staffid=?,staffname=?,staffsex=?,staffaccount=?,staffpassword=?,staffright=?");
	      buffer.append(" WHERE staffid = ?");
	      ps = con.prepareStatement(buffer.toString());

	      //设置参数
	      int index = 1;
	      ps.setInt(index++, staff.getStaffId());
	      ps.setString(index++, staff.getStaffName());
	      ps.setString(index++, staff.getStaffSex());
	      ps.setString(index++, staff.getStaffAccount());
	      ps.setString(index++, staff.getStaffPassword());
	      ps.setString(index++, staff.getStaffRight());
	      ps.setInt(index++, staff.getStaffId());
	      //执行
	      ps.executeUpdate();
	    }
	    catch (Exception e) {
	      throw new SQLException("更新表(tbl_staff)时发生异常 : " + e.getMessage());
	    }
	    finally {
	      DataBaseTool.close(ps);
	    }
	  }
//////////////////////////////////////////////////////////////////////////
//管理员登录测试
	  public boolean StaffLoginTest(String staffaccount,String staffpassword) throws 
	  SQLException{
		  Connection con=DataBaseTool.getConnection();
		  boolean loginFlag=false;
		  PreparedStatement ps = con.prepareStatement("select * from tbl_staff where staffaccount=? and staffpassword=?");
		  ps.setString(1, staffaccount.trim());
		  ps.setString(2,staffpassword.trim());
		  ResultSet rs=ps.executeQuery();
		  if(rs.next())
			  loginFlag=true;
		  
		  return loginFlag;
	  }
}

⌨️ 快捷键说明

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