基于javaswing和mysql实现的员工工资管理系统

工资管理系统EPMS

这个项目是基于javaswing和mysql数据库所开发的

java编译器是eclipse,版本是2020-12-R

mysql管理工具是navicat,版本是8.1

1.功能模块设计

1.1java语言连接数据库jdbc

我们要通过java来操作数据库,首先就要把他们连接起来

代码实现逻辑:

四参五步:我们需要四个参数和五个步骤来成功连接数据库,在这之前,我们需要创建一个lib文件夹,然后再导入我们所需要的mysql-connector的jar包

四参:

  1. 加载jdbc连接mysql的驱动driver
  2. 连接mysql数据库的地址url
  3. 连接msyql的用户名user
  4. 连接mysql的密码password

五步:

  1. 加载jdbc的驱动
  2. 连接mysql的连接对象
  3. 预编译sql执行
  4. 执行sql语句
  5. 关闭数据库

代码如下:

package com.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBConnection {
	//1.加载jdbc连接mysql的驱动
	public final static String driver = "com.mysql.cj.jdbc.Driver";
	
	//2.连接mysql数据库的地址
	public final static String url = "jdbc:mysql://localhost:3306/wages";
	
	//3.连接mysql的用户名
	public final static String user = "root";
	
	//4.连接mysql的密码
	public final static String pwd = "txk0x7d2";
	
	//static静态代码块加载jdbc的驱动
	static {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//连接mysql的连接对象
	public static Connection getConn() {
		try {
			return DriverManager.getConnection(url, user, pwd);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	//关闭连接,保证mysql资源的释放,能够充分使用资源
	public static void close(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的使用
	public static void main(String[] args) {
		System.out.println(getConn()+"数据库连接成功!");
	}

}

1.2系统登录Login

系统登录界面如图所示:

在这里插入图片描述

代码实现逻辑:

  • 首先我们先获取用户输入在账号和密码文本框中的字符串,然后将其拿到我们所连接的数据库里面账号表中,在该表中进行查询相应的账号和密码并拿出来与用户所输入的对比,如果相等则登录成功,反之提示“用户名或密码错误!”;

  • 同时我们要选择ComboBox下拉选择框里面所设置的值,例如管理员登录,则通过这个来区分开不同类型的用户登录后所能使用的功能。管理员能够使用所有功能,而员工则会隐藏部分功能,比如对工资表的操作等等。

代码实现:

package com.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.dao.AccountDAO;
import com.entity.Account;
import com.entity.UserType;
import com.util.StringUtil;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Login extends JFrame {

	private JPanel contentPane;
	private JTextField userNameTextField;
	private JComboBox userTypeComboBox;
	private JTextField passwordField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login frame = new Login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {
		setResizable(false);
		setTitle("\u767B\u5F55");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 330, 311);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u8D26\u53F7\uFF1A");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 18));
		lblNewLabel.setBounds(26, 51, 70, 25);
		contentPane.add(lblNewLabel);
		
		userNameTextField = new JTextField();
		userNameTextField.setBounds(85, 53, 150, 24);
		contentPane.add(userNameTextField);
		userNameTextField.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801\uFF1A");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 18));
		lblNewLabel_1.setBounds(26, 104, 70, 25);
		contentPane.add(lblNewLabel_1);
		
		userTypeComboBox = new JComboBox();
		userTypeComboBox.setModel(new DefaultComboBoxModel(new UserType[] {UserType.ADMIN, UserType.EMPLOYEE}));
		userTypeComboBox.setBounds(122, 157, 113, 23);
		contentPane.add(userTypeComboBox);
		
		JButton loginButton = new JButton("\u767B\u5F55");
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				//登录按钮绑定事件
				loginAct(ae);
			}
		});
		loginButton.setBounds(26, 198, 93, 23);
		contentPane.add(loginButton);
		
		JButton disposeButton = new JButton("\u9000\u51FA");
		disposeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//退出
				dispose();
			}
		});
		disposeButton.setBounds(214, 238, 93, 23);
		contentPane.add(disposeButton);
		
		JLabel lblNewLabel_2 = new JLabel("\u5DE5\u8D44\u7BA1\u7406\u7CFB\u7EDF\u767B\u5F55");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 24));
		lblNewLabel_2.setBounds(61, 10, 206, 31);
		contentPane.add(lblNewLabel_2);
		
		JButton registerButton = new JButton("\u6CE8\u518C");
		registerButton.setBounds(161, 198, 93, 23);
		contentPane.add(registerButton);
		
		passwordField = new JTextField();
		passwordField.setColumns(10);
		passwordField.setBounds(85, 107, 150, 24);
		contentPane.add(passwordField);
	}

	protected void loginAct(ActionEvent ae) {
		// TODO Auto-generated method stub
		String 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 = new AccountDAO();
			Account accountTmp = new Account();
			accountTmp.setZhanghao(userName);
			accountTmp.setPassword(password);
			Account account = accountDAO.login(accountTmp);
			if (account == null) {
				JOptionPane.showMessageDialog(this, "用户名或密码错误!");
				return;
			} else {
				JOptionPane.showMessageDialog(this, "登录成功!");
				AdminSystem frame = new AdminSystem();
				frame.setVisible(true);
				dispose();
			}
		} else {
			//员工登录
		}
	}
}

1.3实体类设计entity

我们要通过java来操作数据库中表里面的数据,那么数据库中的每一张表就对应着java里面的一个实体类,所以我们在这里创建账号表account所对应的实体类Account

代码实现逻辑:

  • 首先我们要和表中的数据保持一致,同样的在实体类中声明相对应的数据和类型

  • 然后我们选中所有声明的数据,来generate他们的getter and setter方法,以便于后面得到值和设置值

代码如下:

package com.entity;

public class Account {
	private int id;
	private String zhanghao;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getZhanghao() {
		return zhanghao;
	}
	public void setZhanghao(String zhanghao) {
		this.zhanghao = zhanghao;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

}

1.4管理员系统AdminSystem

通过管理员选项登录成功后我们会创建一个管理员系统的界面,如图所示

在这里,我们先介绍员工工资管理,点进去后我们会跳转到员工工资管理界面

代码实现逻辑:

这一个主要是ui的设计,而我是通过javaswing来设计的,用eclipse里面扩展的功能WindowBuilder来图形化创建界面,最后给按钮绑定点击事件即可。

代码实现如下:

package com.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AdminSystem extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminSystem frame = new AdminSystem();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AdminSystem() {
		setResizable(false);
		setTitle("\u7BA1\u7406\u5458\u7CFB\u7EDF");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 323, 207);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("\u5458\u5DE5\u4FE1\u606F\u7BA1\u7406");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnNewButton.setBounds(10, 26, 121, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u5458\u5DE5\u5DE5\u8D44\u7BA1\u7406");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AdminWages frame = new AdminWages();
				frame.setVisible(true);
			}
		});
		btnNewButton_1.setBounds(157, 26, 121, 23);
		contentPane.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("\u5458\u5DE5\u8003\u52E4\u7BA1\u7406");
		btnNewButton_2.setBounds(10, 76, 121, 23);
		contentPane.add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("\u5458\u5DE5\u6D25\u8D34\u7BA1\u7406");
		btnNewButton_3.setBounds(157, 76, 121, 23);
		contentPane.add(btnNewButton_3);
		
		JButton btnNewButton_3_1 = new JButton("\u9000\u51FA\u7CFB\u7EDF");
		btnNewButton_3_1.addActionListener(new ActionListener() {
			public void actionPerformed(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里面去,最后关闭数据库

	//增加功能
	public boolean add(Wages wages) {
		
		//1.连接mysql
		Connection 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();
		}
		return false;
	}

2.2员工工资修改功能

和添加功能差不多,只是sql语句不一样

//修改功能
	public boolean update(Wages wages) {
		
		//1.连接mysql
		Connection 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();
		}
		return false;
	}

2.3员工工资删除功能

  • 和添加功能差不多,也是sql语句不一样。
  • 但是,因为后面实际使用的时候,我们删除是通过鼠标选中展示数据中所在的行来确定所要删除的数据,所以我们传入一个变量val
//删除功能
	public boolean delete(String val) {
		
		//1.连接mysql
		Connection 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();
		}
		return false;
	}

2.4员工工资查询单个员工工资功能

大部分步骤和添加功能差不多,

区别在于:

  1. 查询和增删改不同,是获取数据但是不修改数据,所以是query,而其他三个都会修改数据,所以统称为update
  2. 因为查询到的单条数据有多个数据项,所以要创建一个结果集ResultSet来存放这些数据,同时要让next()游标指向下一条数据才能接着查询所需要的数据项
  3. sql语句不同
//查询指定员工
		public Wages query(int code) {
			
			//1.连接mysql
			Connection 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 = new Wages();
				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();
			}
			return null;
		}

2.5员工工资查询全部员工工资功能

查询全部列和查询指定列大部分一样

区别在于:

  1. keyword用于模糊查询,因为后面使用时如果keyword为空则全部查询,如果keyword不为空则会被拼接到查询全部的sql语句中作为筛选条件,没错,这两个功能结合起来使用了
  2. 这里用Vector来创建一个二维列表,因为后面所使用时展示的就是二维列表
//查询所有员工工资信息 keyword用于搜索模糊查询
	public Vector<Vector<String>> getAll(String keyword) {
		//1.连接mysql
		Connection 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 = new Vector<Vector<String>>();
			
			while (rs.next()) {
				Vector<String> w = new Vector<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();
		}
		return null;
	}

3.员工工资管理AdminWages的界面设计

这是我们AdminWages的界面图:

在这里插入图片描述

3.1员工工资查询功能

首先界面的UI设计是通过WindowBuilder直接图形化创建的,我们只是给这个“查询”按钮添加一个点击事件

代码实现逻辑:

  1. String一个keyword,如果有就拼接到sql查询语句中当where的条件,如果没有就查询全部
  2. 创建一个二维列表list,把查询到的数据放在里面展示给我们
JButton btnNewButton = new JButton("\u67E5\u8BE2");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//查询员工工资信息,如果有关键字就模糊搜索,反之查询全部
				String keyword = textField.getText(); 
				WagesDAO w = new WagesDAO();
				Vector<Vector<String>> list = w.getAll(keyword);
				
				Vector<String> head =  new Vector<String>();
				head.add("员工编号");
				head.add("基本工资");
				head.add("岗位工资");
				head.add("水电费");
				head.add("津贴费");
				head.add("扣除工资");
				head.add("实发金额");
				
				//3.设置值
				table = new JTable(list,head);
				scrollPane.setViewportView(table);
				
			}
		});
		btnNewButton.setBounds(196, 29, 93, 23);
		contentPane.add(btnNewButton);

3.2员工工资添加功能

界面如图所示:

在这里插入图片描述

代码实现逻辑:

  1. 我们要把用户输入到文本框的数据得到然后通过sql语句插入到数据库的表中
  2. 因为int和double类型在java里面不能直接强制转换,所以我们就新建一个类似的字符串变量,再进行相应的转换
//AdminInsertWages
package com.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.dao.WagesDAO;
import com.entity.Wages;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AdminInsertWages extends JFrame {

	private JPanel contentPane;
	private JTextField code;
	private JTextField baseWages;
	private JTextField postWages;
	private JTextField money;
	private JTextField subsidy;
	private JTextField deduction;
	private JTextField fact;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminInsertWages frame = new AdminInsertWages();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AdminInsertWages() {
		setTitle("\u6DFB\u52A0\u5458\u5DE5\u5DE5\u8D44\u4FE1\u606F");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 305, 418);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u5458\u5DE5\u7F16\u53F7\uFF1A");
		lblNewLabel.setBounds(28, 27, 90, 27);
		contentPane.add(lblNewLabel);
		
		JLabel 基本工资 = new JLabel("\u57FA\u672C\u5DE5\u8D44\uFF1A");
		基本工资.setBounds(28, 64, 90, 27);
		contentPane.add(基本工资);
		
		JLabel 岗位工资 = new JLabel("\u5C97\u4F4D\u5DE5\u8D44\uFF1A");
		岗位工资.setBounds(28, 109, 90, 27);
		contentPane.add(岗位工资);
		
		JLabel lblNewLabel_3 = new JLabel("\u6C34\u7535\u8D39\uFF1A");
		lblNewLabel_3.setBounds(28, 154, 90, 27);
		contentPane.add(lblNewLabel_3);
		
		JLabel lblNewLabel_4 = new JLabel("\u6D25\u8D34\u5DE5\u8D44\uFF1A");
		lblNewLabel_4.setBounds(28, 197, 90, 27);
		contentPane.add(lblNewLabel_4);
		
		JLabel lblNewLabel_5 = new JLabel("\u6263\u9664\u85AA\u916C\uFF1A");
		lblNewLabel_5.setBounds(28, 244, 90, 27);
		contentPane.add(lblNewLabel_5);
		
		JLabel lblNewLabel_6 = new JLabel("\u5B9E\u53D1\u5DE5\u8D44\uFF1A");
		lblNewLabel_6.setBounds(28, 288, 90, 27);
		contentPane.add(lblNewLabel_6);
		
		code = new JTextField();
		code.setBounds(107, 30, 108, 21);
		contentPane.add(code);
		code.setColumns(10);
		
		baseWages = new JTextField();
		baseWages.setColumns(10);
		baseWages.setBounds(107, 67, 108, 21);
		contentPane.add(baseWages);
		
		postWages = new JTextField();
		postWages.setColumns(10);
		postWages.setBounds(107, 112, 108, 21);
		contentPane.add(postWages);
		
		money = new JTextField();
		money.setColumns(10);
		money.setBounds(107, 157, 108, 21);
		contentPane.add(money);
		
		subsidy = new JTextField();
		subsidy.setColumns(10);
		subsidy.setBounds(107, 200, 108, 21);
		contentPane.add(subsidy);
		
		deduction = new JTextField();
		deduction.setColumns(10);
		deduction.setBounds(107, 247, 108, 21);
		contentPane.add(deduction);
		
		fact = new JTextField();
		fact.setColumns(10);
		fact.setBounds(107, 291, 108, 21);
		contentPane.add(fact);
		
		JButton btnNewButton = new JButton("\u6DFB\u52A0");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//添加信息
				Wages wages = new Wages();
//				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 = new WagesDAO();
				if(w.add(wages)){
                    JOptionPane.showMessageDialog(null, "添加成功!");
                    
                }else {
                    JOptionPane.showMessageDialog(null, "添加失败!");
                }
			}
		});
		btnNewButton.setBounds(25, 346, 93, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u9000\u51FA");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//退出
				dispose();
			}
		});
		btnNewButton_1.setBounds(160, 346, 93, 23);
		contentPane.add(btnNewButton_1);
	}
}

//AdminWages
JButton btnNewButton_1 = new JButton("\u6DFB\u52A0\u4FE1\u606F");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//添加信息
				AdminInsertWages frame = new AdminInsertWages();
				frame.setVisible(true);
			}
		});
		btnNewButton_1.setBounds(314, 29, 93, 23);
		contentPane.add(btnNewButton_1);

3.3员工工资修改功能

界面如图所示:

在这里插入图片描述

代码实现逻辑:

  1. 首先在展示数据表中选中一行
  2. 将选中的那一行的数据得到然后再设置到修改信息界面相应的文本框中
  3. 直接对着原有的数据进行修改
//AdminUpdateWages
package com.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.dao.WagesDAO;
import com.entity.Wages;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AdminUpdateWages extends JFrame {

	private JPanel contentPane;
	private JTextField code;
	private JTextField baseWages;
	private JTextField postWages;
	private JTextField money;
	private JTextField subsidy;
	private JTextField deduction;
	private JTextField fact;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminUpdateWages frame = new AdminUpdateWages(5);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AdminUpdateWages(int id) {
		//查询工资信息回来
		WagesDAO w = new WagesDAO();
		Wages wages = w.query(id);
		
		setTitle("修改信息");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 305, 418);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u5458\u5DE5\u7F16\u53F7\uFF1A");
		lblNewLabel.setBounds(28, 27, 90, 27);
		contentPane.add(lblNewLabel);
		
		JLabel 基本工资 = new JLabel("\u57FA\u672C\u5DE5\u8D44\uFF1A");
		基本工资.setBounds(28, 64, 90, 27);
		contentPane.add(基本工资);
		
		JLabel 岗位工资 = new JLabel("\u5C97\u4F4D\u5DE5\u8D44\uFF1A");
		岗位工资.setBounds(28, 109, 90, 27);
		contentPane.add(岗位工资);
		
		JLabel lblNewLabel_3 = new JLabel("\u6C34\u7535\u8D39\uFF1A");
		lblNewLabel_3.setBounds(28, 154, 90, 27);
		contentPane.add(lblNewLabel_3);
		
		JLabel lblNewLabel_4 = new JLabel("\u6D25\u8D34\u5DE5\u8D44\uFF1A");
		lblNewLabel_4.setBounds(28, 197, 90, 27);
		contentPane.add(lblNewLabel_4);
		
		JLabel lblNewLabel_5 = new JLabel("\u6263\u9664\u85AA\u916C\uFF1A");
		lblNewLabel_5.setBounds(28, 244, 90, 27);
		contentPane.add(lblNewLabel_5);
		
		JLabel lblNewLabel_6 = new JLabel("\u5B9E\u53D1\u5DE5\u8D44\uFF1A");
		lblNewLabel_6.setBounds(28, 288, 90, 27);
		contentPane.add(lblNewLabel_6);
		
		//员工编号
		//除了String,都要类型转换
		String codes = String.valueOf(wages.getCode());
		code = new JTextField(codes);
		code.setBounds(107, 30, 108, 21);
		contentPane.add(code);
		code.setColumns(10);
		
		//基本工资
		String baseWagess = String.valueOf(wages.getBaseWages());
		baseWages = new JTextField(baseWagess);
		baseWages.setColumns(10);
		baseWages.setBounds(107, 67, 108, 21);
		contentPane.add(baseWages);
		
		//岗位工资
		String postWagess = String.valueOf(wages.getPostWages());
		postWages = new JTextField(postWagess);
		postWages.setColumns(10);
		postWages.setBounds(107, 112, 108, 21);
		contentPane.add(postWages);
		
		//水电费
		String moneys = String.valueOf(wages.getMoney());
		money = new JTextField(moneys);
		money.setColumns(10);
		money.setBounds(107, 157, 108, 21);
		contentPane.add(money);
		
		//津贴工资
		String subsidys = String.valueOf(wages.getSubsidy());
		subsidy = new JTextField(subsidys);
		subsidy.setColumns(10);
		subsidy.setBounds(107, 200, 108, 21);
		contentPane.add(subsidy);
		
		//扣除薪酬
		String deductions = String.valueOf(wages.getDeduction());
		deduction = new JTextField(deductions);
		deduction.setColumns(10);
		deduction.setBounds(107, 247, 108, 21);
		contentPane.add(deduction);
		
		//实发金额
		String facts = String.valueOf(wages.getFact());
		fact = new JTextField(facts);
		fact.setColumns(10);
		fact.setBounds(107, 291, 108, 21);
		contentPane.add(fact);
		
		JButton btnNewButton = new JButton("修改信息");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//修改信息
				Wages wages = new Wages();
				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 = new WagesDAO();
				if(w.update(wages)){
                    JOptionPane.showMessageDialog(null, "修改成功!");
                    
                }else {
                    JOptionPane.showMessageDialog(null, "修改失败!");
                }
			}
		});
		btnNewButton.setBounds(25, 346, 93, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u9000\u51FA");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//退出
				dispose();
			}
		});
		btnNewButton_1.setBounds(160, 346, 93, 23);
		contentPane.add(btnNewButton_1);
	}
}

