📄 employeedao.java
字号:
/**
* -----------------------------------------------------------------------------
* 创建时间:2006年2月15日
* 作 者:孙丰伟
* 功 能:员工管理,系统对员工的管理主要有(参看案例分析中的用例图):
* 1.建立新员工, method: addEmployee()
* 2.修改员工, method: updateEmployee()
* 3.删除员工, method: removeEmployeeById,要确保员工
* 在其他表中不被引用
* 4.模糊查询 method: getEmployees()
* 5.根据部门查询员工, method: getEmployeesByDepartment()
* 6.返回所有员工, method: getAllEmployees()
* 7.查找最后一个员工号,method:getLastEmployeeId()
* ------------------------------------------------------------------------------
*/
package cn.sunfengwei.employee.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.HashMap;
import java.util.Vector;
import cn.sunfengwei.employee.db.ConnectDB;
public class EmployeeDAO {
private EmployeeDTO employee;
private Connection con;
private PreparedStatement pstmt;
private Statement stmt;
private ResultSet rs;
public EmployeeDAO() {
}
// 1.添加新员工
public boolean addEmployee(EmployeeDTO employee)
{
boolean flag = false;
con = ConnectDB.getConnection();
String strQuery = "insert into employee values(?,?,?,?,?,?)";
try {
pstmt = con.prepareStatement(strQuery);
pstmt.setInt(1, employee.getEID());
pstmt.setString(2, employee.getName());
pstmt.setString(3, employee.getSex());
pstmt.setDate(4, employee.getBirthday());
pstmt.setInt(5,employee.getDepartmentId());
pstmt.setString(6, employee.getPhone());
if (pstmt.executeUpdate() > 0)
flag = true;
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
//e.printStackTrace();
}
return flag;
}
//2.修改员工信息
public boolean updateEmployee(EmployeeDTO employee)
{
boolean flag = false;
con = ConnectDB.getConnection();
String strQuery ="update employee set name=?,sex=?,birthday=?,departmentId=?,phone=? where eid=?";
try {
pstmt = con.prepareStatement(strQuery);
pstmt.setString(1, employee.getName());
pstmt.setString(2, employee.getSex());
pstmt.setDate(3, employee.getBirthday());
pstmt.setInt(4,employee.getDepartmentId());
pstmt.setString(5, employee.getPhone());
pstmt.setInt(6, employee.getEID());
if (pstmt.executeUpdate() > 0)
flag = true;
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
System.out.println(e.getMessage());
}
return flag;
}
// 3.移除员工
public boolean removeEmployeeById(int EID)
{
boolean flag = false;
con = ConnectDB.getConnection();
String strQuery = "delete from employee where EID=?";
try {
pstmt = con.prepareStatement(strQuery);
pstmt.setInt(1,EID);
if (pstmt.executeUpdate() > 0)
flag = true;
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return flag;
}
//4. 此方法用来实现模糊查询,程序中暂未实现
public Collection getEmployees(EmployeeDTO Employee)
{
Vector departments=new Vector();
return departments;
}
//5. 根据部门查找员工
public Vector getEmployeesByDepartment(int departmentId)
{
//HashMap departments=new HashMap();
Vector departments=new Vector();
con = ConnectDB.getConnection();
String strQuery = "select * from viewEmployee where 部门编号=?";
try {
pstmt = con.prepareStatement(strQuery);
pstmt.setInt(1, departmentId);
rs=pstmt.executeQuery();
while(rs.next())
{
this.employee=new EmployeeDTO();
this.employee.setEID(rs.getInt(1));
this.employee.setName(rs.getString(2));
this.employee.setSex(rs.getString(3));
this.employee.setBirthday(rs.getDate(4));
this.employee.setDepartmentId(rs.getInt(5));
this.employee.setDepartmentName(rs.getString(6));
this.employee.setPhone(rs.getString(7));
//departments.put(new Integer(this.employee.getEID()),this.employee);
departments.add(this.employee);
}
rs.close();
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return departments;
}
//6. 返回所有的员工
public Vector getAllEmployees()
{
//HashMap departments=new HashMap();
Vector departments=new Vector();
con = ConnectDB.getConnection();
String strQuery = "select * from viewEmployee order by 部门编号";
try {
pstmt = con.prepareStatement(strQuery);
rs=pstmt.executeQuery();
while(rs.next())
{
this.employee=new EmployeeDTO();
this.employee.setEID(rs.getInt(1));
this.employee.setName(rs.getString(2));
this.employee.setSex(rs.getString(3));
this.employee.setBirthday(rs.getDate(4));
this.employee.setDepartmentId(rs.getInt(5));
this.employee.setDepartmentName(rs.getString(6));
this.employee.setPhone(rs.getString(7));
//departments.put(new Integer(this.employee.getEID()),this.employee);
departments.add(this.employee);
}
rs.close();
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return departments;
}
//8. 返回最大的员工号
public int getLastEmployeeId()
{
int id=0;
con = ConnectDB.getConnection();
String strQuery = "select max(EID) from Employee";
try {
stmt = con.createStatement();
rs=stmt.executeQuery(strQuery);
if(rs.next())
{
id=rs.getInt(1);
}
else
{
id=0;
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return id;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -