📄 employeedao.java
字号:
package com.buat.employee;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.buat.connect.Connect;
public class EmployeeDAO implements IEmployeeDAO {
public String sqlInfo=null;
public int maxLength=0;
public int countInfo=0;
public PreparedStatement pst=null;
public ResultSet rs=null;
public ArrayList employeeList=null;
//*******************************************删除员工
public boolean deleteEmployee(int employeeId) {
boolean delSuccessful=false;
Connect.getConnect();
sqlInfo="delete from employee where employeeId =? ";
try {
pst=Connect.con.prepareStatement(sqlInfo);
pst.setInt(1, employeeId);
int result=pst.executeUpdate();
if(result>0){
delSuccessful=true;
System.out.println("删除员工成功!");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(pst!=null){
pst.close();
}
if(Connect.con.isClosed()==false){
Connect.con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return delSuccessful;
}
//*******************************************修改员工
public boolean updateEmployee(Employee employee) {
boolean updateSuccessful=false;
sqlInfo="update employee set name=?, major=?, gender=?, birthday=?, departmentId=? where employeeId=? ";
Connect.getConnect();
try {
pst=Connect.con.prepareStatement(sqlInfo);
pst.setString(1, employee.getName());
pst.setString(2, employee.getMajor());
pst.setString(3, employee.getGender());
pst.setDate(4, employee.getBirthday());
pst.setInt(5, employee.getDepartmentId());
pst.setInt(6, employee.getEmployeeId());
int getInfo = pst.executeUpdate();
if(getInfo > 0){
updateSuccessful=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return updateSuccessful;
}
//*******************************************添加员工
public boolean addEmployee(Employee employee) {
boolean addSuccessful=false;
sqlInfo="insert into employee(name,major,gender,birthday,departmentId) values(?,?,?,?,?)";
new Connect().getConnect();
try {
pst=Connect.con.prepareStatement(sqlInfo);
pst.setString(1, employee.getName());
pst.setString(2, employee.getMajor());
pst.setString(3, employee.getGender());
pst.setDate(4, employee.getBirthday());
pst.setInt(5, employee.getDepartmentId());
int result=pst.executeUpdate();
if(result>0){
addSuccessful=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return addSuccessful;
}
//*******************************************浏览员工
public ArrayList queryEmployee(int beginIndex,int endIndex) {
employeeList = new ArrayList();
sqlInfo="select * from employee order by employeeid desc limit ?,?";
maxLength=endIndex-beginIndex+1;
new Connect().getConnect();
try {
pst=Connect.con.prepareStatement(sqlInfo);
pst.setInt(1, beginIndex);
pst.setInt(2, maxLength);
rs=pst.executeQuery();
while(rs.next()){
Employee employee=new Employee();
employee.setEmployeeId(rs.getInt(1));
employee.setName(rs.getString(2));
employee.setMajor(rs.getString(3));
employee.setGender(rs.getString(4));
employee.setBirthday(rs.getDate(5));
employee.setDepartmentId(rs.getInt(6));
employeeList.add(employee);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return employeeList;
}
//*******************************************按编号查询
public ArrayList queryEmployeeById(int interviewerId) {
employeeList = new ArrayList();
sqlInfo="select * from employee where employeeId = ? ";
new Connect().getConnect();
try {
pst=Connect.con.prepareStatement(sqlInfo);
pst.setInt(1, interviewerId);
rs=pst.executeQuery();
while(rs.next()){
Employee employee=new Employee();
employee.setName(rs.getString(2));
employee.setMajor(rs.getString(3));
employee.setGender(rs.getString(4));
employee.setBirthday(rs.getDate(5));
employee.setDepartmentId(rs.getInt(6));
employeeList.add(employee);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return employeeList;
}
//*******************************************获得员工总数
public int getCountEmployee() {
sqlInfo="select count(*) from employee";
new Connect().getConnect();
try {
pst=Connect.con.prepareStatement(sqlInfo);
rs=pst.executeQuery();
while(rs.next()){
countInfo=rs.getInt(1);
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return countInfo;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -