📄 employeedao.java~9~
字号:
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() {
}
/**
* 查询所有员工
* @return List 返回所有员工信息集合
*/
public List getAllEmployee(){
List list = null;
Connection con = DBAccess.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
String sql = "select * from employee";
try {
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
list = new ArrayList();
while(rs.next()){
Employee employee = new Employee();
employee.setEmpid(rs.getInt(1));
employee.setEmpName(rs.getString(2));
employee.setPosition(rs.getInt(3));
employee.setRemarks(rs.getString(4));
list.add(employee);
}
}
catch (Exception ex) {
ex.printStackTrace();
}finally{
try {
rs.close();
}
catch (SQLException ex1) {
ex1.printStackTrace();
}
DBAccess.closeStatement(pst);
DBAccess.closeConnection(con);
}
return list;
}
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;
}
//查询所有职员
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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -