📄 employeedao.java
字号:
package com.buat.hr.employee;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.buat.hr.share.*;
public class EmployeeDAO extends ShareDAO {
DBConnection db = new DBConnection();
public boolean delEmployee(int id) { // 覆盖delete方法
boolean OK = true;
Connection con = db.getConnection();
PreparedStatement ps = null;
try {
ps = con
.prepareStatement("delete from employee where employeeId=?");
ps.setInt(1, id);
int col = ps.executeUpdate();
if (col > 0) {
OK = true;
}
} catch (Exception e) {
} finally {
try {
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (Exception e) {
}
}
return OK;
}
public ArrayList queryDepartment() { // 查询部门表 在添加表中返回部门名称
ArrayList departmentNameList = new ArrayList();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select name from department";
con = db.getConnection();
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
departmentNameList.add(rs.getString("name"));
}
} catch (SQLException e) {
} finally {
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (con != null)
ps.close();
} catch (Exception e) {
}
}
return departmentNameList;
}
public ArrayList queryDepartmentName() { // 部门表中提取名称返回到表中
ArrayList departmentNameList = new ArrayList();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select name from department";
con = db.getConnection();
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
departmentNameList.add(rs.getString("name"));
}
} catch (SQLException e) {
} finally {
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (con != null)
ps.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return departmentNameList;
}
public ArrayList query(int startNo, int perCount, String tableName) {// 查询库中信息后传到表中
// 分页
ArrayList list = new ArrayList();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String str = null;
con = db.getConnection();
str = "select * from " + tableName + " limit " + startNo + ","
+ perCount;
System.out.print(str);
try {
ps = con.prepareStatement(str.trim());
rs = ps.executeQuery();
while (rs.next()) {
Employee employee = new Employee();
employee.setEmployeeId(rs.getInt("employeeId"));
employee.setName(rs.getString("name"));
employee.setMajor(rs.getString("major"));
employee.setGender(rs.getString("gender"));
employee.setBirthday(rs.getDate("birthday"));
employee.setJointime(rs.getDate("jointime"));
employee.setDepartmentId(rs.getInt("departmentId"));
list.add(employee);
}
} catch (SQLException e) {
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
public ArrayList queryById(String tableName, int id) {
return null;
}
public Employee quaryToUpdate(String tableName, int id) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String str = null;
con = db.getConnection();
str = "select * from " + tableName + " where employeeId= " + id;
try {
ps = con.prepareStatement(str);
rs = ps.executeQuery();
rs.next();
Employee employee = new Employee();
employee.setEmployeeId(rs.getInt("employeeId"));
employee.setName(rs.getString("name"));
employee.setMajor(rs.getString("major"));
employee.setGender(rs.getString("gender"));
employee.setBirthday(rs.getDate("birthday"));
employee.setJointime(rs.getDate("jointime"));
employee.setDepartmentId(rs.getInt("departmentId"));
return employee;
} catch (SQLException e) {
return null;
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
}
}
}
public int queryEmployeeCount() // 表中记录的总数
{
int total = 0;
Connection con = db.getConnection();
String sql = "select count(*) from employee";
try {
PreparedStatement pstt = con.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE);
ResultSet rst = pstt.executeQuery();
if (rst.next()) {
total = rst.getInt(1);
con.close();
}
} catch (SQLException e) {
}
return total;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -