老师布置了第二个JAVA作业,让写人事管理系统。
代码思路参考自:清华大学出版设出版的《Java课程设计案例精编(第3版)》
作业是老师在十月一日哦放假前让写的,所以十一回家写了俩界面,然后回到学校老师说先
不做这个,先写一个订餐系统,后来为了参加十一月的区域赛就搁置了这个作业,参加完
区域赛白天还要练题没怎么写,都是晚上快睡觉的时候写了几个界面,断断续续把界面写完
了,这周五、周六一鼓作气把这个给写完了,正好周一上课老师检查。
项目下载地址:https://github.com/wenyaxinluoyang/HRManagement
项目运行环境:
jdk 1.8.0_141
mysql 5.5.27
关于项目几个问题:
1.是否可以把项目发到邮箱?
答:没法发,原因两点,这个代码两三年前写的,那么久远,现在我都毕业工作了,电脑也换了,我没这个代码。
2.关于github的,没有账号的。
答:个知识个代码托管的地方,我记忆中注册好像不需要,直接去注册就好了,然后去下载代码,
不知道怎么下载,百度一下就好,这些操作都特别简单。
3.关于问题,你的项目在我的环境能够成功运行?
答:纠正一点这个项目下载后一定要配备我说的环境,而不是在你的环境运行。
举个例子:工作的时候,技术总监已经说了我们开发项目要用哪个版本的jdk,框架spring用哪个版本,
我们工作人员也按照要求进行开发,然后可以成功运行我们的代码。当我们把项目部署到服务器的时候,
我们服务器的环境配置的和开发环境是会保持一致的,然后我们的代码才能有最大的可能不发生问题跑起来,
我们绝不会任由喜爱,在服务器上随便安装与开发时候不相同的版本的东西,然后对开发人员说,你的代码
要在这个环境中跑起来。时间推移,版本更新,里面的某些内容发生了改变,万一了以前版本的代码不兼容,
代码将永远跑不起来。
我上学的时候jdk发展到1.8,1.9我们大部分都用1.8
现在,写文章时间为2019.06.23,jdk现在版本发展到1.12, 1.11,可以看到和项目开发时的版本相差
很大了。同样项目中使用的mysql等工具,经过几年的发展,版本更新很快,版本跨度这么大,已经出现
了兼容问题。好多使用我代码的人,配置了较高版本的jdk,结果界面无法显示,都是jdk太高导致的。
4.关于数据库链接问题?
改数据库连接对应的用户名和密码,要到DataOperator中修改。
项目使用mysql作为数据库。如果你更换成sqlserver或oracle,那么数据库驱动也要修改为sqlserver或
oracle的驱动,如果mysql用了与项目不同的版本,请记得修改驱动的对应版本。
5.运行代码时无法显示问题
这个是根据很多人遇到反应的,问题所在jdk版本过高,出现对旧版本代码的兼容问题。
解决方案,换成1.8.0_141或比他不要高太多的版本。
6.关于数据库(指用mysql)改了用户名和密码还报错的。
这是最近一个同学新遇到的问题,我之前没见过。改了用户名和密码后,还提示无法连接数据库,
数据库驱动版本也对,是因为高版本的驱动对应数据库连接的写法与低版本的不同,直接百度
抛出的异常,然后根据网上经验修改连接。
7.关于无法修改员工部门的问题
我代码的bug,在PersonBean中修改员工信息的update语句中我忘记写部门了,大家可以自行加上。
8.建议
建议从下载到环境配置到项目导入自己完成,包括驱动,一切前期工作自己完成。然后认真看
一下代码,代码逻辑非常简单,核心代码就是数据库操作,jdbc编程都是固定格式的,看一个
能明白其他的。然后出现什么问题了可以在留言询问了。本来我在评论区留了我的QQ号,
说起来真是后悔,有些同学什么都不管,每一步都希望我告诉他,包括下载和eclipse导入项目
之类的基本操做,让我觉得有点难以接受。甚至我在上班的时候,在外面朋友聚会的时候都在
QQ上回复大家遇到的问题,我有时会气得都锤桌子了,真的不能什么都依靠作者,自己一点
脑子都不想动。
人事管理系统功能简介:
人事管理系统主要用来实现各种人事管理功能,这些功能包括人员基本信息管理,部门
管理,人员调动管理,人员考核管理以及劳资管理这些模块。
系统功能图:
数据库设计:
员工信息表:Person
未使用员工编号表:unUsedPersonId
部门信息表:DeptTable
未使用部门编号表:unUsedDeptId
人员调动历史表:jobChange
人员考核历史表:assessChange
劳资历史记录表:salaryChange
系统主界面图:(HrMain.java)
可以发现主界面是两个分离面板,将左侧的树形结构和右侧的面板隔开。左侧的树形结构清晰地
表示出了该系统地功能。设计该界面左侧是一个树,人事管理系统是树根root,然后有四个一级节点,
接下来是若干个二级节点(叶子节点)。该界面主要响应的事件有,当点击根节点的时候,显示图中
右侧界面,当点击一级节点的时候,如果该节点没有展开,则先展开该节点。当点击叶子节点的时候
将相应的面板显示在右侧区域中。
1.基本信息管理模块:实现对人员信息的增删查该操作,以及对部门的增删查改操作。
1.1 添加人员信息界面(NodeOnePanelOne.java)
该模块主要实现添加员工信息功能,当点击获取新编号按钮的时候,清空所有文本框,并为欲添加的人员
自动分配一个编号。点击添加按钮的时候,首先判断数据库中不能为空的字段在文本框中是否为空,如果
为有是空的值则拒绝插入,否则插入到Person,并弹出提示信息信息插入成功。
1.2 修改人员信息界面(NodeOnePanelTwo.java)
该模块用来实现信息修改功能,首先通过在Person中查找,将人员的编号和姓名等信息绑定到选择人员信息
下拉列表,然后可以选择人员,选中某个人员后从Person中将该人员的基本信息查找出来,并在文本框显示。
对于性别、出生年月、民族这三个信息由于在通常情况下不常变动,则设置文本框不可修改,其他信息可以
进行修改,点击修改按钮的时候,同样要进行信息的检测,看信息输入是否正确,如果正确则更新表中对应
的记录,否则拒绝更新,点击清空按钮的时候,清空所有文本框内的内容。
1.3 删除人员信息界面(NodeOnePanelThree.java)
该模块用来实现删除员工记录的功能,首先在右侧面板初始化的时候,我们先从Person中查找需要的信息
并显示在右侧,当点中界面中表的一条记录时,将该员工的信息显示在下部文本框中,当点击删除按钮
的时候,删除该记录,并刷新界面。
1.4 查询人员信息界面(NodeOnePanelFour.java)
该界面和上个界面相似,就是将所有员工的信息显示在界面上。
1.5 部门管理界面(NodeOnePanelFive.java)
该界面可以实现部门的增删查改功能。初始化面板的时候,将DeptTable总的所有信息绑定到该界面上,
当点击获取新编号的时候,清空文本框,给欲添加的部门分配一个编号,当点击添加按钮的时候,需要
检测信息输入是否合法。当选中表中的一条记录,将该部门的信息显示在下部文本框中,然后可以对一级
部门和二级部门进行修改,当点击修改按钮的时候,检测信息是否合法,合法则修改并提示用户修改成功,
否则拒绝修改,如果修改成功,则该界面也需要跟着刷新。当点击删除按钮的时候,由于数据库中表的级
联关系, 如果Person表中还有对应部门的员工,则不允许删除,否则可以删除。当点击清空按钮的时候,
清空所有文本框内的内容。
2.人员调动模块:实现对人员工作的调动以及对调动历史的查询。
2.1人员调动:(NodeTwoPanelOne.java)
该模块实现人员部门的调动,从Person表中查询图中字段相应的信息并显示,并从DeptTable表中查询所有
部门并将这些部门绑定到新部门下拉列表框中,选择调入新部门后,更新Person表中的信息,并且将该调动
记录添加到jobChange表中。
2.2调动历史查询(NodeTwoPanelTwo.java)
该界面从jobChange表中读出所有信息并在界面上显示。
3.人员考核管理模块:实现对员工工作的考核,并显示考核记录。
3.1人员考核(NodeThreePanelOne.java)
同样从Person表中取出图中相应的信息并在面板上显示,当选择一条记录的时候,将其信息在下方文本框
显示,点击确定按钮的时候,将Person表中的相关信息更新,并将该考核记录插入assessChange表中。
3.2考核历史查询(NodeThreePanelTwo.java)
该界面将assessChange表的内容进行显示。
4.劳资管理:用来实现员工工资的调整和劳资历史的查询。
4.1劳资分配管理(NodeFourPanelOne.java)
该模块用来实现员工工资的调整,同样从Person中取出相应的信息并生成表格显示在界面上,当选中一条记录
时,将该记录的信息在下方文本框中显示,点击确定按钮后,更新Person中的信息,弹出修改提示,并将该劳资
历史插入到salaryChange表中。
4.2劳资历史查询(NodeFourPanelTwo.java)
从salaryChange表中取出所有信息在界面上显示。
项目代码:
主界面(HrMain.java)
package HRPackage;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import DataOperator.Database;
import JobTransfer.*;
import LaborManagement.*;
import PersonAccess.*;
/**
* 人事管理系统的主界面
* @author YaXin Wen
* @date 2017/10/2
*/
public class HrMain extends JFrame implements ActionListener, TreeSelectionListener {
//框架的大小
Dimension facesize = new Dimension(1200,900);
//程序左上角图标
ImageIcon icon;
//建立界面左侧JTree菜单
JTree tree;
DefaultMutableTreeNode root; //根节点,人事管理系统
DefaultMutableTreeNode node1; //一级节点,基本信息管理
DefaultMutableTreeNode node2; //一级节点,人员调动管理
DefaultMutableTreeNode node3; //一级节点,人员考核管理
DefaultMutableTreeNode node4; //一级节点,劳资管理
DefaultMutableTreeNode leafnode; //叶子节点
TreePath treePath;
//建立右侧操作区域,整个界面由两个分离面板构成
public static JSplitPane splitPane;
JPanel panel1;
JPanel panel2;
JPanel panel3;
JPanel panel4;
JLabel welcome = new JLabel(); //用来显示欢迎信息
JScrollPane scrollPane; //滚动面板
/**
* 构造函数,完成对象的初始化工作
*/
public HrMain() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
//添加框架的关闭事件处理
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setSize(facesize); //设置窗体大小
this.setLocation(400, 50); //设置窗体初始位置
this.setVisible(true); //设置窗体可见
this.setTitle("人事管理系统"); //设置窗体标题
this.setResizable(false); //设置窗体大小不可变
//设置窗体左上角图标
icon = new ImageIcon("image\\computer.png");
this.setIconImage(icon.getImage());
try {
Init(); //控件布局。
}catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 初始化函数,完成界面的初始化工作
* @param 无参数
*/
private void Init() throws Exception {
//将各个子节点,添加到root上去。
root = new DefaultMutableTreeNode("人事管理系统");
//实例化root的四个子节点。
node1 = new DefaultMutableTreeNode("基本信息管理");
node2 = new DefaultMutableTreeNode("人员调动管理");
node3 = new DefaultMutableTreeNode("人员考核管理");
node4 = new DefaultMutableTreeNode("劳资管理");
//基本信息管理上添加5个子节点
leafnode = new DefaultMutableTreeNode("添加人员信息");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("修改人员信息");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("删除人员信息");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("查询人员信息");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("部门管理");
node1.add(leafnode);
//基本信息管理节点添加到根节点上。
root.add(node1);
//人员调动管理上添加两个子节点。
leafnode = new DefaultMutableTreeNode("人员调动");
node2.add(leafnode);
leafnode = new DefaultMutableTreeNode("调动历史查询");
node2.add(leafnode);
//将人员调动管理节点添加到根节点上
root.add(node2);
//人员考核管理模块子功能
leafnode = new DefaultMutableTreeNode("人员考核");
node3.add(leafnode);
leafnode = new DefaultMutableTreeNode("考核历史查询");
node3.add(leafnode);
//人员考核管理节点添加到根节点上。
root.add(node3);
//劳资管理模块子功能
leafnode = new DefaultMutableTreeNode("劳资分配管理");
node4.add(leafnode);
leafnode = new DefaultMutableTreeNode("劳资历史查询");
node4.add(leafnode);
//劳资管理节点添加到根节点上。
root.add(node4);
//生成左侧的JTree,并且该树以root为根。
tree = new JTree(root);
//将树添加到滚动面板上
scrollPane = new JScrollPane(tree);
//设置滚动面板的大小。
scrollPane.setPreferredSize(new Dimension(200,850));
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
//生成JPanel,panel1盛装左侧控件,
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel1.add(scrollPane); //在此,左边界面设计完毕。
//开始设计右部界面。
welcome.setText("欢迎使用人事管理系统"); //显示欢迎信息
panel3.add(welcome);
splitPane = new JSplitPane();
splitPane.setOneTouchExpandable(false);
splitPane.setContinuousLayout(true);
splitPane.setPreferredSize(new Dimension(1100,900));
//间隔方式是水平摆放间隔
splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
//功能树位于界面左侧
splitPane.setLeftComponent(panel1);
//相应功能对应的面板位于分离面板的右侧。
splitPane.setRightComponent(panel3);
splitPane.setDividerSize(2);
splitPane.setDividerLocation(200);
this.setContentPane(splitPane);
this.setVisible(true);
//为左侧的tree添加事件监听器。
tree.addTreeSelectionListener(this);
}
@Override
/**
* 点击不同的节点,右侧显示不同的面板
*/
public void valueChanged(TreeSelectionEvent e) {
// TODO Auto-generated method stub
DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)e.getPath().getLastPathComponent();
//System.out.println("nodeText = " + currentNode);
String nodeText = currentNode.toString();
//如果当前选择的节点是root,人事管理系统,右侧面板是初始模样
if(nodeText == "人事管理系统") {
splitPane.setRightComponent(panel3);
}
else if(nodeText == "基本信息管理") {
/* 当点击基本信息管理节点的时候,如果此节点处于
* 关闭状态,则将其自动展开,用此代码实现节点自动展开。
*/
treePath = new TreePath(node1.getPath());
if(tree.isExpanded(treePath)) {
tree.collapsePath(treePath);
}
else {
tree.expandPath(treePath);
}
}
else if (nodeText == "添加人员信息") {
NodeOnePanelOne nodepanel11 = new NodeOnePanelOne();
splitPane.setRightComponent(nodepanel11);
}
else if(nodeText == "修改人员信息") {
NodeOnePanelTwo nodepanel12 = new NodeOnePanelTwo();
splitPane.setRightComponent(nodepanel12);
}
else if(nodeText == "删除人员信息") {
NodeOnePanelThree nodepanel13 = new NodeOnePanelThree();
splitPane.setRightComponent(nodepanel13);
}
else if(nodeText == "查询人员信息") {
NodeOnePanelFour nodepanel14 = new NodeOnePanelFour();
splitPane.setRightComponent(nodepanel14);
}
else if(nodeText == "部门管理") {
NodeOnePanelFive nodepanel15 = new NodeOnePanelFive();
splitPane.setRightComponent(nodepanel15);
}
else if(nodeText == "人员调动") {
NodeTwoPanelOne nodePanel21 = new NodeTwoPanelOne();
splitPane.setRightComponent(nodePanel21);
}
else if(nodeText == "调动历史查询") {
NodeTwoPanelTwo nodePanel22 = new NodeTwoPanelTwo();
splitPane.setRightComponent(nodePanel22);
}
else if(nodeText == "人员考核") {
NodeThreePanelOne nodePanel31 = new NodeThreePanelOne();
splitPane.setRightComponent(nodePanel31);
}
else if(nodeText == "考核历史查询") {
NodeThreePanelTwo nodePanel32 = new NodeThreePanelTwo();
splitPane.setRightComponent(nodePanel32);
}
else if(nodeText == "劳资分配管理") {
NodeFourPanelOne nodePanel41 = new NodeFourPanelOne();
splitPane.setRightComponent(nodePanel41);
}
else if(nodeText == "劳资历史查询") {
NodeFourPanelTwo nodePanel42 = new NodeFourPanelTwo();
splitPane.setRightComponent(nodePanel42);
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String[] args) throws Exception{
HrMain hr = new HrMain();
/**Database db = new Database();
db.OpenConn();
//String sql = "insert into DeptTable(DeptId,FatherDept,SonDept) values('3','办公室','机要科')";
String sql = "select * from DeptTable";
int number = db.RecordNumber(sql);
System.out.println(number);
db.closeStmt();
db.closeConn();
*/
}
}
1.基本信息管理模块:
1.1添加人员信息(NodeOnePanelOne.java)
package HRPackage;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import DataOperator.DeptBean;
import DataOperator.PersonBean;
/**
* 每个节点对应一个面板,这些面板按照父节点、子节点进行编号。
* 1号节点下的1号子节点,其面板命名为NodeOnePanelOne.
* @author Yaxin Wen
* @date 2017/10/2
*
*/
public class NodeOnePanelOne extends JPanel implements ActionListener, ItemListener {
//界面分为两部分,上部分用来显示该模块的作用
JPanel upPanel = new JPanel();
//中部用来让用户执行相应的操作,来满足自己的需求。
JPanel centerPanel = new JPanel();
//定义界面标签
JLabel title = new JLabel("增加人员信息"); //说明文字,表明该模块的功能
JLabel pIdLabel = new JLabel(); //员工编号
JLabel pNameLabel = new JLabel(); //员工姓名
JLabel pSexLabel = new JLabel(); //员工性别
JLabel pBirthDateLabel = new JLabel(); //出生年月
JLabel pNationLabel = new JLabel(); //民族
JLabel pAddressLabel = new JLabel(); //地址
JLabel pDepartmentLabel = new JLabel(); //部门
JLabel pOtherLabel = new JLabel(); //其他
//定义界面需要的文本框
JTextField pIdText = new JTextField(15); //用来显示或填写员工编号
JTextField pNameText = new JTextField(30); //用来显示或填写员工姓名
JTextField pSexText = new JTextField(15); //用来显示或填写员工性别
JTextField pBirthDateText = new JTextField(30); //用来显示或填写出生年月
JTextField pNationText = new JTextField(15); //用来显示或填写民族
JTextField pAddressText = new JTextField(30); //用来显示或填写地址
JTextField pOtherText = new JTextField(30); //用来显示或填写其他信息
JComboBox pDepartmentCombo = null; //用来选择部门信息
//两个按钮,用来添加信息和清空信息。
//该按钮用来为新增加的员工分配编号
JButton getNewIdButton = new JButton("获取新编号");
//该按钮用来执行添加员工信息的操作
JButton addInfoButton = new JButton("添 加");
//该按钮用来完成清空文本框的操作
JButton clearInfoButton = new JButton("清 空");
JScrollPane jScrollPane1 = new JScrollPane();
String Did = "0"; //员工所属部门编号
String Salary = "0"; //员工工资
String Assess = "未考核"; //员工烤鹅
//使用网格布局。
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints gridBagCon;
/**
* 构造函数完成对象的初始化工作
*/
public NodeOnePanelOne() {
this.setLayout(new BorderLayout());
try {
jScrollPanelInit(); //中部面板
panelInit(); //上部面板
addListener(); //为控件添加监听器
}catch (Exception ex) {
ex.printStackTrace();
}
}
public void jScrollPanelInit() throws Exception {
//设置面板的布局为网格布局。
centerPanel.setLayout(gridBag);
//将“人员编号”这个标签添加到面板的指定位置
gridBagCon = new GridBagConstraints();
pIdLabel.setText("人 员 编 号 :");
pIdLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 0;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,10,10,1);
gridBag.setConstraints(pIdLabel,gridBagCon); //把标题放到相应的位置
centerPanel.add(pIdLabel);
//添加用来填写人员编号的文本框到面板上
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 1;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,1,10,15);
gridBag.setConstraints(pIdText, gridBagCon);
centerPanel.add(pIdText);
//添加人员姓名标签
gridBagCon = new GridBagConstraints();
pNameLabel.setText("人 员 姓 名 :");
pNameLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 2;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,15,10,1);
gridBag.setConstraints(pNameLabel, gridBagCon);
centerPanel.add(pNameLabel);
//添加显示人员姓名的文本框
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 3;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,1,10,10);
gridBag.setConstraints(pNameText, gridBagCon);
centerPanel.add(pNameText);
//添加性别标签
gridBagCon = new GridBagConstraints();
pSexLabel.setText("性 别 :");
pSexLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 0;
gridBagCon.gridy = 2;
gridBagCon.insets = new Insets(15,10,10,1);
gridBag.setConstraints(pSexLabel, gridBagCon);
centerPanel.add(pSexLabel);
//添加填写性别文本框
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 1;
gridBagCon.gridy = 2;
gridBagCon.insets = new Insets(15,1,10,15);
gridBag.setConstraints(pSexText, gridBagCon);
centerPanel.add(pSexText);
//添加出生年月文本标签
gridBagCon = new GridBagConstraints();
pBirthDateLabel.setText("出 生 年 月 :");
pBirthDateLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 2;
gridBagCon.gridy = 2;
gridBagCon.insets = new Insets(15,15,10,1);
gridBag.setConstraints(pBirthDateLabel, gridBagCon);
centerPanel.add(pBirthDateLabel);
//添加出生年月文本框
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 3;
gridBagCon.gridy = 2;
gridBagCon.insets = new Insets(15,1,10,10);
gridBag.setConstraints(pBirthDateText, gridBagCon);
centerPanel.add(pBirthDateText);
//添加民族标签
gridBagCon = new GridBagConstraints();
pNationLabel.setText("民 族 :");
pNationLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 0;
gridBagCon.gridy = 3;
gridBagCon.insets = new Insets(15,10,10,1);
gridBag.setConstraints(pNationLabel, gridBagCon);
centerPanel.add(pNationLabel);
//添加填写民族的文本框
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 1;
gridBagCon.gridy = 3;
gridBagCon.insets = new Insets(15,1,10,15);
gridBag.setConstraints(pNationText, gridBagCon);
centerPanel.add(pNationText);
//添加地址标签
gridBagCon = new GridBagConstraints();
pAddressLabel.setText("地 址 :");
pAddressLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 2;
gridBagCon.gridy = 3;
gridBagCon.insets = new Insets(15,15,10,1);
gridBag.setConstraints(pAddressLabel, gridBagCon);
centerPanel.add(pAddressLabel);
//添加地址文本框
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 3;
gridBagCon.gridy = 3;
gridBagCon.insets = new Insets(15,1,10,10);
gridBag.setConstraints(pAddressText, gridBagCon);
centerPanel.add(pAddressText);
//添加部门标签
gridBagCon = new GridBagConstraints();
pDepartmentLabel.setText("部 门 :");
pDepartmentLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 0;
gridBagCon.gridy = 4;
gridBagCon.insets = new Insets(15,10,10,1);
gridBag.setConstraints(pDepartmentLabel, gridBagCon);
centerPanel.add(pDepartmentLabel);
//添加部门列表
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 1;
gridBagCon.gridy = 4;
gridBagCon.insets = new Insets(15,1,10,15);
DeptBean bean = new DeptBean();
try {
String[] values = bean.getAllNodeInfor();
pDepartmentCombo = new JComboBox(values);
} catch(Exception ex) {
ex.printStackTrace();
}
gridBag.setConstraints(pDepartmentCombo, gridBagCon);
centerPanel.add(pDepartmentCombo);
//其他标签
gridBagCon = new GridBagConstraints();
pOtherLabel.setText("其 他 :");
pOtherLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 2;
gridBagCon.gridy = 4;
gridBagCon.insets = new Insets(15,15,10,1);
gridBag.setConstraints(pOtherLabel, gridBagCon);
centerPanel.add(pOtherLabel);
//其他标签对应的文本框
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 3;
gridBagCon.gridy = 4;
gridBagCon.insets = new Insets(15,1,10,10);
gridBag.setConstraints(pOtherText, gridBagCon);
centerPanel.add(pOtherText);
gridBagCon = new GridBagConstraints();
getNewIdButton.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 0;
gridBagCon.gridy = 5;
gridBagCon.gridheight = 1;
gridBagCon.gridwidth = 2;
gridBagCon.insets = new Insets(15,0,10,0);
gridBag.setConstraints(getNewIdButton, gridBagCon);
centerPanel.add(getNewIdButton);
//添加添加按钮
gridBagCon = new GridBagConstraints();
addInfoButton.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 2;
gridBagCon.gridy = 5;
gridBagCon.gridwidth = 1;
gridBagCon.gridheight = 1;
gridBagCon.insets = new Insets(15,0,10,0);
gridBag.setConstraints(addInfoButton, gridBagCon);
centerPanel.add(addInfoButton);
addInfoButton.setEnabled(false);
//添加清空按钮
gridBagCon = new GridBagConstraints();
clearInfoButton.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 3;
gridBagCon.gridy = 5;
gridBagCon.gridwidth = 1;
gridBagCon.gridheight = 1;
gridBagCon.insets = new Insets(15,0,10,0);
gridBag.setConstraints(clearInfoButton, gridBagCon);
centerPanel.add(clearInfoButton);
}
/**
* 面板进行初始化
* @throws Exception
*/
public void panelInit() throws Exception {
//设置面板的布局方式为网格布局
upPanel.setLayout(gridBag);
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 0;
gridBagCon.insets = new Insets(0,10,0,10);
title.setFont(new Font("Dialog",0,25));
gridBag.setConstraints(title, gridBagCon);
upPanel.add(title);
jScrollPane1 = new JScrollPane(centerPanel);
jScrollPane1.setPreferredSize(new Dimension(900,750));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,0,0,0);
gridBag.setConstraints(jScrollPane1, gridBagCon);
upPanel.add(jScrollPane1);
this.add(upPanel,BorderLayout.NORTH);
pIdText.setEditable(false);
pNameText.setEditable(true);
pSexText.setEditable(true);
pBirthDateText.setEditable(true);
pNationText.setEditable(true);
pAddressText.setEditable(true);
pOtherText.setEditable(true);
addInfoButton.setEnabled(false);
}
/**
* 为各个按钮添加事件监听器
* @throws Exception
*/
public void addListener() throws Exception {
addInfoButton.addActionListener(this);
clearInfoButton.addActionListener(this);
pDepartmentCombo.addItemListener(this);
getNewIdButton.addActionListener(this);
}
@Override
public void itemStateChanged(ItemEvent event) {
// TODO Auto-generated method stub
if(event.getStateChange() == ItemEvent.SELECTED) {
String temp = "" + event.getItem();
int i = temp.indexOf("-");
Did = temp.substring(0,i);
}
}
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
Object obj = event.getSource();
//事件源是添加信息按钮
if(obj == getNewIdButton) {
setNull();
PersonBean bean = new PersonBean();
try {
int number = bean.getNewId();
pIdText.setText(String.valueOf(number));
addInfoButton.setEnabled(true);
} catch(Exception ex) {
ex.printStackTrace();
}
}
else if(obj == addInfoButton) {
//添加人员信息,将所有参数传进去
PersonBean bean = new PersonBean();
try{
bean.addInfo(pIdText.getText(), pNameText.getText(), pSexText.getText(),pBirthDateText.getText(), pNationText.getText(), pAddressText.getText(),Did,Salary, Assess, pOtherText.getText());
} catch(Exception ex) {
ex.printStackTrace();
}
}
else if(obj == clearInfoButton) {
setNull();
}
}
/**
* 点击清空按钮的时候,清空所有文本框中的值
*/
void setNull() {
pIdText.setText(null);
pNameText.setText(null);
pSexText.setText(null);
pBirthDateText.setText(null);
pNationText.setText(null);
pAddressText.setText(null);
pOtherText.setText(null);
}
}
1.2修改人员信息(NodeOnePanelTwo.java)
package HRPackage;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import DataOperator.PersonBean;
/**
* 实现修改人员信息的界面
* @author Yaxin Wen
* @date 2017/11/17
*
*/
public class NodeOnePanelTwo extends JPanel implements ActionListener, ItemListener {
//定义界面所需要用的面板
JPanel centerPanel = new JPanel();
JPanel upPanel = new JPanel();
//定义界面所需要的标签(静态文本)
JLabel title = new JLabel("修改人员信息");
JLabel pIdLabel = new JLabel(); //员工编号
JLabel pNameLabel = new JLabel(); //员工姓名
JLabel pSexLabel = new JLabel(); //员工性别
JLabel pBirthDateLabel = new JLabel(); //出生年月
JLabel pNationLabel = new JLabel(); //民族
JLabel pAddressLabel = new JLabel(); //地址
JLabel pDepartmentLabel = new JLabel(); //部门
JLabel pOtherLabel = new JLabel(); //其他
JLabel pInfoChoseLabel = new JLabel(); //选择人员信息。
//定义界面所需的文本框
JTextField pIdText = new JTextField(15); //编号
JTextField pNameText = new JTextField(30); //姓名
JTextField pSexText = new JTextField(15); //性别
JTextField pBirthDateText = new JTextField(30); //出生年月
JTextField pNationText = new JTextField(15); //民族
JTextField pAddressText = new JTextField(30); //地址
JTextField pOtherText = new JTextField(50); //其他
JComboBox pInfoChoseCombo = null; //人员信息列表。
JScrollPane jScrollPane;
//这三个变量并没有什么实际意义。
String Did = "0"; //人员所属部门
String Salary = "0"; //人员工资
String Assess = "未考核"; //人员考核信息
//定义两个按钮,一个是修改按钮,一个是清空按钮。
JButton modifyInfoButton = new JButton("修 改");
JButton clearInfoButton = new JButton("清 空");
//定义网格布局
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints gridBagCon;
/**
* 构造函数完成初始化工作
*/
public NodeOnePanelTwo() {
this.setLayout(new BorderLayout());
try{
jScrollPanelInit(); //上部面板布局
panelInit(); //中部面板布局
addListener(); //为相应控件添加事件监听器
} catch (Exception ex) {
ex.printStackTrace();
}
}
//jScrollPanel的布局。
public void jScrollPanelInit() throws Exception {
centerPanel.setLayout(gridBag);
//添加人员编号标签
gridBagCon = new GridBagConstraints();
pIdLabel.setText("人 员 编 号 :");
pIdLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 0;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,10,10,1);
gridBag.setConstraints(pIdLabel, gridBagCon);
centerPanel.add(pIdLabel);
//添加显示人员标号所需要的文本框
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 1;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,1,10,15);
gridBag.setConstraints(pIdText, gridBagCon);
centerPanel.add(pIdText);
//添加人员姓名标签
gridBagCon = new GridBagConstraints();
pNameLabel.setText("人 员 姓 名 :");
pNameLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 2;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,15,10,1);
gridBag.setConstraints(pNameLabel, gridBagCon);
centerPanel.add(pNameLabel);
//添加显示人员姓名的文本框
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 3;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,1,10,10);
gridBag.setConstraints(pNameText, gridBagCon);
centerPanel.add(pNameText);
//添加人员性别标签
gridBagCon = new GridBagConstraints();
pSexLabel.setText("性 别 :");
pSexLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 0;
gridBagCon.gridy = 2;
gridBagCon.insets = new Insets(15,10,10,1);
gridBag.setConstraints(pSexLabel, gridBagCon);
centerPanel.add(pSexLabel);
//添加显示人员性别的文本框
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 1;
gridBagCon.gridy = 2;
gridBagCon.insets = new Insets(15,1,10,15);
gridBag.setConstraints(pSexText, gridBagCon);
centerPanel.add(pSexText);
//添加出生年月的标签
gridBagCon = new GridBagConstraints();
pBirthDateLabel.setText("出 生 年 月 :");
pBirthDateLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 2;
gridBagCon.gridy = 2;
gridBagCon.insets = new Insets(15,15,10,1);
gridBag.setConstraints(pBirthDateLabel, gridBagCon);
centerPanel.add(pBirthDateLabel);
//添加出生年月所需的文本框
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 3;
gridBagCon.gridy = 2;
gridBagCon.insets = new Insets(15,1,10,10);
gridBag.setConstraints(pBirthDateText, gridBagCon);
centerPanel.add(pBirthDateText);
//添加民族标签
gridBagCon = new GridBagConstraints();
pNationLabel.setText("民 族 :");
pNationLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 0;
gridBagCon.gridy = 3;
gridBagCon.insets = new Insets(15,10,10,1);
gridBag.setConstraints(pNationLabel, gridBagCon);
centerPanel.add(pNationLabel);
//添加民族文本框
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 1;
gridBagCon.gridy = 3;
gridBagCon.insets = new Insets(15,1,10,15);
gridBag.setConstraints(pNationText, gridBagCon);
centerPanel.add(pNationText);
//添加地址标签
gridBagCon = new GridBagConstraints();
pAddressLabel.setText("地 址 :");
pAddressLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 2;
gridBagCon.gridy = 3;
gridBagCon.insets = new Insets(15,15,10,1);
gridBag.setConstraints(pAddressLabel, gridBagCon);
centerPanel.add(pAddressLabel);
//添加地址文本框
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 3;
gridBagCon.gridy = 3;
gridBagCon.insets = new Insets(15,1,10,10);
gridBag.setConstraints(pAddressText, gridBagCon);
centerPanel.add(pAddressText);
//添加其他标签
gridBagCon = new GridBagConstraints();
pOtherLabel.setText("其 他 :");
pOtherLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 0;
gridBagCon.gridy = 4;
gridBagCon.insets = new Insets(15,10,10,1);
gridBag.setConstraints(pOtherLabel, gridBagCon);
centerPanel.add(pOtherLabel);
//添加其他文本框
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 1;
gridBagCon.gridy = 4;
gridBagCon.gridwidth = 3;
gridBagCon.gridheight = 1;
gridBagCon.insets = new Insets(15,1,10,115);
gridBag.setConstraints(pOtherText, gridBagCon);
centerPanel.add(pOtherText);
//选择人员信息标签
gridBagCon = new GridBagConstraints();
pInfoChoseLabel.setText("选择人员信息");
pInfoChoseLabel.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 0;
gridBagCon.gridy = 5;
gridBagCon.insets = new Insets(15,10,10,1);
gridBag.setConstraints(pInfoChoseLabel, gridBagCon);
centerPanel.add(pInfoChoseLabel);
//选择人员需要用的下拉列表框
PersonBean bean = new PersonBean();
String[] values=null;
try {
String[] id = bean.selectField("pId");
String[] name = bean.selectField("pName");
int len = id.length;
values = new String[len];
for(int i = 0; i < len; i++) {
values[i] = "" + id[i] + "-" + name[i];
}
} catch(Exception ex) {
ex.printStackTrace();
}
pInfoChoseCombo = new JComboBox(values);
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 1;
gridBagCon.gridy = 5;
gridBagCon.gridwidth = 1;
gridBagCon.gridheight = 1;
gridBagCon.insets = new Insets(15,10,10,10);
gridBag.setConstraints(pInfoChoseCombo, gridBagCon);
centerPanel.add(pInfoChoseCombo);
//添加修改按钮到面板上
gridBagCon = new GridBagConstraints();
modifyInfoButton.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 2;
gridBagCon.gridy = 5;
gridBagCon.insets = new Insets(10,10,10,10);
gridBag.setConstraints(modifyInfoButton, gridBagCon);
centerPanel.add(modifyInfoButton);
//添加清空按钮到面板上
gridBagCon = new GridBagConstraints();
clearInfoButton.setFont(new Font("Dialog",0,15));
gridBagCon.gridx = 3;
gridBagCon.gridy = 5;
gridBagCon.insets = new Insets(10,10,10,10);
gridBag.setConstraints(clearInfoButton, gridBagCon);
centerPanel.add(clearInfoButton);
jScrollPane = new JScrollPane(centerPanel);
jScrollPane.setPreferredSize(new Dimension(900,750));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,0,0,0);
gridBag.setConstraints(jScrollPane,gridBagCon);
upPanel.add(jScrollPane);
}
public void panelInit() throws Exception {
this.setLayout(new BorderLayout());
upPanel.setLayout(gridBag);
gridBagCon = new GridBagConstraints();
title.setFont(new Font("Dialog",0,25));
gridBagCon.gridx = 0;
gridBagCon.gridy = 0;
gridBagCon.insets = new Insets(0,10,0,10);
gridBag.setConstraints(title, gridBagCon);
upPanel.add(title);
//所有文本框初始都设置为不可用,直到选中一个人,然后再将文本框设置为可用。
pIdText.setEditable(false);
pNameText.setEditable(false);
pSexText.setEditable(false);
pBirthDateText.setEditable(false);
pNationText.setEditable(false);
pAddressText.setEditable(false);
pOtherText.setEditable(false);
modifyInfoButton.setEnabled(false);
this.add(upPanel,BorderLayout.NORTH);
}
/**
* 为一些控件添加事件监听器
* @throws Exception
*/
public void addListener() throws Exception {
modifyInfoButton.addActionListener(this);
clearInfoButton.addActionListener(this);
pInfoChoseCombo.addItemListener(this);
}
/**
* 实现对下拉列表发出事件的响应。
*/
@Override
public void itemStateChanged(ItemEvent event) {
// TODO Auto-generated method stub
if(event.getStateChange() == ItemEvent.SELECTED) {
String temp = "" + event.getItem(); //获取被选中的信息
int i = temp.indexOf("-"); //找到'-'所在的位置,然后进行求字串的操作
String pId = "" + temp.substring(0,i); //查找该编号人员的所有信息
PersonBean bean = new PersonBean();
//查找该编号对应人员的所有信息,并且显示在文本框内
String[] info = bean.searchInfo(pId);
pIdText.setText(info[0]);
pNameText.setText(info[1]);
pSexText.setText(info[2]);
pBirthDateText.setText(info[3]);
pNationText.setText(info[4]);
pAddressText.setText(info[5]);
Did = info[6];
Salary = info[7];
Assess = info[8];
pOtherText.setText(info[9]);
//设置修改按钮可用
modifyInfoButton.setEnabled(true);
//一般一个人他的姓名、地址和其他信息可能改变,其他信息应该不会改变
pNameText.setEditable(true);
pAddressText.setEditable(true);
pOtherText.setEditable(true);
}
}
/**
* 点击不同的按钮,执行不同的操作。
*/
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
//修改按钮,执行更新操作
if(event.getSource() == modifyInfoButton) {
PersonBean bean = new PersonBean();
try {
bean.modifyInfo(pIdText.getText(), pNameText.getText(), pSexText.getText(), pBirthDateText.getText(),pNationText.getText(), pAddressText.getText(),Did, Salary, Assess, pOtherText.getText());
} catch(Exception ex) {
ex.printStackTrace();
}
}
else if(event.getSource() == clearInfoButton) {
//点击清空按钮,将所有信息清空
setNull();
}
}
void setNull() {
//把所有内容置空
pIdText.setText(null);
pNameText.setText(null);
pSexText.setText(null);
pBirthDateText.setText(null);
pNationText.setText(null);
pAddressText.setText(null);
pOtherText.setText(null);
//将所有按钮重新设置为不可用的状态
pIdText.setEditable(false);
pNameText.setEditable(false);
pSexText.setEditable(false);
pBirthDateText.setEditable(false);
pNationText.setEditable(false);
pAddressText.setEditable(false);
pOtherText.setEditable(false);
}
}
1.3删除人员信息(NodeOnePanelThree.java)
package HRPackage;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import DataOperator.PersonBean;
/**
* 该面板用来实现删除员工信息,将所有员工的信息呈现,
* 选中一条记录后可以执行删除操作。
* @author YaXin Wen
* @date 2017/10/22
*/
public class NodeOnePanelThree extends JPanel implements ActionListener, ListSelectionListener {
private static final Container BottomPanel = null;
//该模块整个界面分为三个部分
JPanel upPanel = new JPanel(); //上部面板用来盛装显示该模块作用的标签
JPanel centerPanel = new JPanel(); //中部面板用来展示员工的各项信息
JPanel bottomPanel = new JPanel(); //下部面板用来展示欲删除的员工的信息
JLabel title = new JLabel("人员信息删除");
JLabel pIdLabel = new JLabel(); //人员编号静态文本
JLabel pNameLabel = new JLabel(); //人员姓名静态文本
JLabel pDepartmentLabel = new JLabel(); //显示人员部门静态文本
JTextField pIdText = new JTextField(15); //显示欲删除人员的编号
JTextField pNameText = new JTextField(20); //显示欲删除人员的姓名
JTextField pDepartmentText = new JTextField(15); //显示欲删除人员所在的部门
JButton deleteButton = new JButton("删 除");
//定义一个可以滚动的面板
JScrollPane jScrollPanel;
//定义一个表格
JTable table;
ListSelectionModel listSelectionModel = null;
//设置表的字段信息
String[] colName = {"编 号","姓 名","出生年月","民 族","地 址","部 门",};
String[][] colValue;
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints gridBagCon;
/**
* 构造函数完成对象的初始化工作
*/
public NodeOnePanelThree() {
this.setLayout(new BorderLayout());
try{
upPanelInit();
centerPanelInit();
bottomPanelInit();
} catch(Exception ex) {
ex.printStackTrace();
}
}
/**
* 完成上部面板的初始化工作,该工作主要是让所有员工的信息
* 用表显示出来。
* @throws Exception
*/
public void upPanelInit() throws Exception {
//定义PersonBean用来实现对员工信息的各种操作
PersonBean bean = new PersonBean();
//设置上部面板的布局方式为网格布局
upPanel.setLayout(gridBag);
//设置该模块标题的字体大小
title.setFont(new Font("Dialog",0,25));
//将模块标题添加到上部面板
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 0;
gridBagCon.insets = new Insets(0,10,0,10);
gridBag.setConstraints(title, gridBagCon);
upPanel.add(title);
//colValue用来存放表的记录,通过调用searchAllForNode方法查找员工表中的所有信息并返回
colValue = bean.searchAllForNode();
//对表格实例化,并将查询到的信息绑定到表上。
table = new JTable(colValue,colName);
//设置表的大小
table.setPreferredScrollableViewportSize(new Dimension(900,750));
//设置表只能选中一行
listSelectionModel = table.getSelectionModel();
listSelectionModel.setSelectionMode(listSelectionModel.SINGLE_SELECTION);
//添加监听器,当选中表的某条记录时触发
listSelectionModel.addListSelectionListener(this);
//将表格放到滚动面板里面
jScrollPanel = new JScrollPane(table);
//设置滚动面板的大小。
jScrollPanel.setPreferredSize(new Dimension(900,750));
//将滚动面板添加到上部面板上
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,0,0,0);
gridBag.setConstraints(jScrollPanel,gridBagCon);
upPanel.add(jScrollPanel);
//将上部面板添加到当前面板的上部
this.add(upPanel,BorderLayout.NORTH);
}
/**
* 中部面板的布局
*/
public void centerPanelInit() throws Exception {
this.add(centerPanel);
}
/**
* 下部面板的布局,由几个标签和几个文本框和一个删除按钮
* 构成,标签用来显示数据的字段名,文本框用来呈现选中记
* 录对应的人员信息,
*/
public void bottomPanelInit() {
//设置标签的文本内容和字体大小。
pIdLabel.setText("编号:");
pIdLabel.setFont(new Font("Dialog",0,20));
//将标签和其相对应的文本框添加到面板上
bottomPanel.add(pIdLabel);
bottomPanel.add(pIdText);
//设置标签的文本内容和字体大小
pNameLabel.setText("姓名:");
pNameLabel.setFont(new Font("Dialog",0,20));
//将标签和其相对应的文本框添加到面板上
bottomPanel.add(pNameLabel);
bottomPanel.add(pNameText);
//设置标签的文本内容和字体大小
pDepartmentLabel.setText("部门:");
pDepartmentLabel.setFont(new Font("Dialog",0,20));
//将标签和其相对应的文本框添加到面板上
bottomPanel.add(pDepartmentLabel);
bottomPanel.add(pDepartmentText);
//设置各个文本框中的字体大小。
pIdText.setFont(new Font("Dialog",0,15));
pNameText.setFont(new Font("Dialog",0,15));
pDepartmentText.setFont(new Font("Dialog",0,15));
//所有文本框初始时刻都设置为不可编辑,这些文本框只用来呈现信息。
pIdText.setEditable(false);
pNameText.setEditable(false);
pDepartmentText.setEditable(false);
//设置按钮的字体大小,设置按钮初始不可用,并为按钮添加事件监听器
deleteButton.setFont(new Font("Dialog",0,15));
bottomPanel.add(deleteButton);
deleteButton.setEnabled(false);
deleteButton.addActionListener(this);
//将下部面板添加到主面板的下部
this.add(bottomPanel,BorderLayout.SOUTH);
}
/**
* 当选中的表格中的某条记录,将该记录对应人员的工号,姓名,
* 和所在部门显示在下部的文本框中,并且把删除按钮设置为可用
*/
@Override
public void valueChanged(ListSelectionEvent lse) {
// TODO Auto-generated method stub
int selectedRow = table.getSelectedRow();
pIdText.setText(colValue[selectedRow][0]);
pNameText.setText(colValue[selectedRow][1]);
pDepartmentText.setText(colValue[selectedRow][5]);
deleteButton.setEnabled(true);
}
/**
* 编写各个控件的事件响应方法。
*/
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
Object obj = event.getSource();
//当点击删除按钮的时候,删除对应的记录
if(obj == deleteButton) {
PersonBean bean = new PersonBean();
try {
//success 用来记录记录的删除是否成功
boolean success = bean.deleteInfo(pIdText.getText());
//删除成功,界面中表的内容需要被更新。
if(success == true) {
NodeOnePanelThree nodePanel13 = new NodeOnePanelThree();
HrMain.splitPane.setRightComponent(nodePanel13);
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
}
1.4查询人员信息(NodeOnePanelFour.java)
package HRPackage;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import DataOperator.PersonBean;
/**
* 该模块用来实现显示所有人员信息的功能
* @author Yaxin Wen
* @date 2017/11/18
*/
public class NodeOnePanelFour extends JPanel implements ActionListener {
//定义所需要的面板
JPanel upPanel = new JPanel();
//定义标签,用来显示该模块的功能
JLabel title = new JLabel("人员信息查询");
//定义表格
JScrollPane jScrollPane;
JTable table;
ListSelectionModel listSelectionModel = null;
//定义表格字段
String[] colName = {"编 号","姓 名","出生年月","民 族","地 址","部门"};
//定义存放表格记录所需的二维素组
String[][] colValue;
//定义网格布局
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints gridBagCon;
/**
* 构造函数完成初始化工作
*/
public NodeOnePanelFour() {
this.setLayout(new BorderLayout());
try{
upPanelInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 对上部面板进行初始化,在上面添加模块标题和表格
* @throws Exception
*/
public void upPanelInit() throws Exception {
PersonBean bean = new PersonBean();
//设置面板的布局方式为网格布局
upPanel.setLayout(gridBag);
//将标题添加到面板上
title.setFont(new Font("Dialog",0,25));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 0;
gridBagCon.insets = new Insets(0,10,0,10);
gridBag.setConstraints(title, gridBagCon);
upPanel.add(title);
try {
//获取员工表的所有信息并返回给colValue
colValue = bean.searchAllForNode();
//初始化表并将得到的数据绑定到表上
table = new JTable(colValue,colName);
//设置表格大小
table.setPreferredScrollableViewportSize(new Dimension(900,750));
//将表格添加到滚动面板上
jScrollPane = new JScrollPane(table);
//设置滚动面板的大小
jScrollPane.setPreferredSize(new Dimension(900,750));
//将滚动面板添加到面板上
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,0,0,0);
gridBag.setConstraints(jScrollPane, gridBagCon);
upPanel.add(jScrollPane);
this.add(upPanel,BorderLayout.NORTH);
} catch(Exception ex){
ex.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
1.5部门管理(NodeOnePanelFive.java)
package HRPackage;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import DataOperator.DeptBean;
/**
* 该模块用来实现部门管理的功能,包括部门添加,部门修改,
* 部门删除等操作。
* @author YaXin Wen
* @date 2017/11/17
*/
public class NodeOnePanelFive extends JPanel implements ActionListener, ListSelectionListener {
//定义所需要用的面板
JPanel upPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel bottomPanel = new JPanel();
//定义三个需要的文本标签
JLabel departIdLabel; //部门编号
JLabel firstLevelDepart; //一级部门
JLabel secondLevelDepart; //二级部门
//定义三个所需的文本框
JTextField departIdText = new JTextField(10); //用来显示或填写部门编号
JTextField firstLevelText = new JTextField(15); //用来显示或填写一级部门
JTextField secondLevelText = new JTextField(15); //用来显示或填写二级部门
//定义五个按钮
//获取新编号按钮,点击此按钮,为当前要添加的部门分配新的表闹
JButton getNewIdBtn = new JButton("获取新编号");
//添加按钮,点击该按钮可以实现增加部门的功能
JButton addInfoBtn = new JButton("增加");
//修改按钮,点击该按钮可以实现部门的修改功能
JButton modifyInfoBtn = new JButton("修改");
//删除按钮,点击该按钮实现删除该部门
JButton deleteInfoBtn = new JButton("删除");
//清空按钮,当点击清空按钮的时候,清空文本框内的值
JButton clearInfoBtn = new JButton("清空");
//定义表格
JScrollPane jScrollPane;
JTable table;
ListSelectionModel listSelectionModel = null;
//定义表格的字段
String[] colName = {"部门编号","一级部门","二级部门"};
//用于存放表格记录的二维数组
String[][] colValue;
//定义网格布局方式
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints gridBagCon = new GridBagConstraints();
/**
* 构造函数完成类的初始化工作。
*/
public NodeOnePanelFive() {
//设计当前面板采用BorderLayout布局
this.setLayout(new BorderLayout());
try {
upPanelInit(); //上部面板初始化
centerPanelInit(); //中部面板初始化
bottomPanelInit(); //下部面板的布局
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 上部面板初始化,上部面板主要用来盛放部门表
* @throws Exception
*/
public void upPanelInit() throws Exception {
//DeptBean可以完成对部门信息的各种操作
DeptBean bean = new DeptBean();
try {
//获取部门表中的信息并返回给colValue
colValue = bean.searchAll();
//将从表中获取到的值绑定到表格上
table = new JTable(colValue,colName);
//设置表格大小
table.setPreferredScrollableViewportSize(new Dimension(900,750));
//设置表格选择记录时只能选择一行
listSelectionModel = table.getSelectionModel();
listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//添加事件监听器
listSelectionModel.addListSelectionListener(this);
jScrollPane = new JScrollPane(table);
jScrollPane.setPreferredSize(new Dimension(900,750));
} catch(Exception ex) {
ex.printStackTrace();
}
upPanel.add(jScrollPane);
this.add(upPanel,BorderLayout.NORTH);
}
/**
* 中部面板的布局,将所需要的文本框和所需要的标签添加
* 到中部面板
* @throws Exception
*/
public void centerPanelInit() throws Exception {
//设置标签文本,和字体大小
departIdLabel = new JLabel("编 号:");
departIdLabel.setFont(new Font("Dialog",0,15));
//添加编号标签和其对应的文本框
centerPanel.add(departIdLabel);
centerPanel.add(departIdText);
//设置标签文本,和字体大小
firstLevelDepart = new JLabel("一级部门:") ;
firstLevelDepart.setFont(new Font("Dialog",0,15));
//添加一级部门标签和其对应的文本框
centerPanel.add(firstLevelDepart);
centerPanel.add(firstLevelText);
//设置标签文本,和字体大小
secondLevelDepart = new JLabel("二级部门:");
secondLevelDepart.setFont(new Font("Dialog",0,15));
//添加二级部门标签和其对应的文本框
centerPanel.add(secondLevelDepart);
centerPanel.add(secondLevelText);
/*初始所有文本框都设置为不可用,当在表格总选中一条记录的
* 时候,或者点击获取编号按钮的时候(即欲添加新部门的时候)
* 将部分文本再设置为可用。
*/
departIdText.setEditable(false);
firstLevelText.setEditable(false);
secondLevelText.setEditable(false);
//将中部面板添加到主面板的中部
this.add(centerPanel);
}
/**
* 下部面板的初始化,将各个按钮添加到下部面板上
*/
public void bottomPanelInit(){
//获取新编号按钮,点击按钮,分配一个编号给当前欲添加的部门
getNewIdBtn.setFont(new Font("Dialog",0,15));
getNewIdBtn.addActionListener(this);
bottomPanel.add(getNewIdBtn);
//添加新部门按钮,点击按钮,向部门表中添加一条新记录
addInfoBtn.setFont(new Font("Dialog",0,15));
addInfoBtn.addActionListener(this);
bottomPanel.add(addInfoBtn);
//修改按钮,点击按钮,可以修改部门的名称
modifyInfoBtn.setFont(new Font("Dialog",0,15));
modifyInfoBtn.addActionListener(this);
bottomPanel.add(modifyInfoBtn);
//删除部门按钮,点击按钮,如果员工表中没有该部门的员工,则允许删除一条记录。
deleteInfoBtn.setFont(new Font("Dialog",0,15));
deleteInfoBtn.addActionListener(this);
bottomPanel.add(deleteInfoBtn);
//清空按钮,当点击清空按钮,清空所有文本框中的值。
clearInfoBtn.setFont(new Font("Dialog",0,15));
clearInfoBtn.addActionListener(this);
bottomPanel.add(clearInfoBtn);
//初始各个按钮均不可用。
getNewIdBtn.setEnabled(true);
addInfoBtn.setEnabled(false);
modifyInfoBtn.setEnabled(false);
deleteInfoBtn.setEnabled(false);
clearInfoBtn.setEnabled(true);
//将下部面板添加到主面板的下部。
this.add(bottomPanel,BorderLayout.SOUTH);
}
/**
* 当选中表中的一条记录时,将该记录的信息显示在下部的文本框
*/
@Override
public void valueChanged(ListSelectionEvent lse) {
// TODO Auto-generated method stub
int selectionRow = table.getSelectedRow();
for(int i = 0; i < 3; i++) {
//将选中的记录的信息显示在下部的文本框
departIdText.setText(colValue[selectionRow][0]);
firstLevelText.setText(colValue[selectionRow][1]);
secondLevelText.setText(colValue[selectionRow][2]);
}
firstLevelText.setEditable(true); //一级部门设置为可编辑
secondLevelText.setEditable(true); //二级部门设置为可编辑
addInfoBtn.setEnabled(false); //添加信息按钮设置为不可用
modifyInfoBtn.setEnabled(true); //修改信息按钮设置为可用
deleteInfoBtn.setEnabled(true); //删除信息按钮设置为可用
clearInfoBtn.setEnabled(true); //清空各项信息
}
/**
* 为某些控件编写事件响应方法
*/
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
/**
* 如果当前点击的按钮是获取新编号,则为将要添加的
* 的部门信息分配一个新的编号
*/
if(event.getSource() == getNewIdBtn) {
setNull();
DeptBean bean = new DeptBean();
try {
int newId = bean.getNewId();
departIdText.setText(String.valueOf(newId));
} catch(Exception ex) {
ex.printStackTrace();
}
firstLevelText.setEditable(true);
secondLevelText.setEditable(true);
addInfoBtn.setEnabled(true);
modifyInfoBtn.setEnabled(false);
deleteInfoBtn.setEnabled(false);
}
else if(event.getSource() == addInfoBtn) {
//如果当前要添加该记录。则将该记录添加到表中,并更新表。
DeptBean bean = new DeptBean();
try {
boolean success = bean.addInfo(departIdText.getText(), firstLevelText.getText(), secondLevelText.getText());
//如果插入成功,重新生成界面
if(success == true) {
NodeOnePanelFive nodePanel15 = new NodeOnePanelFive();
HrMain.splitPane.setRightComponent(nodePanel15);
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
else if(event.getSource() == modifyInfoBtn) {
/*如果要修改当前记录,人员信息表中由于存在的是部门的编号,
* 所以其相应信息已经根着改变。*/
DeptBean bean = new DeptBean();
try {
boolean success = bean.modifyInfo(departIdText.getText(), firstLevelText.getText(), secondLevelText.getText());
if(success == true) {
NodeOnePanelFive nodePanel15 = new NodeOnePanelFive();
HrMain.splitPane.setRightComponent(nodePanel15);
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
else if(event.getSource() == deleteInfoBtn) {
/*如果要删除该部门,则如果人员表中没有该部门的人了,则允许删除,否则不允许删除*/
DeptBean bean = new DeptBean();
try {
//判断该id在员工信息表中是否存在,如果存在,则不能删除,否则可以删除,并将该编号存入unUsedDepartId
boolean exist = bean.isExist(departIdText.getText());
if(exist == false) {
bean.deleteInfo(departIdText.getText());
NodeOnePanelFive nodePanel15 = new NodeOnePanelFive();
HrMain.splitPane.setRightComponent(nodePanel15);
}
else {
JOptionPane.showMessageDialog(null,"该部门中尚有员工,不可删除");
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
else if(event.getSource() == clearInfoBtn) {
setNull();
}
}
/**
* 对所有文本框进行制空操作。
*/
void setNull() {
departIdText.setText(null);
firstLevelText.setText(null);
secondLevelText.setText(null);
}
}
2.人员调动管理
2.1人员调动(NodeTwoPanelOne.java)
package JobTransfer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import DataOperator.DeptBean;
import DataOperator.PersonBean;
import HRPackage.HrMain;
/**
* 该类用于实现人员调动
* @author Yaxin Wen
* @data 2017/11/13
*
*/
public class NodeTwoPanelOne extends JPanel implements ActionListener, ListSelectionListener, ItemListener {
//定义所用的面板
JPanel upPanel = new JPanel(); //上部面板
JPanel centerPanel = new JPanel(); //中部面板
JPanel bottomPanel = new JPanel(); //下部面板
JLabel title = new JLabel("人员调动"); //表明该模块的作用
JLabel pNameLabel = new JLabel();
JTextField pNameText = new JTextField(15); //存放人员姓名
JLabel oldDepartNameLabel = new JLabel();
JTextField oldDepartNameText = new JTextField(15); //显示原部门名称的文本框
JLabel newDepartNameLabel = new JLabel();
JComboBox newDepartNameCon = null;
//定义下拉列表,人员调用是将人员从原部门调用到公司已存在的另一个部门
//定义两个所需要的按钮
JButton transToNewDepartBtn = new JButton("调入新部门");
JButton clearInfoBtn = new JButton("清空信息");
//定义表格
JScrollPane jScrollPane;
JTable table;
ListSelectionModel listSelectionModel = null;
//定义表的字段
String[] colName = {"工号","姓名","性别","部门","薪酬","考核信息"};
String[][] colValue;
String pId; //人员工号
String newDeptId; //新部门编号
String newDeptName;
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints gridBagCon;
//构造函数完成该类对象的初始化工作
public NodeTwoPanelOne() {
//设置当前面板的布局方式为BorderLayout
this.setLayout(new BorderLayout());
try{
upPanelInit(); //上部面板初始化
centerPanelInit(); //中部面板初始化
bottomPanelInit(); //下部面板初始化
addActionListener(); //为相应的控件添加事件监听器
} catch (Exception ex) {
ex.printStackTrace();
}
}
//上部面板初始化函数
public void upPanelInit() throws Exception {
PersonBean bean = new PersonBean();
//设置上部面板的布局方式为网格布局
try{
upPanel.setLayout(gridBag);
title.setFont(new Font("Dialog",0,25)); //设置字号
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 0;
gridBagCon.insets = new Insets(0,10,0,10);
gridBag.setConstraints(title,gridBagCon);
upPanel.add(title);
colValue = bean.searchAll();
table = new JTable(colValue,colName);
table.setPreferredScrollableViewportSize(new Dimension(900,750));
listSelectionModel = table.getSelectionModel();
listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listSelectionModel.addListSelectionListener(this);
jScrollPane = new JScrollPane(table);
jScrollPane.setPreferredSize(new Dimension(900,750));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,0,0,0);
gridBag.setConstraints(jScrollPane,gridBagCon);
upPanel.add(jScrollPane);
this.add(upPanel,BorderLayout.NORTH);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void centerPanelInit() throws Exception{
pNameLabel.setText("姓名:"); //姓名
pNameLabel.setFont(new Font("Dialog",0,15));
oldDepartNameLabel.setText("原部门:");
oldDepartNameLabel.setFont(new Font("Dialog",0,15));
newDepartNameLabel.setText("新部门:");
newDepartNameLabel.setFont(new Font("Dialog",0,15));
try{
DeptBean bean = new DeptBean();
String[] allType = bean.getAllNodeInfor();
newDepartNameCon = new JComboBox(allType);
//将这些控件添加到面板上
centerPanel.add(pNameLabel);
centerPanel.add(pNameText);
centerPanel.add(oldDepartNameLabel);
centerPanel.add(oldDepartNameText);
centerPanel.add(newDepartNameLabel);
centerPanel.add(newDepartNameCon);
//设置所有文本框都不可更改,原来的信息不允许变动
pNameText.setEditable(false);
oldDepartNameText.setEditable(false);
this.add(centerPanel,BorderLayout.CENTER); //将该面板添加到中部
} catch(Exception ex) {
ex.printStackTrace();
}
}
public void bottomPanelInit() {
transToNewDepartBtn.setFont(new Font("Dialog",0,15));
transToNewDepartBtn.setEnabled(false);
clearInfoBtn.setFont(new Font("Dialog",0,15));
//将控件添加到下部面板上
bottomPanel.add(transToNewDepartBtn);
bottomPanel.add(clearInfoBtn);
this.add(bottomPanel,BorderLayout.SOUTH);
}
public void addActionListener() {
transToNewDepartBtn.addActionListener(this);
clearInfoBtn.addActionListener(this);
newDepartNameCon.addItemListener(this);
}
@Override
public void itemStateChanged(ItemEvent event) {
// TODO Auto-generated method stub
if(event.getStateChange() == ItemEvent.SELECTED) {
newDeptName = "" + event.getItem();
int i = newDeptName.indexOf("-");
newDeptId = newDeptName.substring(0,i);
}
}
@Override
public void valueChanged(ListSelectionEvent arg0) {
// TODO Auto-generated method stub
int selectedRow= table.getSelectedRow(); //获取选中的行号
pNameText.setText(colValue[selectedRow][1]); //获取姓名
oldDepartNameText.setText(colValue[selectedRow][3]); //原部门
pId = colValue[selectedRow][0]; //获取员工工号。
transToNewDepartBtn.setEnabled(true); //设置按钮可用
}
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
Object obj = event.getSource();
//如果事件来源是调入新部门的按钮,就执行修改操作。
if(obj == transToNewDepartBtn) {
String name = pNameText.getText(); //名字
String oldDeptName = oldDepartNameText.getText(); //旧名
PersonBean bean = new PersonBean();
try {
boolean success = bean.updateDept(pId,name, newDeptId, oldDeptName, newDeptName);
if(success == true) {
NodeTwoPanelOne nodePanel21 = new NodeTwoPanelOne();
HrMain.splitPane.setRightComponent(nodePanel21);
}
} catch(Exception ex) {
ex.printStackTrace();
}
} else if(obj == clearInfoBtn) {
setNull();
transToNewDepartBtn.setEnabled(false);
}
}
void setNull() {
pNameText.setText(null);
oldDepartNameText.setText(null);
}
}
2.2调动历史查询(NodeTwoPanelTwo.java)
package JobTransfer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import DataOperator.JobChangeHistory;
/**
* 该模块用来显示人员调动的历史
* @author 57215
*
*/
public class NodeTwoPanelTwo extends JPanel implements ActionListener {
//定义所用面板
JPanel upPanel = new JPanel();
JLabel title = new JLabel("调动历史查询");
//定义表格
JScrollPane jScrollPane;
JTable table;
ListSelectionModel listSelectionModel = null;
String[] colName = {"流水号","员工编号","员工姓名","原部门","新部门","变更次数","变更日期"};
String[][] colValue;
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints gridBagCon;
//构造函数完成对象的初始化工作
public NodeTwoPanelTwo() {
this.setLayout(new BorderLayout());
try {
upPanelInit();
} catch(Exception ex) {
ex.printStackTrace();
}
}
//上部面板初始化
public void upPanelInit() {
JobChangeHistory bean = new JobChangeHistory();
upPanel.setLayout(gridBag);
title.setFont(new Font("Dialog",0,25));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 0;
gridBagCon.insets = new Insets(0,10,0,10);
gridBag.setConstraints(title, gridBagCon);
upPanel.add(title);
colValue = bean.searchAll();
table = new JTable(colValue,colName);
table.setPreferredScrollableViewportSize(new Dimension(900,750));
jScrollPane = new JScrollPane(table);
jScrollPane.setPreferredSize(new Dimension(900,750));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,0,0,0);
gridBag.setConstraints(jScrollPane, gridBagCon);
upPanel.add(jScrollPane);
this.add(upPanel);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
3.人员考核管理
3.1人员考核(NodeThreePanelOne.java)
package PersonAccess;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import DataOperator.PersonBean;
import HRPackage.HrMain;
/**
* 该模块用来实现人员的考核功能。
* @author Yaxin Wen
* @data 2017/11/13
*/
public class NodeThreePanelOne extends JPanel implements ActionListener, ItemListener, ListSelectionListener {
//定义所用的面板
JPanel upPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel bottomPanel = new JPanel();
//当以三个所需要的静态文本
JLabel title = new JLabel("人员考核");
JLabel pNameLabel = new JLabel();
JTextField pNameText = new JTextField(15);
JLabel oldAccessRecordLabel = new JLabel();
JTextField oldAccessRecordText = new JTextField(10);
JLabel newAccessRecordLabel = new JLabel();
JComboBox newAccessRecordCom = null;
JButton okBtn = new JButton("确定");
JButton clearBtn = new JButton("清空");
String pId; //人员编号
String pName; //人员姓名
String oldAssess; //上次考核
String newAssess; //本次考核
//定义表格
JScrollPane jScrollPane;
JTable table;
ListSelectionModel listSelectionModel = null;
String[] colName = {"工号","姓名","性别","部门","薪酬","考核信息"};
String[][] colValue;
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints gridBagCon = new GridBagConstraints();
//构造寒素完成对象的初始化工作
public NodeThreePanelOne() {
//设置当前面板布局方式为borderLayout
this.setLayout(new BorderLayout());
try {
upPanelInit();
centerPanelInit();
bottomPanelInit();
addActionListener();
} catch(Exception ex) {
ex.printStackTrace();
}
}
//该函数用来完成上部面板的初始化工作
public void upPanelInit() throws Exception{
PersonBean bean = new PersonBean();
//设置上部面板的布局方式为网格布局
upPanel.setLayout(gridBag);
try{
title.setFont(new Font("Dialog",0,25));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 0;
gridBagCon.insets = new Insets(0,10,0,10);
gridBag.setConstraints(title,gridBagCon);
upPanel.add(title);
colValue = bean.searchAll();
table = new JTable(colValue,colName);
table.setPreferredScrollableViewportSize(new Dimension(900,750));
listSelectionModel = table.getSelectionModel();
listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listSelectionModel.addListSelectionListener(this);
jScrollPane = new JScrollPane(table);
jScrollPane.setPreferredSize(new Dimension(900,750));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,0,0,0);
gridBag.setConstraints(jScrollPane,gridBagCon);
upPanel.add(jScrollPane);
this.add(upPanel,BorderLayout.NORTH);
} catch(Exception ex) {
ex.printStackTrace();
}
}
public void centerPanelInit() throws Exception {
pNameLabel.setText("姓名:");
pNameLabel.setFont(new Font("Dialog",0,15));
oldAccessRecordLabel.setText("上次考核:");
oldAccessRecordLabel.setFont(new Font("Dialog",0,15));
newAccessRecordLabel.setText("本次考核:");
newAccessRecordLabel.setFont(new Font("Dialog",0,15));
String[] value = {"优秀","合格","不合格"};
newAccessRecordCom = new JComboBox(value);
pNameText.setEditable(false);
oldAccessRecordText.setEditable(false);
okBtn.setEnabled(false);
centerPanel.add(pNameLabel);
centerPanel.add(pNameText);
centerPanel.add(oldAccessRecordLabel);
centerPanel.add(oldAccessRecordText);
centerPanel.add(newAccessRecordLabel);
centerPanel.add(newAccessRecordCom);
this.add(centerPanel);
}
public void bottomPanelInit() {
okBtn.setFont(new Font("Dialog",0,15));
clearBtn.setFont(new Font("Dialog",0,15));
bottomPanel.add(okBtn);
bottomPanel.add(clearBtn);
this.add(bottomPanel,BorderLayout.SOUTH);
}
void addActionListener() {
okBtn.addActionListener(this);
clearBtn.addActionListener(this);
newAccessRecordCom.addItemListener(this);
}
void setNull() {
pNameText.setText(null);
oldAccessRecordText.setText(null);
pNameText.setEditable(false);
oldAccessRecordText.setEditable(false);
okBtn.setEnabled(false);
clearBtn.setEnabled(false);
newAccessRecordCom.setEnabled(false);
}
@Override
public void valueChanged(ListSelectionEvent lse) {
// TODO Auto-generated method stub
int selectedRow = table.getSelectedRow();
pId = colValue[selectedRow][0];
pName = colValue[selectedRow][1];
pNameText.setText(colValue[selectedRow][1]);
oldAccessRecordText.setText(colValue[selectedRow][5]);
oldAssess = colValue[selectedRow][5]; //旧的考核信息
okBtn.setEnabled(true);
}
@Override
public void itemStateChanged(ItemEvent event) {
// TODO Auto-generated method stub
if(event.getStateChange() == ItemEvent.SELECTED) {
newAssess = "" + event.getItem();
}
}
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
Object obj = event.getSource();
//点击确定按钮的时候,进行信息更改
if(obj == okBtn) {
PersonBean bean = new PersonBean();
try {
boolean success = bean.updateAssess(pId, pName, oldAssess, newAssess);
if(success == true) {
NodeThreePanelOne nodePanel31 = new NodeThreePanelOne();
HrMain.splitPane.setRightComponent(nodePanel31);
}
} catch(Exception ex) {
ex.printStackTrace();
}
} else if(obj == clearBtn) {
setNull();
okBtn.setEnabled(false);
}
}
}
3.2考核历史查询(NodeThreePanelTwo.java)
package PersonAccess;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
import DataOperator.AssessChangeHistory;
/**
* 该模块用来实现人员考核历史的查询。
* @author YaXin Wen
* @date 2017/11/18
*
*/
public class NodeThreePanelTwo extends JPanel {
JPanel upPanel = new JPanel();
JLabel title = new JLabel("人员考核历史查询");
//定义表格
JScrollPane jScrollPane;
JTable table = new JTable();
ListSelectionModel listSelectionModel = null;
String[] colName = {"流水号","员工编号","员工姓名","上次考核","本次考核","变更次数","变更日期"};
String[][] colValue;
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints gridBagCon = new GridBagConstraints();
//构造函数完成对象的初始化工作
public NodeThreePanelTwo() {
try {
upPanelInit();
} catch(Exception ex) {
ex.printStackTrace();
}
}
public void upPanelInit() {
AssessChangeHistory bean = new AssessChangeHistory();
upPanel.setLayout(gridBag);
title.setFont(new Font("Dialog",0,15));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 0;
gridBagCon.insets = new Insets(0,10,0,10);
gridBag.setConstraints(title, gridBagCon);
upPanel.add(title);
try {
colValue = bean.searchAll();
table = new JTable(colValue,colName);
table.setPreferredScrollableViewportSize(new Dimension(900,750));
jScrollPane = new JScrollPane(table);
jScrollPane.setPreferredSize(new Dimension(900,750));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,0,0,0);
gridBag.setConstraints(jScrollPane, gridBagCon);
upPanel.add(jScrollPane);
} catch(Exception ex) {
ex.printStackTrace();
}
this.add(upPanel);
}
}
4.劳资管理
4.1劳资分配管理(NodeFourPanelOne.java)
package LaborManagement;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import DataOperator.PersonBean;
import HRPackage.HrMain;
/**
* @author Yaxin Wen
* @date 2017/11/18
*/
public class NodeFourPanelOne extends JPanel implements ActionListener, ListSelectionListener {
//定义所用的面板
JPanel upPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel bottomPanel = new JPanel();
//定义四个所需的静态文本
JLabel title = new JLabel("劳资分配");
JLabel pNameLabel = new JLabel("姓名:");
JLabel oldSalaryLabel = new JLabel("调整前的工资:");
JLabel newSalaryLabel = new JLabel("调整后的工资:");
//定义三个需要的文本框
JTextField pNameText = new JTextField(10);
JTextField oldSalaryText = new JTextField(10);
JTextField newSalaryText = new JTextField(10);
//定义需要用的两个按钮
JButton okBtn = new JButton("确定");
JButton clearBtn = new JButton("清空");
//定义表格
JScrollPane jScrollPane;
JTable table;
ListSelectionModel listSelectionModel = null;
String[] colName = {"工号","姓名","性别","部门","薪酬","考核信息"};
String[][] colValue;
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints gridBagCon;
String pId; //员工编号
String pName; //员工姓名
String oldSalary; //原来的工资
String newSalary; //新调整的工资。
public NodeFourPanelOne() {
//设置布局方式为BorderLayout
this.setLayout(new BorderLayout());
try {
upPanelInit(); //上部面板布局
centerPanelInit();
bottomPanelInit();
addActionListener();
} catch(Exception ex) {
ex.printStackTrace();
}
}
public void upPanelInit() throws Exception{
PersonBean bean = new PersonBean();
upPanel.setLayout(gridBag);
try {
title.setFont(new Font("Dialog",0,25));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 0;
gridBagCon.insets = new Insets(0,10,0,10);
gridBag.setConstraints(title,gridBagCon);
upPanel.add(title);
colValue = bean.searchAll();
table = new JTable(colValue,colName);
table.setPreferredScrollableViewportSize(new Dimension(900,750));
listSelectionModel = table.getSelectionModel();
listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listSelectionModel.addListSelectionListener(this);
jScrollPane = new JScrollPane(table);
jScrollPane.setPreferredSize(new Dimension(900,750));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,0,0,0);
gridBag.setConstraints(jScrollPane,gridBagCon);
upPanel.add(jScrollPane);
this.add(upPanel,BorderLayout.NORTH);
} catch(Exception ex) {
ex.printStackTrace();
}
}
//中部面板的布局
public void centerPanelInit() throws Exception {
pNameLabel.setFont(new Font("Dialog",0,15));
oldSalaryLabel.setFont(new Font("Dialog",0,15));
newSalaryLabel.setFont(new Font("Dialog",0,15));
//将控件添加到面板上
centerPanel.add(pNameLabel);
centerPanel.add(pNameText);
centerPanel.add(oldSalaryLabel);
centerPanel.add(oldSalaryText);
centerPanel.add(newSalaryLabel);
centerPanel.add(newSalaryText);
pNameText.setEditable(false);
oldSalaryText.setEditable(false);
newSalaryText.setEditable(false);
this.add(centerPanel);
}
public void bottomPanelInit() {
okBtn.setFont(new Font("Dialog",0,15));
clearBtn.setFont(new Font("Dialog",0,15));
okBtn.setEnabled(false);
clearBtn.setEnabled(true);
bottomPanel.add(okBtn);
bottomPanel.add(clearBtn);
this.add(bottomPanel,BorderLayout.SOUTH);
}
public void addActionListener() {
okBtn.addActionListener(this);
clearBtn.addActionListener(this);
}
@Override
public void valueChanged(ListSelectionEvent lse) {
// TODO Auto-generated method stub
//当表格被选中的时候执行这些操作
int selectedRow = table.getSelectedRow();
pId = colValue[selectedRow][0]; //员工编号
pName = colValue[selectedRow][1]; //员工姓名
oldSalary = colValue[selectedRow][4]; //原来的工资
pNameText.setText(pName);
oldSalaryText.setText(oldSalary);
newSalaryText.setEditable(true);
okBtn.setEnabled(true);
}
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
Object obj = new Object();
obj = event.getSource();
if(obj == okBtn) {
PersonBean bean = new PersonBean();
try {
newSalary = newSalaryText.getText();
boolean success = bean.updateSalary(pId, pName, oldSalary, newSalary);
if(success == true) {
NodeFourPanelOne nodePanel41 = new NodeFourPanelOne();
HrMain.splitPane.setRightComponent(nodePanel41);
}
} catch(Exception ex) {
ex.printStackTrace();
}
} else if(obj == clearBtn){
setNull();
okBtn.setEnabled(false);
}
}
void setNull() {
pNameText.setText(null);
oldSalaryText.setText(null);
newSalaryText.setText(null);
pNameText.setEditable(false);
oldSalaryText.setEditable(false);
newSalaryText.setEditable(false);
okBtn.setEnabled(false);
}
}
4.2劳资历史查询(NodeFourPanelTwo.java)
package LaborManagement;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
import DataOperator.salaryChangeHistory;
/**
* @author Yaxin Wen
* @date 2017/11/18
*/
public class NodeFourPanelTwo extends JPanel {
JPanel upPanel = new JPanel();
JLabel title = new JLabel("劳资管理历史查询");
//定义表格
JScrollPane jScrollPane;
JTable table;
ListSelectionModel listSelectionModel = null;
String[] colName = {"流水号","员工编号","员工姓名","原薪资","当前薪资","变更次数","变更日期"};
String[][] colValue;
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints gridBagCon;
//构造函数完成初始化工作
public NodeFourPanelTwo() {
try {
upPanelInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public void upPanelInit() {
upPanel.setLayout(gridBag);
salaryChangeHistory bean = new salaryChangeHistory();
title.setFont(new Font("Dialog",0,25));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 0;
gridBagCon.insets = new Insets(0,10,0,10);
gridBag.setConstraints(title, gridBagCon);
upPanel.add(title);
try {
colValue = bean.searchAll();
table = new JTable(colValue,colName);
table.setPreferredScrollableViewportSize(new Dimension(900,750));
jScrollPane = new JScrollPane(table);
jScrollPane.setPreferredSize(new Dimension(900,750));
gridBagCon = new GridBagConstraints();
gridBagCon.gridx = 0;
gridBagCon.gridy = 1;
gridBagCon.insets = new Insets(0,0,0,0);
gridBag.setConstraints(jScrollPane, gridBagCon);
upPanel.add(jScrollPane);
}
catch(Exception ex) {
ex.printStackTrace();
}
this.add(upPanel);
}
}
5.数据库操作
5.1 数据库操作文件(Database.java)
package DataOperator;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* 连接数据库操作的类
* @author 温雅新
* @date 2017/11/15
*/
public class Database {
private Statement stmt = null;
ResultSet rs = null;
private Connection conn = null;
String url = "jdbc:Access:///E:/HRdatabase.accdb"; //数据库链接
//构造函数
public Database() {
}
/**
* 打开数据库连接
*/
public void OpenConn() throws Exception {
try {
Class.forName("com.hxtt.sql.access.AccessDriver");
conn = DriverManager.getConnection(url);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 执行查询语句返回结果集rs
*/
public ResultSet QueryInfo(String sql) {
stmt = null;
rs = null;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (Exception ex) {
System.err.println("executeQuery:" + ex.getMessage());
}
return rs;
}
public int RecordNumber(String sql) {
stmt = null;
rs = null;
int row = 0;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
if(rs.last()) {
row = rs.getRow();
}
} catch(Exception ex) {
System.err.println("executeQuery:" + ex.getMessage());
}
return row;
}
public int getMaxId(String sql) throws Exception {
stmt = null;
rs = null;
int number = -1;
try{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
while(rs.next()) {
if(number < rs.getInt(1)) {
number = rs.getInt(1);
}
}
} catch(Exception ex) {
System.err.println("executeQuery:" + ex.getMessage());
}
return number;
}
public int getMinId(String sql) throws Exception {
stmt = null;
rs = null;
int number = 100000;
try{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
while(rs.next()) {
if(number > rs.getInt(1)) {
number = rs.getInt(1);
}
}
} catch(Exception ex) {
System.err.println("executeQuery:" + ex.getMessage());
}
return number;
}
/**
* 执行更新语句
*/
public void UpdateInfo(String sql) {
stmt = null;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
} catch(Exception ex) {
System.err.println("executeQuery:" + ex.getMessage());
}
}
/**
* 关闭Statement
*/
public void closeStmt() {
try{
stmt.close();
} catch(Exception ex) {
System.err.println("executeQuery:" + ex.getMessage());
}
}
/**
* 关闭数据库连接
*/
public void closeConn() {
try{
conn.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
5.2 员工信息表操作文件(PersonBean.java)
package DataOperator;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
/**
* 该类用来实现员工信息的增删查改。
* @author YaXin Wen
* @date 2017/11/15
*/
public class PersonBean {
ResultSet rs = null;
String sql; //存放sql语句
String pId; //员工编号
String pName; //员工姓名
String pSex; //员工性别
String pBirth; //出生年月
String pNation; //民族
String pAddress; //地址
String Did; //所属部门编号
String pSalary; //工资
String pAssess; //是否考核
String pOther; //其他
/**
* @param name 员工姓名
* @param sex 员工性别
* @param birth 员工出生年月
* @param nation 员工民族
* @param address 员工地址
* @return 如果信息合法,返回true,否则返回false
*/
public boolean isLegal(String name,String sex,String birth,String nation,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(nation==null || nation.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 nation 员工民族
* @param address 员工家庭住址
* @param did 员工所属部门
* @param salary 员工薪资
* @param access 员工考核
* @param other 其他信息
*/
public void addInfo(String id,String name,String sex,String birth,String nation,String address,String did,String salary,String access,String other) {
//如果信息合法
this.pId = id;
this.pName = name;
this.pSex = sex;
this.pBirth = birth;
this.pNation = nation;
this.pAddress = address;
this.Did = did;
this.pSalary = salary;
this.pAssess = access;
this.pOther = other;
if(isLegal(name,sex,birth,nation,address)) {
Database db = new Database();
sql = "insert into Person(pId,pName,pSex,pBirth,pNation,pAddress,DeptId,pSalary,pAccess,pOther) ";
String temp = "values('"+pId+"','"+pName+"','"+pSex+"','"+pBirth+"','"+pNation+"','"+pAddress+"','"+Did+"','"+pSalary+"','"+pAssess+"','"+pOther+"')";
sql += temp;
try {
db.OpenConn(); //打开连接
db.UpdateInfo(sql); //插入数据
//删除未使用编号表中的相关的编号
int t = Integer.valueOf(pId);
sql = "delete from unUsedPersonId where personId='"+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();
}
}
}
/**
* 该函数用来实现信息的修改。
* @param id
* @param name
* @param sex
* @param birth
* @param nation
* @param address
* @param did
* @param salary
* @param access
* @param other
*/
public void modifyInfo(String id,String name,String sex,String birth,String nation,String address,String did,String salary,String access,String other) {
//如果信息合法
this.pId = id;
this.pName = name;
this.pSex = sex;
this.pBirth = birth;
this.pNation = nation;
this.pAddress = address;
this.Did = did;
this.pSalary = salary;
this.pAssess = access;
this.pOther = other;
if(isLegal(name,sex,birth,nation,address)) {
Database db = new Database();
try {
int temp = Integer.valueOf(id);
sql = "update Person set pName='"+pName+"',pSex='"+pSex+"',pBirth='"+pBirth+"',pNation='"+pNation+"',pOther='"+pOther+"' 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) {
Database db = new Database();
int Did = Integer.valueOf(newDeptId);
int Pid = Integer.valueOf(id);
sql = "update Person set DeptId='"+Did+"' where pId='"+Pid+"'";
try {
db.OpenConn();
db.UpdateInfo(sql); //改变部门编号
JOptionPane.showMessageDialog(null, "人员调动成功");
sql = "select * from jobChange where pId='"+Pid+"'";
int time = 0;
rs = db.QueryInfo(sql);
if(rs.last()) {
time = rs.getRow();
}
time++;
//获取当前时间
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,modifyTime,modifyDate) values('"+id+"','"+name+"','"+oldName+"','"+newName+"','"+String.valueOf(time)+"','"+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 oldAssess,String newAssess) {
Database db = new Database();
int temp = Integer.valueOf(id); //将编号转换成整数
sql = "update Person set pAccess='"+newAssess+"' where pId='"+temp+"'";
try {
db.OpenConn();
db.UpdateInfo(sql);
JOptionPane.showMessageDialog(null, "考核信息修改成功");
sql = "select * from assessChange where pId='"+temp+"'";
rs = db.QueryInfo(sql);
int time = 0;
if(rs.last()) {
time = rs.getRow();
}
time++;
//获取当前时间
Date nowTime = new Date();
//设置时间格式
SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//日期
String date = matter.format(nowTime);
sql = "insert into assessChange(pId,pName,oldAssess,newAssess,modifyTime,modifyDate) values('"+id+"','"+name+"','"+oldAssess+"','"+newAssess+"','"+time+"','"+date+"')";
db.UpdateInfo(sql);
db.closeStmt();
db.closeConn();
return true;
} catch(Exception ex) {
ex.printStackTrace();
db.closeStmt();
db.closeConn();
return false;
}
}
public boolean updateSalary(String id,String name,String oldSalary,String newSalary) {
Database db = new Database();
int temp = Integer.valueOf(id);
sql = "update Person set pSalary='"+newSalary+"' where pId='"+temp+"'";
try {
db.OpenConn();
db.UpdateInfo(sql);
JOptionPane.showMessageDialog(null, "成功修改薪资");
sql = "select * from salaryChange where pId='"+temp+"'";
rs = db.QueryInfo(sql);
int time = 0;
if(rs.last()) {
time = rs.getRow();
}
time++;
//获取当前时间
Date nowTime = new Date();
//设置时间格式
SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//日期
String date = matter.format(nowTime);
sql = "insert into salaryChange(pId,pName,oldSalary,newSalary,modifyTime,modifyDate) values('"+id+"','"+name+"','"+oldSalary+"','"+newSalary+"','"+time+"','"+date+"')";
db.UpdateInfo(sql);
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 Person 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("pNation");
s[5] = rs.getString("pAddress");
s[6] = rs.getString("DeptId");
s[7] = rs.getString("pSalary");
s[8] = rs.getString("pAccess");
s[9] = rs.getString("pOther");
}
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 Person order by pId"; //查找该字段的所有值
db.OpenConn();
rs = db.QueryInfo(sql);
if(rs.last()) {
row = rs.getRow();
}
if(row == 0) {
s = new String[1];
s[0] = " ";
}
else {
s = new String[row];
rs.first();
rs.previous();
int i = 0;
while(rs.next()) {
s[i++] = rs.getString(1);
}
}
} catch(Exception ex) {
ex.printStackTrace();
} finally {
db.closeStmt();
db.closeConn();
}
return s;
}
//人员调动模块使用
public String[][] searchAll() {
Database db = new Database();
String[][] sn = null;
int row = 0;
int i = 0;
sql = "select pId,pName,pSex,DeptId,pSalary,pAccess from Person order by pId";
try {
db.OpenConn();
rs = db.QueryInfo(sql);
if(rs.last()) {
row = rs.getRow();
}
if(row == 0) {
sn = new String[1][6];
sn[0][0] = " ";
sn[0][1] = " ";
sn[0][2] = " ";
sn[0][3] = " ";
sn[0][4] = " ";
sn[0][5] = " ";
}
else {
sn = new String[row][6];
rs.first();
rs.previous();
while(rs.next()) {
sn[i][0] = rs.getString("pId");
sn[i][1] = rs.getString("pName");
sn[i][2] = rs.getString("pSex");
sn[i][3] = rs.getString("DeptId");
if(sn[i][3].equals("0")) {
sn[i][3] = "0-未分配部门";
}
else {
int temp = Integer.valueOf(sn[i][3]);
sql = "select FatherDept,SonDept from DeptTable where DeptId='"+temp+"'";
ResultSet rs1 = db.QueryInfo(sql);
if(rs1.next()) {
sn[i][3] += "-" + rs1.getString("FatherDept") + "-" + rs1.getString("SonDept");
}
}
sn[i][4] = rs.getString("pSalary");
sn[i][5] = rs.getString("pAccess");
i++;
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally{
db.closeStmt();
db.closeConn();
}
return sn;
}
public String[][] searchAllForNode() {
Database db = new Database();
String[][] sn = null;
int row = 0;
int i = 0;
sql = "select pId,pName,pBirth,pNation,pAddress,DeptId from Person order by pId";
try {
db.OpenConn();
rs = db.QueryInfo(sql);
if(rs.last()) {
row = rs.getRow();
}
if(row == 0) {
sn = new String[1][6];
sn[0][0] = " ";
sn[0][1] = " ";
sn[0][2] = " ";
sn[0][3] = " ";
sn[0][4] = " ";
sn[0][5] = " ";
} else {
sn = new String[row][6];
rs.first();
rs.previous();
while(rs.next()) {
sn[i][0] = rs.getString("pId"); //编号
sn[i][1] = rs.getString("pName"); //姓名
sn[i][2] = rs.getString("pBirth"); //出生年月
sn[i][3] = rs.getString("pNation"); //民族
sn[i][4] = rs.getString("pAddress"); //地址
sn[i][5] = rs.getString("DeptId"); //部门
if(sn[i][5].equals("0")) {
sn[i][5] = "未分配部门";
} else {
int temp = Integer.valueOf(sn[i][5]);
sql = "select FatherDept,SonDept from DeptTable where DeptId = '"+temp+"'";
ResultSet rs1 = db.QueryInfo(sql);
if(rs1 != null) {
if(rs1.next()) {
sn[i][5] = "" + rs1.getString("FatherDept") +"-" + rs1.getString("SonDept");
}
}
}
i++;
}
}
} catch(Exception ex) {
ex.printStackTrace();
} finally {
db.closeStmt();
db.closeConn();
}
return sn;
}
public boolean deleteInfo(String id) {
this.pId = id;
int temp = Integer.valueOf(pId);
Database db = new Database();
sql = "delete from Person where pId='"+temp+"'";
try {
db.OpenConn();
db.UpdateInfo(sql);
JOptionPane.showMessageDialog(null,"成功删除一条记录");
sql = "insert into unUsedPersonId(personId) values('"+temp+"')";
db.UpdateInfo(sql);
db.closeStmt();
db.closeConn();
return true;
} catch(Exception ex) {
JOptionPane.showMessageDialog(null, "删除信息失败");
db.closeStmt();
db.closeConn();
return false;
}
}
/**
* 该函数实现获取新编号的功能
* @return 返回新编号
*/
public int getNewId() {
Database db = new Database();
try{
db.OpenConn(); //打开连接
sql = "select * from Person"; //获取员工表的记录条数
int number = db.RecordNumber(sql); ///获取表的记录数
if(number == 0) {
db.closeStmt();
db.closeConn();
return 1;///如果表中没有记录。
}
int Max = db.getMaxId(sql); ///获取最大编号
//如果最大表闹和表中记录条数一样,则返回number+1
if(Max == number) {
db.closeStmt();
db.closeConn();
Max++;
return Max;
}
else { ///
sql = "select * from unUsedPersonId";
int Min = db.getMinId(sql);
db.closeStmt();
db.closeConn();
return Min;
}
} catch(Exception ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(null,"出错","错误",JOptionPane.ERROR_MESSAGE);
db.closeStmt();
db.closeConn();
return -1;
}
}
}
5.3 部门信息表操作文件(DeptBean.java)
package DataOperator;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
/**
* 该类用来实现对部门相关信息进行数据库操作的功能
* @author YaXin Wen
* @date 2017/11/15
*/
public class DeptBean {
ResultSet rs = null;
String sql; //存放sql语句
String Did; //部门编号
String firstLevel; //一级部门
String secondLevel; //二级部门
String colName; //列名
String colValue; //列值
String colValue2; //列值
/**
* 添加相关信息
*/
public boolean addInfo(String id,String first,String second) {
Database db = new Database();
this.Did = id;
this.firstLevel = first;
this.secondLevel = second;
if(firstLevel == null || firstLevel == "") {
JOptionPane.showMessageDialog(null, "请输入一级部门名称","错误",JOptionPane.ERROR_MESSAGE);
return false; //记录添加失败
}
else if(secondLevel == null || secondLevel == "") {
JOptionPane.showConfirmDialog(null, "请输入二级部门名称","错误",JOptionPane.ERROR_MESSAGE);
return false; //记录添加失败
}
sql = "insert into DeptTable(DeptId,FatherDept,SonDept) values('"+Did+"','"+firstLevel+"','"+secondLevel+"')";
try {
db.OpenConn();
db.UpdateInfo(sql);
JOptionPane.showMessageDialog(null, "成功添加一条记录");
sql = "delete from unUsedDeptId where DeptId = '"+id+"'";
db.UpdateInfo(sql);
db.closeStmt();
db.closeConn();
return true;
} catch(Exception ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(null, "保存失败","错误",JOptionPane.ERROR_MESSAGE);
db.closeStmt();
db.closeConn();
return false;
}
}
//获取部门编号
public int getNewId() throws Exception{
Database db = new Database();
db.OpenConn(); //打开连接
sql = "select * from DeptTable";
int number = db.RecordNumber(sql); ///获取表的记录数
if(number == 0) {
db.closeStmt();
db.closeConn();
return 1;///如果表中没有记录。
}
int Max = db.getMaxId(sql); ///获取最大编号
//如果最大表闹和表中记录条数一样,则返回number+1
if(Max == number) {
db.closeStmt();
db.closeConn();
Max++;
return Max;
}
else { ///
sql = "select * from unUsedDeptId";
int Min = db.getMinId(sql);
db.closeStmt();
db.closeConn();
return Min;
}
}
/**
* @param id 部门编号
* @param first 一级部门名称
* @param second 二级部门名称
* @return 修改成功返回true,否则返回false
* @throws Exception
*/
public boolean modifyInfo(String id,String first,String second) throws Exception {
Database db = new Database();
this.Did = id;
this.firstLevel = first;
this.secondLevel = second;
if(firstLevel == null || firstLevel == "") {
JOptionPane.showMessageDialog(null, "请输入一级部门名称","错误",JOptionPane.ERROR_MESSAGE);
return false; //记录添加失败
}
else if(secondLevel == null || secondLevel == "") {
JOptionPane.showConfirmDialog(null, "请输入二级部门名称","错误",JOptionPane.ERROR_MESSAGE);
return false; //记录添加失败
}
int temp = Integer.valueOf(Did);
sql = "update DeptTable set FatherDept='"+first+"',SonDept='"+second+"' where DeptId='"+temp+"'";
try {
db.OpenConn();
db.UpdateInfo(sql); //更新信息
JOptionPane.showMessageDialog(null, "成功修改一条记录");
/**
* 将相应的人员信息也进行更新。
*/
db.closeStmt();
db.closeConn();
return true;
} catch(Exception ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(null, "保存失败","错误",JOptionPane.ERROR_MESSAGE);
db.closeStmt();
db.closeConn();
return false;
}
}
/**
* 判断符合要求的记录是否存在。如果存在返回true,否则返回false;
* @param id
* @return
* @throws Exception
*/
public boolean isExist(String id) throws Exception{
Database db = new Database();
int temp = Integer.valueOf(id);
sql = "select * from Person where DeptId = '"+id+"'";
boolean flag = false; //假设不存在。
try {
db.OpenConn();
rs = db.QueryInfo(sql);
int num = 0;
while(rs.next()) {
num++;
db.closeStmt();
db.closeConn();
return true;
}
} catch(Exception ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(null, "修改失败","错误",JOptionPane.ERROR_MESSAGE);
db.closeStmt();
db.closeConn();
return false;
}
return flag;
}
public boolean deleteInfo(String id) {
Database db = new Database();
this.Did = id;
int temp = Integer.valueOf(Did);
sql = "delete from DeptTable where DeptId = '"+temp+"'";
try {
db.OpenConn();
db.UpdateInfo(sql); //删除该记录
JOptionPane.showMessageDialog(null, "成功删除一条记录");
//把编号插入到未使用编号表中
sql = "insert into unUsedDeptId(DeptId) values('"+Did+"')";
db.UpdateInfo(sql);
db.closeStmt();
db.closeConn();
return true;
} catch(Exception ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(null, "删除失败","错误",JOptionPane.ERROR_MESSAGE);
db.closeStmt();
db.closeConn();
return false;
}
}
public String[] getAllNodeInfor() throws Exception{
Database db = new Database();
String[] sn = null;
int row = 0;
int i = 0;
sql = "select * from DeptTable order by DeptId";
try {
db.OpenConn();
rs = db.QueryInfo(sql);
if(rs.last()) {
row = rs.getRow();
}
if(row == 0) {
sn = new String[1];
sn[0] = " ";
}
else {
sn = new String[row];
rs.first();
rs.previous();
while(rs.next()) {
sn[i] = "";
sn[i] += rs.getString("DeptId");
sn[i] += "-";
sn[i] += rs.getString("FatherDept");
sn[i] += "-";
sn[i] += rs.getString("SonDept");
i++;
}
}
} catch(Exception ex) {
System.out.println(ex);
JOptionPane.showMessageDialog(null, "失败","错误",JOptionPane.ERROR_MESSAGE);
} finally{
db.closeStmt();
db.closeConn();
}
return sn;
}
/**
* 查询所有记录
*/
public String[][] searchAll() throws Exception {
Database db = new Database();
String[][] sn = null;
int row = 0;
int i = 0;
sql = "select * from DeptTable order by DeptId";
try {
db.OpenConn();
rs = db.QueryInfo(sql);
if(rs.last()) {
row = rs.getRow();
}
if(row == 0) {
sn = new String[1][3];
sn[0][0] = " ";
sn[0][1] = " ";
sn[0][2] = " ";
}
else {
sn = new String[row][3];
rs.first();
rs.previous();
while(rs.next()) {
sn[i][0] = rs.getString("DeptId");
sn[i][1] = rs.getString("FatherDept");
sn[i][2] = rs.getString("SonDept");
i++;
}
}
} catch(Exception ex) {
ex.printStackTrace();
} finally{
db.closeStmt();
db.closeConn();
}
return sn;
}
}
5.4 人员调动记录表操作文件(JobChangeHistory.java)
package DataOperator;
import java.sql.ResultSet;
/**
* 该模块用于实现对jobChange表的各种操作
* @author YaXin Wen
* @date 2017/11/18
*/
public class JobChangeHistory {
String sql;
ResultSet rs;
public String[][] searchAll(){
Database db = new Database();
String[][] sn = null;
sql = "select * from jobChange";
int row = 0;
int i = 0;
try {
db.OpenConn();
rs = db.QueryInfo(sql);
if(rs.last()) {
row = rs.getRow();
}
if(row == 0) {
sn = new String[1][7];
for(int j = 0; j < 7; j++) {
sn[0][j] = " ";
}
}
else {
sn = new String[row][7];
rs.first();
rs.previous();
while(rs.next()) {
sn[i][0] = rs.getString("operatorId");
sn[i][1] = rs.getString("pId");
sn[i][2] = rs.getString("pName");
sn[i][3] = rs.getString("oldDept");
sn[i][4] = rs.getString("newDept");
sn[i][5] = rs.getString("modifyTime");
sn[i][6] = rs.getString("modifyDate");
i++;
}
}
} catch(Exception ex) {
ex.printStackTrace();
}
return sn;
}
}
5.5 人员考核记录表操作文件(AssessChangeHistroy.java)
package DataOperator;
import java.sql.ResultSet;
/**
*
* @author YaXin Wen
* @date 2017/11/18
*/
public class AssessChangeHistory {
String sql;
ResultSet rs;
public String[][] searchAll() {
Database db = new Database();
String[][] sn=null;
int row=0,i=0;
sql = "select * from assessChange";
try {
db.OpenConn();
rs = db.QueryInfo(sql);
if(rs.last()) {
row = rs.getRow();
}
if(row == 0) {
sn = new String[1][7];
for(int j = 0; j < 7; j++) {
sn[1][j] = " ";
}
}
else {
sn = new String[row][7];
rs.first();
rs.previous();
while(rs.next()) {
sn[i][0] = rs.getString("operatorId");
sn[i][1] = rs.getString("pId");
sn[i][2] = rs.getString("pName");
sn[i][3] = rs.getString("oldAssess");
sn[i][4] = rs.getString("newAssess");
sn[i][5] = rs.getString("modifyTime");
sn[i][6] = rs.getString("modifyDate");
i++;
}
}
} catch(Exception ex) {
ex.printStackTrace();
} finally {
db.closeStmt();
db.closeConn();
}
return sn;
}
}
5.6 劳资记录表操作文件(salaryChangeHistroy.java)
package DataOperator;
import java.sql.ResultSet;
/**
*
* @author YaXin Wen
* @date 2017/11/18
*/
public class salaryChangeHistory {
String sql;
ResultSet rs;
public String[][] searchAll() {
Database db = new Database();
String[][] sn = null;
int row = 0,i = 0;
sql = "select * from salaryChange";
try {
db.OpenConn();
rs = db.QueryInfo(sql);
if(rs.last()) {
row = rs.getRow();
}
if(row == 0) {
sn = new String[1][7];
for(int j = 0; j <= 7; j++) {
sn[0][j] = " ";
}
}
else {
sn = new String[row][7];
rs.first();
rs.previous();
while(rs.next()) {
sn[i][0] = rs.getString("operatorId");
sn[i][1] = rs.getString("pId");
sn[i][2] = rs.getString("pName");
sn[i][3] = rs.getString("oldSalary");
sn[i][4] = rs.getString("newSalary");
sn[i][5] = rs.getString("modifyTime");
sn[i][6] = rs.getString("modifyDate");
i++;
}
}
}
catch(Exception ex) {
ex.printStackTrace();
}
finally{
db.closeStmt();
db.closeConn();
}
return sn;
}
}
总结:
虽然这个系统书上有代码,但是并没有直接上手写,在课上一直都是没着手写代码,而是看书上的功能描述,
首先还是分析系统的功能和结构,然后看表的设计,然后思考功能的实现。做什么都得自己先理好思路才能
更好、更快、更正确地写出来。对书上代码做了一些改进。