package database;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
import database.Database;
import hrbean.Jobchangebean;
import hrbean.Person;
import hrbean.attence;
import hrbean.Leavebean;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
/**
* 该类用来实现员工信息的增删查改。
*/
public class PersonBean {
ResultSet rs = null;
String sql; // 存放sql语句
String pId; // 员工编号
String pName; // 员工姓名
String pSex; // 员工性别
String pBirth; // 出生年月
String pAddress; // 地址
String Did; // 所属部门编号
String Duties; // 民族
String Salary; // 工资
String Assess; // 是否考核
String Phone; // 联系方式
/**
* @param name 员工姓名
* @param sex 员工性别
* @param birth 员工出生年月
* @param address 员工地址
* @return 如果信息合法,返回true,否则返回false
*/
public boolean isLegal(String name, String sex, String birth, String address) {
if (name == null || name.equals("")) {
JOptionPane.showMessageDialog(null, "请输入姓名", "错误", JOptionPane.ERROR_MESSAGE);
return false;
}
if (sex == null || sex.equals("")) {
JOptionPane.showMessageDialog(null, "请输入性别", "错误", JOptionPane.ERROR_MESSAGE);
return false;
}
if (birth == null || birth.equals("")) {
JOptionPane.showMessageDialog(null, "请输入出生年月", "错误", JOptionPane.ERROR_MESSAGE);
return false;
}
if (address == null || address.equals("")) {
JOptionPane.showMessageDialog(null, "请输入地址", "错误", JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
/**
*
* @param id 员工编号
* @param name 员工姓名
* @param sex 员工性别
* @param birth 员工出生年月
* @param address 员工家庭住址
* @param did 员工所属部门
* @param salary 员工薪资
*/
public void addInfo(String id, String name, String sex, String birth, String address, String did, String duties,
String salary, String check,String phone) {
// 如果信息合法
this.pId = id;
this.pName = name;
this.pSex = sex;
this.pBirth = birth;
this.pAddress = address;
this.Did = did;
this.Duties = duties;
this.Salary = salary;
this.Assess = check;
this.Phone=phone;
if (isLegal(name, sex, birth, address)) {
Database db = new Database();
sql = "insert into personnel(pid,pname,psex,pbirth,paddress,deptid,duties,salary,access,phone) ";
String temp = "values('" + pId + "','" + pName + "','" + pSex + "','" + pBirth + "','" + pAddress + "','"
+ Did + "','" + Duties + "','" + Salary + "','" + Assess + "','" + Phone + "')";
sql += temp;
try {
db.OpenConn(); // 打开连接
db.UpdateInfo(sql); // 插入数据
// 删除未使用编号表中的相关的编号
int t = Integer.valueOf(pId);
sql = "delete from unusedpersonnelid where pid='" + t + "'";
db.UpdateInfo(sql);
JOptionPane.showMessageDialog(null, "成功添加一条新的纪录");
} catch (Exception ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(null, "信息添加失败", "错误", JOptionPane.ERROR_MESSAGE);
} finally {
db.closeStmt();
db.closeConn();
}
}
}
/**
* 该函数用来实现信息的修改。
*
*/
public void modifyInfo(String id, String name, String sex, String birth, String address, String did, String duties,
String salary, String check, String phone) {
// 如果信息合法
this.pId = id;
this.pName = name;
this.pSex = sex;
this.pBirth = birth;
this.pAddress = address;
this.Did = did;
this.Duties = duties;
this.Salary = salary;
this.Assess = check;
this.Phone = phone;
if (isLegal(name, sex, birth, address)) {
Database db = new Database();
try {
int temp = Integer.valueOf(id);
sql = "update personnel set pname='" + name + "',psex='" + sex + "',pbirth='" + birth + "',paddress='"
+ address + "',Phone='" + phone + "' where pid = '" + temp + "'";
db.OpenConn(); // 打开连接
db.UpdateInfo(sql);
JOptionPane.showMessageDialog(null, "修改成功");
} catch (Exception ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(null, "信息修改失败", "错误", JOptionPane.ERROR_MESSAGE);
} finally {
db.closeStmt();
db.closeConn();
}
}
}
/*
* 该类用来实现员工调动
*
* */
public boolean updateDept(String id, String name, String newDeptId, String oldName, String newName, String oldjob,
String newjob) {
Database db = new Database();
int Did = Integer.valueOf(newDeptId);
int Pid = Integer.valueOf(id);
sql = "update personnel set deptid='" + Did + "',duties='" + newjob + "' where pid='" + Pid + "'";
try {
db.OpenConn();
db.UpdateInfo(sql); // 改变部门编号
JOptionPane.showMessageDialog(null, "人员调动成功");
sql = "select * from jobChange where pid='" + Pid + "'";
rs = db.QueryInfo(sql);
if (rs.last()) {
// time = rs.getRow();
}
// 获取当前时间
Date nowTime = new Date();
// 设置时间格式
SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 日期
String date = matter.format(nowTime);
sql = "insert into jobChange(pid,pname,olddept,newdept,oldjob,newjob,moddate) values('" + id + "','" + name
+ "','" + oldName + "','" + newName + "','" + oldjob + "','" + newjob + "','" + date + "')";
db.UpdateInfo(sql);
db.closeStmt();
db.closeConn();
return true;
} catch (Exception ex) {
db.closeStmt();
db.closeConn();
ex.printStackTrace();
return false;
}
}
public boolean updateAssess(String id,String name,String newsalary,String newAssess) {
Database db = new Database();
int temp = Integer.valueOf(id); //将编号转换成整数
sql = "update personnel set salary='"+newsalary+"', access='"+newAssess+"' where pId='"+temp+"'";
try {
db.OpenConn();
db.UpdateInfo(sql);
JOptionPane.showMessageDialog(null, "考核信息修改成功");
db.closeStmt();
db.closeConn();
return true;
} catch(Exception ex) {
ex.printStackTrace();
db.closeStmt();
db.closeConn();
return false;
}
}
/*
* 该方法用来实现员工请假情况更新
* */
public boolean updateleave(String leaid,String newresult) {
Database db = new Database();
int temp = Integer.valueOf(leaid);
sql = "update leavetable set result='" + newresult + "' where leaid='" + temp + "'";
try {
db.OpenConn();
db.UpdateInfo(sql);
JOptionPane.showMessageDialog(null, "成功更新");
db.closeStmt();
db.closeConn();
return true;
} catch (Exception ex) {
ex.printStackTrace();
db.closeStmt();
db.closeConn();
return false;
}
}
/**
* 该函数用来实现返回某个员工的所有信息
*
* @param id
* @return
*/
public String[] searchInfo(String id) {
Database db = new Database();
String[] s = new String[15];
int temp = Integer.valueOf(id);
sql = "select * from personnel where pid = '" + temp + "'";
try {
db.OpenConn();
rs = db.QueryInfo(sql);
if (rs.next()) {
s[0] = rs.getString("pid");
s[1] = rs.getString("pname");
s[2] = rs.getString("psex");
s[3] = rs.getString("pbirth");
s[4] = rs.getString("paddress");
s[5] = rs.getString("deptid");
s[6] = rs.getString("duties");
s[7] = rs.getString("salary");
s[8] = rs.getString("access");
s[9] = rs.getString("phone");
} else
s = null;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
db.closeStmt();
db.closeConn();
}
return s;
}
/**
* 该方法用来实现查询该字段的所有值。
*
* @param field 字段名
* @return
*/
public String[] selectField(String field) {
String[] s = null;
Database db = new Database();
int row = 0;
try {
sql = "select " + field + " from personnel order by pid"; // 查找该字段的所有值
db.OpenConn();
rs = db.QueryInfo(sql);
if (rs.last()) {
row = rs.getRow();
}
if (row == 0) {
s = new String[1];
- 1
- 2
- 3
前往页