📄 employeedao.java
字号:
package com.cdaccp.dao;
import java.util.ArrayList;
import com.util.DBAccess;
import java.sql.*;
import com.cdaccp.entity.Employee;
import java.util.List;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2008</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class EmployeeDAO {
public EmployeeDAO() {
}
//添加职工
public boolean addEmployee(Employee emp) {
Connection con = DBAccess.getConnection();
String sql = "insert into employee values(?,?,?)";
PreparedStatement pst = null;
try {
pst = con.prepareStatement(sql);
pst.setString(1, emp.getEmpName());
pst.setInt(2, emp.getPosition());
pst.setString(3, emp.getRemarks());
int i = pst.executeUpdate();
if (i > 0) {
return true;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
DBAccess.closeStatement(pst);
DBAccess.closeConnection(con);
}
return false;
}
/**
* 查询所有员工
* @return List 返回所有员工信息集合
*/
public List SelectEmployee() {
List list = new ArrayList();
Connection con = DBAccess.getConnection();
String sql = "select employee.* ,job.jobName from employee inner join job on (employee.position = job.jobid) ";
PreparedStatement pst = null;
try {
pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Employee emp = new Employee();
emp.setEmpid(rs.getInt("empid"));
emp.setEmpName(rs.getString("empname"));
emp.setPosition(rs.getInt("position"));
emp.setRemarks(rs.getString("remarks"));
emp.setJopName(rs.getString("jobname"));
list.add(emp);
}
rs.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
DBAccess.closeStatement(pst);
DBAccess.closeConnection(con);
}
return list;
}
//修改职员
public boolean UpdateEmployee(Employee emp) {
Connection con = DBAccess.getConnection();
String sql =
"update employee set empname = ?,position = ?,remarks = ? where empid = ?";
PreparedStatement pst = null;
try {
pst = con.prepareStatement(sql);
pst.setString(1, emp.getEmpName());
pst.setInt(2, emp.getPosition());
pst.setString(3, emp.getRemarks());
pst.setInt(4, emp.getEmpid());
int i = pst.executeUpdate();
if (i > 0) {
return true;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
DBAccess.closeStatement(pst);
DBAccess.closeConnection(con);
}
return false;
}
//删除职员
public boolean DeleteEmployee(int id) {
Connection con = DBAccess.getConnection();
String sql = "delete from employee where empid = ?";
PreparedStatement pst = null;
try {
pst = con.prepareStatement(sql);
pst.setInt(1, id);
int i = pst.executeUpdate();
if (i > 0) {
return true;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
DBAccess.closeStatement(pst);
DBAccess.closeConnection(con);
}
return false;
}
//姓名糊模查找
public List loadEmployeeName(String name) {
List list = new ArrayList();
Connection con = DBAccess.getConnection();
String sql = "select employee.* ,job.jobName from employee inner join job on (employee.position = job.jobid) where empname like '" +
name + "%' ";
PreparedStatement pst = null;
try {
pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Employee emp = new Employee();
emp.setEmpid(rs.getInt("empid"));
emp.setEmpName(rs.getString("empname"));
emp.setPosition(rs.getInt("position"));
emp.setRemarks(rs.getString("remarks"));
emp.setJopName(rs.getString("jobname"));
list.add(emp);
}
rs.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
DBAccess.closeStatement(pst);
DBAccess.closeConnection(con);
}
return list;
}
//按工号查找
public List loadEmployeeid(int empId) {
List list = new ArrayList();
Connection con = DBAccess.getConnection();
String sql = "select employee.* ,job.jobName from employee inner join job on (employee.position = job.jobid) where empid like '"+empId+"%'";
PreparedStatement pst = null;
try {
pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Employee emp = new Employee();
emp.setEmpid(rs.getInt("empid"));
emp.setEmpName(rs.getString("empname"));
emp.setPosition(rs.getInt("position"));
emp.setRemarks(rs.getString("remarks"));
emp.setJopName(rs.getString("jobname"));
list.add(emp);
}
rs.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
DBAccess.closeStatement(pst);
DBAccess.closeConnection(con);
}
return list;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -