项目上传到了github,下载EPMS文件夹即可,源码都在里面,记得给我一个star
https://github.com/XK-coding/projects.git
工资管理系统EPMS目录
工资管理系统EPMS
这个项目是基于javaswing和mysql数据库所开发的
java编译器是eclipse,版本是2020-12-R
mysql管理工具是navicat,版本是8.1
1.功能模块设计
1.1java语言连接数据库jdbc
我们要通过java来操作数据库,首先就要把他们连接起来
代码实现逻辑:
四参五步:我们需要四个参数和五个步骤来成功连接数据库,在这之前,我们需要创建一个lib文件夹,然后再导入我们所需要的mysql-connector的jar包
四参:
- 加载jdbc连接mysql的驱动driver
- 连接mysql数据库的地址url
- 连接msyql的用户名user
- 连接mysql的密码password
五步:
- 加载jdbc的驱动
- 连接mysql的连接对象
- 预编译sql执行
- 执行sql语句
- 关闭数据库
代码如下:
packagecom.jdbc;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;publicclassDBConnection{//1.加载jdbc连接mysql的驱动publicfinalstaticString driver ="com.mysql.cj.jdbc.Driver";//2.连接mysql数据库的地址publicfinalstaticString url ="jdbc:mysql://localhost:3306/wages";//3.连接mysql的用户名publicfinalstaticString user ="root";//4.连接mysql的密码publicfinalstaticString pwd ="txk0x7d2";//static静态代码块加载jdbc的驱动static{try{Class.forName(driver);}catch(ClassNotFoundException e){// TODO Auto-generated catch block
e.printStackTrace();}}//连接mysql的连接对象publicstaticConnectiongetConn(){try{returnDriverManager.getConnection(url, user, pwd);}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}returnnull;}//关闭连接,保证mysql资源的释放,能够充分使用资源publicstaticvoidclose(ResultSet rs,PreparedStatement ps,Connection conn){try{if(rs !=null){
rs.close();}if(ps !=null){
ps.close();}if(conn !=null){
conn.close();}}catch(Exception e){// TODO Auto-generated catch block
e.printStackTrace();}}//验证jdbc的使用publicstaticvoidmain(String[] args){System.out.println(getConn()+"数据库连接成功!");}}
1.2系统登录Login
系统登录界面如图所示:
代码实现逻辑:
- 首先我们先获取用户输入在账号和密码文本框中的字符串,然后将其拿到我们所连接的数据库里面账号表中,在该表中进行查询相应的账号和密码并拿出来与用户所输入的对比,如果相等则登录成功,反之提示“用户名或密码错误!”;
- 同时我们要选择ComboBox下拉选择框里面所设置的值,例如管理员登录,则通过这个来区分开不同类型的用户登录后所能使用的功能。管理员能够使用所有功能,而员工则会隐藏部分功能,比如对工资表的操作等等。
代码实现:
packagecom.ui;importjava.awt.EventQueue;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.border.EmptyBorder;importcom.dao.AccountDAO;importcom.entity.Account;importcom.entity.UserType;importcom.util.StringUtil;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjava.awt.Font;importjavax.swing.JTextField;importjavax.swing.JPasswordField;importjavax.swing.JComboBox;importjavax.swing.DefaultComboBoxModel;importjavax.swing.JButton;importjava.awt.event.ActionListener;importjava.awt.event.ActionEvent;publicclassLoginextendsJFrame{privateJPanel contentPane;privateJTextField userNameTextField;privateJComboBox userTypeComboBox;privateJTextField passwordField;/**
* Launch the application.
*/publicstaticvoidmain(String[] args){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{Login frame =newLogin();
frame.setVisible(true);}catch(Exception e){
e.printStackTrace();}}});}/**
* Create the frame.
*/publicLogin(){setResizable(false);setTitle("\u767B\u5F55");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,330,311);
contentPane =newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));setContentPane(contentPane);
contentPane.setLayout(null);JLabel lblNewLabel =newJLabel("\u8D26\u53F7\uFF1A");
lblNewLabel.setFont(newFont("宋体",Font.PLAIN,18));
lblNewLabel.setBounds(26,51,70,25);
contentPane.add(lblNewLabel);
userNameTextField =newJTextField();
userNameTextField.setBounds(85,53,150,24);
contentPane.add(userNameTextField);
userNameTextField.setColumns(10);JLabel lblNewLabel_1 =newJLabel("\u5BC6\u7801\uFF1A");
lblNewLabel_1.setFont(newFont("宋体",Font.PLAIN,18));
lblNewLabel_1.setBounds(26,104,70,25);
contentPane.add(lblNewLabel_1);
userTypeComboBox =newJComboBox();
userTypeComboBox.setModel(newDefaultComboBoxModel(newUserType[]{UserType.ADMIN,UserType.EMPLOYEE}));
userTypeComboBox.setBounds(122,157,113,23);
contentPane.add(userTypeComboBox);JButton loginButton =newJButton("\u767B\u5F55");
loginButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent ae){//登录按钮绑定事件loginAct(ae);}});
loginButton.setBounds(26,198,93,23);
contentPane.add(loginButton);JButton disposeButton =newJButton("\u9000\u51FA");
disposeButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){//退出dispose();}});
disposeButton.setBounds(214,238,93,23);
contentPane.add(disposeButton);JLabel lblNewLabel_2 =newJLabel("\u5DE5\u8D44\u7BA1\u7406\u7CFB\u7EDF\u767B\u5F55");
lblNewLabel_2.setFont(newFont("宋体",Font.PLAIN,24));
lblNewLabel_2.setBounds(61,10,206,31);
contentPane.add(lblNewLabel_2);JButton registerButton =newJButton("\u6CE8\u518C");
registerButton.setBounds(161,198,93,23);
contentPane.add(registerButton);
passwordField =newJTextField();
passwordField.setColumns(10);
passwordField.setBounds(85,107,150,24);
contentPane.add(passwordField);}protectedvoidloginAct(ActionEvent ae){// TODO Auto-generated method stubString userName = userNameTextField.getText().toString();String password = passwordField.getText().toString();UserType selectedItem =(UserType)userTypeComboBox.getSelectedItem();//判断用户名是否为空if(StringUtil.isEmpty(userName)){JOptionPane.showMessageDialog(this,"用户名不能为空!");return;}//判断密码是否为空if(StringUtil.isEmpty(password)){JOptionPane.showMessageDialog(this,"密码不能为空!");return;}if("系统管理员".equals(selectedItem.getName())){//系统管理员登录AccountDAO accountDAO =newAccountDAO();Account accountTmp =newAccount();
accountTmp.setZhanghao(userName);
accountTmp.setPassword(password);Account account = accountDAO.login(accountTmp);if(account ==null){JOptionPane.showMessageDialog(this,"用户名或密码错误!");return;}else{JOptionPane.showMessageDialog(this,"登录成功!");AdminSystem frame =newAdminSystem();
frame.setVisible(true);dispose();}}else{//员工登录}}}
1.3实体类设计entity
我们要通过java来操作数据库中表里面的数据,那么数据库中的每一张表就对应着java里面的一个实体类,所以我们在这里创建账号表account所对应的实体类Account
代码实现逻辑:
- 首先我们要和表中的数据保持一致,同样的在实体类中声明相对应的数据和类型
- 然后我们选中所有声明的数据,来generate他们的getter and setter方法,以便于后面得到值和设置值
代码如下:
packagecom.entity;publicclassAccount{privateint id;privateString zhanghao;privateString password;publicintgetId(){return id;}publicvoidsetId(int id){this.id = id;}publicStringgetZhanghao(){return zhanghao;}publicvoidsetZhanghao(String zhanghao){this.zhanghao = zhanghao;}publicStringgetPassword(){return password;}publicvoidsetPassword(String password){this.password = password;}}
1.4管理员系统AdminSystem
通过管理员选项登录成功后我们会创建一个管理员系统的界面,如图所示
在这里,我们先介绍员工工资管理,点进去后我们会跳转到员工工资管理界面
代码实现逻辑:
这一个主要是ui的设计,而我是通过javaswing来设计的,用eclipse里面扩展的功能WindowBuilder来图形化创建界面,最后给按钮绑定点击事件即可。
代码实现如下:
packagecom.ui;importjava.awt.EventQueue;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.border.EmptyBorder;importjavax.swing.JButton;importjava.awt.event.ActionListener;importjava.awt.event.ActionEvent;publicclassAdminSystemextendsJFrame{privateJPanel contentPane;/**
* Launch the application.
*/publicstaticvoidmain(String[] args){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{AdminSystem frame =newAdminSystem();
frame.setVisible(true);}catch(Exception e){
e.printStackTrace();}}});}/**
* Create the frame.
*/publicAdminSystem(){setResizable(false);setTitle("\u7BA1\u7406\u5458\u7CFB\u7EDF");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,323,207);
contentPane =newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));setContentPane(contentPane);
contentPane.setLayout(null);JButton btnNewButton =newJButton("\u5458\u5DE5\u4FE1\u606F\u7BA1\u7406");
btnNewButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){}});
btnNewButton.setBounds(10,26,121,23);
contentPane.add(btnNewButton);JButton btnNewButton_1 =newJButton("\u5458\u5DE5\u5DE5\u8D44\u7BA1\u7406");
btnNewButton_1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){AdminWages frame =newAdminWages();
frame.setVisible(true);}});
btnNewButton_1.setBounds(157,26,121,23);
contentPane.add(btnNewButton_1);JButton btnNewButton_2 =newJButton("\u5458\u5DE5\u8003\u52E4\u7BA1\u7406");
btnNewButton_2.setBounds(10,76,121,23);
contentPane.add(btnNewButton_2);JButton btnNewButton_3 =newJButton("\u5458\u5DE5\u6D25\u8D34\u7BA1\u7406");
btnNewButton_3.setBounds(157,76,121,23);
contentPane.add(btnNewButton_3);JButton btnNewButton_3_1 =newJButton("\u9000\u51FA\u7CFB\u7EDF");
btnNewButton_3_1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){dispose();}});
btnNewButton_3_1.setBounds(100,124,95,29);
contentPane.add(btnNewButton_3_1);}}
2.员工工资管理AdminWages
员工工资管理功能则包含了对员工工资信息的增删改查,下面会分别介绍这几个功能
2.1员工工资添加功能
代码实现逻辑:
- 首先我们连接mysql,然后预编译sql执行,String 一个sql,把添加语句放在sql变量里面,“?”用来作为占位符
- 然后我们对应工资表wages里面的数据把用户输入的数据得到后设置到wages里面去,最后关闭数据库
//增加功能publicbooleanadd(Wages wages){//1.连接mysqlConnection conn =DBConnection.getConn();//2.预编译sql执行String sql ="insert into wages values(?,?,?,?,?,?,?)";try{PreparedStatement ps = conn.prepareCall(sql);
ps.setInt(1, wages.getCode());
ps.setInt(2, wages.getBaseWages());
ps.setInt(3, wages.getPostWages());
ps.setFloat(4,(float) wages.getMoney());
ps.setInt(5, wages.getSubsidy());
ps.setInt(6, wages.getDeduction());
ps.setFloat(7,(float) wages.getFact());//执行操作更改boolean result = ps.executeUpdate()>0;//关闭数据库DBConnection.close(null, ps, conn);return result;}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}returnfalse;}
2.2员工工资修改功能
和添加功能差不多,只是sql语句不一样
//修改功能publicbooleanupdate(Wages wages){//1.连接mysqlConnection conn =DBConnection.getConn();//2.预编译sql执行String sql ="update wages set baseWages=?,postWages=?,money=?,subsidy=?,deduction=?,fact=? where code=?";try{PreparedStatement ps = conn.prepareCall(sql);
ps.setInt(1, wages.getBaseWages());
ps.setInt(2, wages.getPostWages());
ps.setFloat(3,(float) wages.getMoney());
ps.setInt(4, wages.getSubsidy());
ps.setInt(5, wages.getDeduction());
ps.setFloat(6,(float) wages.getFact());
ps.setInt(7, wages.getCode());//执行操作更改boolean result = ps.executeUpdate()>0;//关闭数据库DBConnection.close(null, ps, conn);return result;}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}returnfalse;}
2.3员工工资删除功能
- 和添加功能差不多,也是sql语句不一样。
- 但是,因为后面实际使用的时候,我们删除是通过鼠标选中展示数据中所在的行来确定所要删除的数据,所以我们传入一个变量val
//删除功能publicbooleandelete(String val){//1.连接mysqlConnection conn =DBConnection.getConn();//2.预编译sql执行String sql ="delete from wages where code=?";try{PreparedStatement ps = conn.prepareCall(sql);
ps.setString(1,val);//执行操作更改boolean result = ps.executeUpdate()>0;//关闭数据库DBConnection.close(null, ps, conn);return result;}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}returnfalse;}
2.4员工工资查询单个员工工资功能
大部分步骤和添加功能差不多,
区别在于:
- 查询和增删改不同,是获取数据但是不修改数据,所以是query,而其他三个都会修改数据,所以统称为update
- 因为查询到的单条数据有多个数据项,所以要创建一个结果集ResultSet来存放这些数据,同时要让next()游标指向下一条数据才能接着查询所需要的数据项
- sql语句不同
//查询指定员工publicWagesquery(int code){//1.连接mysqlConnection conn =DBConnection.getConn();//2.预编译sql执行String sql ="select * from wages where code=?";try{PreparedStatement ps = conn.prepareCall(sql);
ps.setInt(1,code);//执行操作更改ResultSet rs = ps.executeQuery();//创建一份工资对象返回Wages w =newWages();while(rs.next()){
w.setCode(rs.getInt(1));
w.setBaseWages(rs.getInt(2));
w.setPostWages(rs.getInt(3));
w.setMoney(rs.getDouble(4));
w.setSubsidy(rs.getInt(5));
w.setDeduction(rs.getInt(6));
w.setMoney(rs.getDouble(7));}//关闭数据库DBConnection.close(rs, ps, conn);return w;}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}returnnull;}
2.5员工工资查询全部员工工资功能
查询全部列和查询指定列大部分一样
区别在于:
- keyword用于模糊查询,因为后面使用时如果keyword为空则全部查询,如果keyword不为空则会被拼接到查询全部的sql语句中作为筛选条件,没错,这两个功能结合起来使用了
- 这里用Vector来创建一个二维列表,因为后面所使用时展示的就是二维列表
//查询所有员工工资信息 keyword用于搜索模糊查询publicVector<Vector<String>>getAll(String keyword){//1.连接mysqlConnection conn =DBConnection.getConn();//2.预编译sql执行String sql ="select * from wages";//如果有关键字,则把它拼接到查询语句当中作限制条件if(keyword !=null){
sql +=" where code like '%"+keyword+"%'";}try{PreparedStatement ps = conn.prepareStatement(sql);//执行操作更改ResultSet rs = ps.executeQuery();//创建一个二维数组返回Vector<Vector<String>> list =newVector<Vector<String>>();while(rs.next()){Vector<String> w =newVector<String>();
w.add(rs.getString(1));
w.add(rs.getString(2));
w.add(rs.getString(3));
w.add(rs.getString(4));
w.add(rs.getString(5));
w.add(rs.getString(6));
w.add(rs.getString(7));//把当前对象存储到list集合中
list.add(w);}//关闭数据库DBConnection.close(rs, ps, conn);return list;}catch(SQLException e){// TODO Auto-generated catch block
e.printStackTrace();}returnnull;}
3.员工工资管理AdminWages的界面设计
这是我们AdminWages的界面图:
3.1员工工资查询功能
首先界面的UI设计是通过WindowBuilder直接图形化创建的,我们只是给这个“查询”按钮添加一个点击事件
代码实现逻辑:
- String一个keyword,如果有就拼接到sql查询语句中当where的条件,如果没有就查询全部
- 创建一个二维列表list,把查询到的数据放在里面展示给我们
JButton btnNewButton =newJButton("\u67E5\u8BE2");
btnNewButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){//查询员工工资信息,如果有关键字就模糊搜索,反之查询全部String keyword = textField.getText();WagesDAO w =newWagesDAO();Vector<Vector<String>> list = w.getAll(keyword);Vector<String> head =newVector<String>();
head.add("员工编号");
head.add("基本工资");
head.add("岗位工资");
head.add("水电费");
head.add("津贴费");
head.add("扣除工资");
head.add("实发金额");//3.设置值
table =newJTable(list,head);
scrollPane.setViewportView(table);}});
btnNewButton.setBounds(196,29,93,23);
contentPane.add(btnNewButton);
3.2员工工资添加功能
界面如图所示:
代码实现逻辑:
- 我们要把用户输入到文本框的数据得到然后通过sql语句插入到数据库的表中
- 因为int和double类型在java里面不能直接强制转换,所以我们就新建一个类似的字符串变量,再进行相应的转换
//AdminInsertWagespackagecom.ui;importjava.awt.EventQueue;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.border.EmptyBorder;importcom.dao.WagesDAO;importcom.entity.Wages;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JTextField;importjavax.swing.JButton;importjava.awt.event.ActionListener;importjava.awt.event.ActionEvent;publicclassAdminInsertWagesextendsJFrame{privateJPanel contentPane;privateJTextField code;privateJTextField baseWages;privateJTextField postWages;privateJTextField money;privateJTextField subsidy;privateJTextField deduction;privateJTextField fact;/**
* Launch the application.
*/publicstaticvoidmain(String[] args){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{AdminInsertWages frame =newAdminInsertWages();
frame.setVisible(true);}catch(Exception e){
e.printStackTrace();}}});}/**
* Create the frame.
*/publicAdminInsertWages(){setTitle("\u6DFB\u52A0\u5458\u5DE5\u5DE5\u8D44\u4FE1\u606F");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,305,418);
contentPane =newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));setContentPane(contentPane);
contentPane.setLayout(null);JLabel lblNewLabel =newJLabel("\u5458\u5DE5\u7F16\u53F7\uFF1A");
lblNewLabel.setBounds(28,27,90,27);
contentPane.add(lblNewLabel);JLabel 基本工资 =newJLabel("\u57FA\u672C\u5DE5\u8D44\uFF1A");
基本工资.setBounds(28,64,90,27);
contentPane.add(基本工资);JLabel 岗位工资 =newJLabel("\u5C97\u4F4D\u5DE5\u8D44\uFF1A");
岗位工资.setBounds(28,109,90,27);
contentPane.add(岗位工资);JLabel lblNewLabel_3 =newJLabel("\u6C34\u7535\u8D39\uFF1A");
lblNewLabel_3.setBounds(28,154,90,27);
contentPane.add(lblNewLabel_3);JLabel lblNewLabel_4 =newJLabel("\u6D25\u8D34\u5DE5\u8D44\uFF1A");
lblNewLabel_4.setBounds(28,197,90,27);
contentPane.add(lblNewLabel_4);JLabel lblNewLabel_5 =newJLabel("\u6263\u9664\u85AA\u916C\uFF1A");
lblNewLabel_5.setBounds(28,244,90,27);
contentPane.add(lblNewLabel_5);JLabel lblNewLabel_6 =newJLabel("\u5B9E\u53D1\u5DE5\u8D44\uFF1A");
lblNewLabel_6.setBounds(28,288,90,27);
contentPane.add(lblNewLabel_6);
code =newJTextField();
code.setBounds(107,30,108,21);
contentPane.add(code);
code.setColumns(10);
baseWages =newJTextField();
baseWages.setColumns(10);
baseWages.setBounds(107,67,108,21);
contentPane.add(baseWages);
postWages =newJTextField();
postWages.setColumns(10);
postWages.setBounds(107,112,108,21);
contentPane.add(postWages);
money =newJTextField();
money.setColumns(10);
money.setBounds(107,157,108,21);
contentPane.add(money);
subsidy =newJTextField();
subsidy.setColumns(10);
subsidy.setBounds(107,200,108,21);
contentPane.add(subsidy);
deduction =newJTextField();
deduction.setColumns(10);
deduction.setBounds(107,247,108,21);
contentPane.add(deduction);
fact =newJTextField();
fact.setColumns(10);
fact.setBounds(107,291,108,21);
contentPane.add(fact);JButton btnNewButton =newJButton("\u6DFB\u52A0");
btnNewButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){//添加信息Wages wages =newWages();// WagesDAO w = new WagesDAO();//因为不能int直接转string所以要转换类型String codes = code.getText();int code =Integer.valueOf(codes);
wages.setCode(code);String baseWagess = baseWages.getText();int baseWages =Integer.valueOf(baseWagess);
wages.setBaseWages(baseWages);String postWagess = postWages.getText();int postWages =Integer.valueOf(postWagess);
wages.setPostWages(postWages);String moneys = money.getText();double money =Double.valueOf(moneys);
wages.setMoney(money);String subsidys = subsidy.getText();int subsidy =Integer.valueOf(subsidys);
wages.setSubsidy(subsidy);String deductions = deduction.getText();int deduction =Integer.valueOf(deductions);
wages.setDeduction(deduction);String facts = fact.getText();double fact =Double.valueOf(facts);
wages.setFact(fact);//执行添加WagesDAO w =newWagesDAO();if(w.add(wages)){JOptionPane.showMessageDialog(null,"添加成功!");}else{JOptionPane.showMessageDialog(null,"添加失败!");}}});
btnNewButton.setBounds(25,346,93,23);
contentPane.add(btnNewButton);JButton btnNewButton_1 =newJButton("\u9000\u51FA");
btnNewButton_1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){//退出dispose();}});
btnNewButton_1.setBounds(160,346,93,23);
contentPane.add(btnNewButton_1);}}//AdminWagesJButton btnNewButton_1 =newJButton("\u6DFB\u52A0\u4FE1\u606F");
btnNewButton_1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){//添加信息AdminInsertWages frame =newAdminInsertWages();
frame.setVisible(true);}});
btnNewButton_1.setBounds(314,29,93,23);
contentPane.add(btnNewButton_1);
3.3员工工资修改功能
界面如图所示:
代码实现逻辑:
- 首先在展示数据表中选中一行
- 将选中的那一行的数据得到然后再设置到修改信息界面相应的文本框中
- 直接对着原有的数据进行修改
//AdminUpdateWagespackagecom.ui;importjava.awt.EventQueue;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.border.EmptyBorder;importcom.dao.WagesDAO;importcom.entity.Wages;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JTextField;importjavax.swing.JButton;importjava.awt.event.ActionListener;importjava.awt.event.ActionEvent;publicclassAdminUpdateWagesextendsJFrame{privateJPanel contentPane;privateJTextField code;privateJTextField baseWages;privateJTextField postWages;privateJTextField money;privateJTextField subsidy;privateJTextField deduction;privateJTextField fact;/**
* Launch the application.
*/publicstaticvoidmain(String[] args){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{AdminUpdateWages frame =newAdminUpdateWages(5);
frame.setVisible(true);}catch(Exception e){
e.printStackTrace();}}});}/**
* Create the frame.
*/publicAdminUpdateWages(int id){//查询工资信息回来WagesDAO w =newWagesDAO();Wages wages = w.query(id);setTitle("修改信息");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,305,418);
contentPane =newJPanel();
contentPane.setBorder(newEmptyBorder(5,5,5,5));setContentPane(contentPane);
contentPane.setLayout(null);JLabel lblNewLabel =newJLabel("\u5458\u5DE5\u7F16\u53F7\uFF1A");
lblNewLabel.setBounds(28,27,90,27);
contentPane.add(lblNewLabel);JLabel 基本工资 =newJLabel("\u57FA\u672C\u5DE5\u8D44\uFF1A");
基本工资.setBounds(28,64,90,27);
contentPane.add(基本工资);JLabel 岗位工资 =newJLabel("\u5C97\u4F4D\u5DE5\u8D44\uFF1A");
岗位工资.setBounds(28,109,90,27);
contentPane.add(岗位工资);JLabel lblNewLabel_3 =newJLabel("\u6C34\u7535\u8D39\uFF1A");
lblNewLabel_3.setBounds(28,154,90,27);
contentPane.add(lblNewLabel_3);JLabel lblNewLabel_4 =newJLabel("\u6D25\u8D34\u5DE5\u8D44\uFF1A");
lblNewLabel_4.setBounds(28,197,90,27);
contentPane.add(lblNewLabel_4);JLabel lblNewLabel_5 =newJLabel("\u6263\u9664\u85AA\u916C\uFF1A");
lblNewLabel_5.setBounds(28,244,90,27);
contentPane.add(lblNewLabel_5);JLabel lblNewLabel_6 =newJLabel("\u5B9E\u53D1\u5DE5\u8D44\uFF1A");
lblNewLabel_6.setBounds(28,288,90,27);
contentPane.add(lblNewLabel_6);//员工编号//除了String,都要类型转换String codes =String.valueOf(wages.getCode());
code =newJTextField(codes);
code.setBounds(107,30,108,21);
contentPane.add(code);
code.setColumns(10);//基本工资String baseWagess =String.valueOf(wages.getBaseWages());
baseWages =newJTextField(baseWagess);
baseWages.setColumns(10);
baseWages.setBounds(107,67,108,21);
contentPane.add(baseWages);//岗位工资String postWagess =String.valueOf(wages.getPostWages());
postWages =newJTextField(postWagess);
postWages.setColumns(10);
postWages.setBounds(107,112,108,21);
contentPane.add(postWages);//水电费String moneys =String.valueOf(wages.getMoney());
money =newJTextField(moneys);
money.setColumns(10);
money.setBounds(107,157,108,21);
contentPane.add(money);//津贴工资String subsidys =String.valueOf(wages.getSubsidy());
subsidy =newJTextField(subsidys);
subsidy.setColumns(10);
subsidy.setBounds(107,200,108,21);
contentPane.add(subsidy);//扣除薪酬String deductions =String.valueOf(wages.getDeduction());
deduction =newJTextField(deductions);
deduction.setColumns(10);
deduction.setBounds(107,247,108,21);
contentPane.add(deduction);//实发金额String facts =String.valueOf(wages.getFact());
fact =newJTextField(facts);
fact.setColumns(10);
fact.setBounds(107,291,108,21);
contentPane.add(fact);JButton btnNewButton =newJButton("修改信息");
btnNewButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){//修改信息Wages wages =newWages();
wages.setCode(id);//修改时需要获得该修改人的ID//因为不能int直接转string所以要转换类型String codes = code.getText();int code =Integer.valueOf(codes);
wages.setCode(code);String baseWagess = baseWages.getText();int baseWages =Integer.valueOf(baseWagess);
wages.setBaseWages(baseWages);String postWagess = postWages.getText();int postWages =Integer.valueOf(postWagess);
wages.setPostWages(postWages);String moneys = money.getText();double money =Double.valueOf(moneys);
wages.setMoney(money);String subsidys = subsidy.getText();int subsidy =Integer.valueOf(subsidys);
wages.setSubsidy(subsidy);String deductions = deduction.getText();int deduction =Integer.valueOf(deductions);
wages.setDeduction(deduction);String facts = fact.getText();double fact =Double.valueOf(facts);
wages.setFact(fact);//执行修改WagesDAO w =newWagesDAO();if(w.update(wages)){JOptionPane.showMessageDialog(null,"修改成功!");}else{JOptionPane.showMessageDialog(null,"修改失败!");}}});
btnNewButton.setBounds(25,346,93,23);
contentPane.add(btnNewButton);JButton btnNewButton_1 =newJButton("\u9000\u51FA");
btnNewButton_1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){//退出dispose();}});
btnNewButton_1.setBounds(160,346,93,23);
contentPane.add(btnNewButton_1);}}//AdminWagesJButton btnNewButton_2 =newJButton("\u4FEE\u6539\u4FE1\u606F");
btnNewButton_2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){//修改信息int rows = table.getSelectedRow();if(rows ==-1){//没有选中行时得到的是-1JOptionPane.showMessageDialog(null,"请选择行!");return;}//根据行和列,获取对应的数字int val =Integer.valueOf((String)table.getValueAt(rows,0));System.out.println(table.getValueAt(rows,0));AdminUpdateWages frame =newAdminUpdateWages(val);
frame.setVisible(true);}});
btnNewButton_2.setBounds(423,29,93,23);
contentPane.add(btnNewButton_2);
3.4员工工资删除功能
代码实现逻辑:
- 首先在展示数据表中选中一行,得到该行的行号
- 然后删除对应行号的数据
- 删除数据后更新展示的二维列表
JButton btnNewButton_3 =newJButton("\u5220\u9664\u4FE1\u606F");
btnNewButton_3.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent e){//删除功能int rows = table.getSelectedRow();if(rows ==-1){JOptionPane.showMessageDialog(null,"请选择行");return;}//根据行和列,获取对应的数字String val =(String) table.getValueAt(rows,0);//去WagesDAO中执行删除WagesDAO w =newWagesDAO();boolean result = w.delete(val);if(result){JOptionPane.showMessageDialog(null,"删除成功!");updateAll(null);}else{JOptionPane.showMessageDialog(null,"删除失败!");}}});
btnNewButton_3.setBounds(533,29,93,23);
contentPane.add(btnNewButton_3);
3.5工资表格更新功能
代码实现逻辑:
- 创建一个二维列表list
- 设置表头
- 设置值
//表格更新publicvoidupdateAll(String keyword){//表格WagesDAO w =newWagesDAO();Vector<Vector<String>> list = w.getAll(null);//2.设置表头Vector<String> head =newVector<String>();
head.add("员工编号");
head.add("基本工资");
head.add("岗位工资");
head.add("水电费");
head.add("津贴费");
head.add("扣除工资");
head.add("实发金额");//3.设置值
table =newJTable(list,head);
scrollPane.setViewportView(table);}
4.数据库设计wages
4.1账号表设计account
账号表设计三个值,id、账号和密码,其中id为自增
数据如图所示:
创建sql代码:
CREATETABLE account (
id intPRIMARYkeyNOTNULL,
zhanghao varchar(20)notNULL,
password varchar(20)notNULL)
4.2工资表设计wages
工资表创建设计七个值,员工编号、基本工资、岗位工资、水电费、津贴、扣除薪酬和实发金额
数据如图所示:
创建sql代码:
CREATETABLE wages (
code intPRIMARYkeyNOTNULL,
baseWages intnotNULL,
postWages intnotNULL,
money float(10,0)notNULL,
subsidy intnotNULL,
deduction intnotNULL,
fact intnotNULL)
5.EPMS项目搭建
5.1项目搭建如图所示
5.2项目搭建每个package的作用和相互之间的联系
- dao层是各个实体类的操作方法如增删改查等
- entity是实体类,每一个实体类对应着数据库中的一张表
- test是用来测试的一个包,里面有着对各种方法的测试看其是否能够正常运行
- ui是写界面的一个包,有着各个界面的UI设计
- util里面是我们自己封装的工具类或者导入的工具类,就是我们提前写好的方法
- lib是存放我们依赖文件的一个包,比如mysql-connecttor的jar包
版权归原作者 Muko_0x7d2 所有, 如有侵权,请联系我们删除。