//AdminWages
JButton btnNewButton_2 = new JButton("\u4FEE\u6539\u4FE1\u606F");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//修改信息
				 int rows = table.getSelectedRow();
	                if(rows == -1) { //没有选中行时得到的是-1
	                    JOptionPane.showMessageDialog(null, "请选择行!");
	                    return;
	                }
	                //根据行和列,获取对应的数字
	                int val = Integer.valueOf((String)table.getValueAt(rows, 0));
	                System.out.println(table.getValueAt(rows, 0));
	                AdminUpdateWages frame = new AdminUpdateWages(val);
					frame.setVisible(true);
			}
		});
		btnNewButton_2.setBounds(423, 29, 93, 23);
		contentPane.add(btnNewButton_2);

3.4员工工资删除功能

代码实现逻辑:

  1. 首先在展示数据表中选中一行,得到该行的行号
  2. 然后删除对应行号的数据
  3. 删除数据后更新展示的二维列表
JButton btnNewButton_3 = new JButton("\u5220\u9664\u4FE1\u606F");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//删除功能
				int rows = table.getSelectedRow();
				if (rows == -1) {
					JOptionPane.showMessageDialog(null, "请选择行");
					return;
				}
				//根据行和列,获取对应的数字
				String val = (String) table.getValueAt(rows, 0);
				//去WagesDAO中执行删除
				WagesDAO w = new WagesDAO();
				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工资表格更新功能

代码实现逻辑:

  1. 创建一个二维列表list
  2. 设置表头
  3. 设置值
//表格更新
	public void updateAll(String keyword) {
		//表格

		WagesDAO w = new WagesDAO();
		Vector<Vector<String>> list = w.getAll(null);
		
		//2.设置表头
		Vector<String> head =  new Vector<String>();
		head.add("员工编号");
		head.add("基本工资");
		head.add("岗位工资");
		head.add("水电费");
		head.add("津贴费");
		head.add("扣除工资");
		head.add("实发金额");
		
		//3.设置值
		table = new JTable(list,head);
		scrollPane.setViewportView(table);
		
	}

4.数据库设计wages

4.1账号表设计account

账号表设计三个值,id、账号和密码,其中id为自增

数据如图所示:

在这里插入图片描述

创建sql代码:

CREATE TABLE account  (

  id int PRIMARY key NOT NULL,

  zhanghao varchar(20) not NULL,

  password varchar(20) not NULL
)

4.2工资表设计wages

工资表创建设计七个值,员工编号、基本工资、岗位工资、水电费、津贴、扣除薪酬和实发金额

数据如图所示:

在这里插入图片描述

创建sql代码:

CREATE TABLE wages  (

  code int PRIMARY key NOT NULL,

  baseWages int not NULL,

  postWages int not NULL,

  money float(10, 0) not NULL,

  subsidy int not NULL,

  deduction int not NULL,
  
  fact int not NULL
)

5.EPMS项目搭建

5.1项目搭建如图所示

在这里插入图片描述

5.2项目搭建每个package的作用和相互之间的联系

  1. dao层是各个实体类的操作方法如增删改查等
  2. entity是实体类,每一个实体类对应着数据库中的一张表
  3. test是用来测试的一个包,里面有着对各种方法的测试看其是否能够正常运行
  4. ui是写界面的一个包,有着各个界面的UI设计
  5. util里面是我们自己封装的工具类或者导入的工具类,就是我们提前写好的方法
  6. lib是存放我们依赖文件的一个包,比如mysql-connecttor的jar包
  • 26
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值