0


Java Swing大作业--简易员工管理系统

@Java Swing简易员工管理系统

系统功能模块图

系统包括管理员登录页面和主页面设计,在主界面包括员工信息的增删改查功能。
在这里插入图片描述

系统实现环境说明

开发环境:eclipse 2022-03
Jdk版本:11
数据库:MySQL

数据库实现代码

1.创建数据库employees。

CREATEDATABASE employees;USE employees;

2.创建管理员表admin并增加数据。

DROPTABLEIFEXISTS`admin`;CREATETABLE`admin`(`ano`int(0)NOTNULLCOMMENT'管理员号',`aname`varchar(10)CHARACTERSET utf8 COLLATE utf8_general_ci NOTNULLCOMMENT'账号',`password`varchar(10)CHARACTERSET utf8 COLLATE utf8_general_ci NOTNULLCOMMENT'密码',PRIMARYKEY(`ano`)USINGBTREE)ENGINE=InnoDBCHARACTERSET= utf8 COLLATE= utf8_general_ci ROW_FORMAT = Dynamic;INSERTINTO`admin`VALUES(1,'admin','admin');

3.创建员工信息表emp并增加数据。

DROPTABLEIFEXISTS`emp`;CREATETABLE`emp`(`yno`int(0)NOTNULLAUTO_INCREMENTCOMMENT'员工号',`yname`varchar(10)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'员工姓名',`sex`varchar(4)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'员工性别',`birth`dateNULLDEFAULTNULLCOMMENT'出生日期',`home`varchar(10)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'籍贯',`degree`varchar(10)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'学历',`salary`double(10,2)NULLDEFAULTNULLCOMMENT'薪水',`info`varchar(255)CHARACTERSET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULLDEFAULTNULLCOMMENT'备注',PRIMARYKEY(`yno`)USINGBTREE)ENGINE=InnoDBAUTO_INCREMENT=9CHARACTERSET= utf8mb4 COLLATE= utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;INSERTINTO`emp`VALUES(2,'王二','男','2000-10-11','湖南长沙','初中',2300.00,'');INSERTINTO`emp`VALUES(4,'张三','男','1996-08-27','湖北武汉','初中',2300.00,'平时兢兢业业');INSERTINTO`emp`VALUES(5,'麻子','男','1994-09-13','湖南怀化','专科',4500.00,'有一技之长');INSERTINTO`emp`VALUES(6,'李四','男','1996-12-21','湖南怀化','高中',3200.00,'');INSERTINTO`emp`VALUES(7,'林黛玉','女','1998-08-12','湖南株洲','研究生',6500.00,'工作认真负责,可堪大用');INSERTINTO`emp`VALUES(8,'王小二','男','1999-02-03','湖南常德','高中',4500.00,'做事认真');INSERTINTO`emp`VALUES(9,'张晓','女','2000-02-13','湖南岳阳','高中',4500.00,'工作认真负责,兢兢业业');INSERTINTO`emp`VALUES(10,'顾枫熙','女','2000-03-16','湖南株洲','专科',4500.00,'');

Java实现代码

数据访问层

1.基础层BaseDao

packagecom.dao;importjava.sql.Connection;importjava.sql.SQLException;importcom.util.DbUtil;//创建对数据库连接对象publicclassBaseDao{publicConnection con=newDbUtil().getCon();publicvoidcloseDao(){try{
            con.close();}catch(SQLException e){
            e.printStackTrace();}}}

2.管理员AdminDao

packagecom.dao;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importcom.model.Admin;publicclassAdminDaoextendsBaseDao{publicAdminlogin(Admin admin){String sql ="select * from admin where aname=? and password=?";Admin adminRst =null;try{PreparedStatement prst = con.prepareStatement(sql);//把sql语句传给数据库操作对象
            prst.setString(1, admin.getName());
            prst.setString(2, admin.getPassword());ResultSet executeQuery = prst.executeQuery();if(executeQuery.next()){
                adminRst =newAdmin();
                adminRst.setId(executeQuery.getInt("ano"));
                adminRst.setName(executeQuery.getString("aname"));
                adminRst.setPassword(executeQuery.getString("password"));}}catch(SQLException e){
            e.printStackTrace();}return adminRst;}}

3.员工StaffDao

packagecom.dao;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.List;importcom.model.Staff;importcom.util.StringUtil;publicclassStaffDaoextendsBaseDao{//展示员工列表,按员工姓名模糊查询publicList<Staff>getStaffList(Staff staff ){List<Staff> retList=newArrayList<Staff>();StringBuffer sqlString =newStringBuffer("select * from emp");if(!StringUtil.isEmpty(staff.getYname())){
            sqlString.append(" and yname like '%"+staff.getYname()+"%'");}try{PreparedStatement preparedStatement = con.prepareStatement(sqlString.toString().replaceFirst("and","where"));ResultSet executeQuery = preparedStatement.executeQuery();while(executeQuery.next()){Staff s =newStaff();
                s.setYno(executeQuery.getInt("yno"));
                s.setYname(executeQuery.getString("yname"));
                s.setSex(executeQuery.getString("sex"));
                s.setBirth(executeQuery.getString("birth"));
                s.setHome(executeQuery.getString("home"));
                s.setDegree(executeQuery.getString("degree"));
                s.setSalary(executeQuery.getDouble("salary"));
                s.setInfo(executeQuery.getString("info"));
                retList.add(s);}}catch(SQLException e){
            e.printStackTrace();}return retList;}//删除员工信息publicbooleandelete(int yno){String sql ="delete from emp where yno=?";try{PreparedStatement preparedStatement = con.prepareStatement(sql);
                preparedStatement.setInt(1, yno);if(preparedStatement.executeUpdate()>0){returntrue;}}catch(SQLException e){
                e.printStackTrace();}returnfalse;}//修改员工信息publicbooleanupdate(Staff staff){String sql ="update emp set yname=?,sex=?,birth=?,home=?,degree=?,salary=?,info=? where yno=?";try{PreparedStatement preparedStatement = con.prepareStatement(sql);
                preparedStatement.setString(1, staff.getYname());
                preparedStatement.setString(2, staff.getSex());
                preparedStatement.setString(3, staff.getBirth());
                preparedStatement.setString(4, staff.getHome());
                preparedStatement.setString(5, staff.getDegree());
                preparedStatement.setDouble(6, staff.getSalary());
                preparedStatement.setString(7, staff.getInfo());
                preparedStatement.setInt(8, staff.getYno());if(preparedStatement.executeUpdate()>0){returntrue;}}catch(SQLException e){
                e.printStackTrace();}returnfalse;}//增加员工信息publicbooleanaddStaff(Staff staff){String sql ="insert into emp values(null,?,?,?,?,?,?,?)";try{java.sql.PreparedStatement preparedStatement = con.prepareStatement(sql);
                preparedStatement.setString(1, staff.getYname());
                preparedStatement.setString(2, staff.getSex());
                preparedStatement.setString(3, staff.getBirth());
                preparedStatement.setString(4, staff.getHome());
                preparedStatement.setString(5, staff.getDegree());
                preparedStatement.setDouble(6, staff.getSalary());
                preparedStatement.setString(7, staff.getInfo());if(preparedStatement.executeUpdate()>0)returntrue;}catch(SQLException e){
                e.printStackTrace();}returnfalse;}}

数据库实体层

1.管理员Admin

packagecom.model;publicclassAdmin{privateint id;privateString name;privateString password;publicintgetId(){return id;}publicvoidsetId(int id){this.id = id;}publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicStringgetPassword(){return password;}publicvoidsetPassword(String password){this.password = password;}}

2.员工Staff

packagecom.model;publicclassStaff{privateint yno;privateString yname;privateString ypassword;privateString sex;privateString birth;privateString home;privateString degree;privateDouble salary;privateString info;publicintgetYno(){return yno;}publicvoidsetYno(int yno){this.yno = yno;}publicStringgetYname(){return yname;}publicvoidsetYname(String yname){this.yname = yname;}publicStringgetYpassword(){return ypassword;}publicvoidsetYpassword(String ypassword){this.ypassword = ypassword;}publicStringgetSex(){return sex;}publicvoidsetSex(String sex){this.sex = sex;}publicStringgetBirth(){return birth;}publicvoidsetBirth(String birth){this.birth = birth;}publicStringgetHome(){return home;}publicvoidsetHome(String home){this.home = home;}publicStringgetDegree(){return degree;}publicvoidsetDegree(String degree){this.degree = degree;}publicDoublegetSalary(){return salary;}publicvoidsetSalary(Double salary){this.salary = salary;}publicStringgetInfo(){return info;}publicvoidsetInfo(String info){this.info = info;}}

工具类

1.DbUtil

packagecom.util;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;//利用JDBC连接MySQL数据库publicclassDbUtil{privateString dbUrl="jdbc:mysql://localhost:3306/employees?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";privateString dbUserName="root";privateString dbPassword="root";privateString jdbcName="com.mysql.cj.jdbc.Driver";//获取数据库连接publicConnection getCon (){try{Class.forName(jdbcName);}catch(ClassNotFoundException e){
                e.printStackTrace();}Connection con=null;try{
                con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);}catch(SQLException e){
                e.printStackTrace();}return con;}//关闭数据库连接publicvoidcloseCon(Connection con)throwsException{if(con!=null){
             con.close();}}publicstaticvoidmain(String[] args){DbUtil dbUtil=newDbUtil();try{
             dbUtil.getCon();System.out.println("数据库连接成功!");}catch(Exception e){
             e.printStackTrace();System.out.println("数据库连接失败!");}}}

2.StringUtil

packagecom.util;//判断字符串是否为空publicclassStringUtil{publicstaticbooleanisEmpty(String str){if("".equals(str)|| str==null){returntrue;}returnfalse;}}

视图层

1.LoginFrm

packagecom.view;importjava.awt.EventQueue;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JPasswordField;importjavax.swing.JTextField;importcom.dao.AdminDao;importcom.model.Admin;importcom.view.MainFrm;importcom.util.StringUtil;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;@SuppressWarnings({"serial","unused"})publicclassLoginFrmextendsJFrame{privateJPanel contentPane;privateJLabel titleLabel;privateJLabel anameJlabel;privateJTextField anameTextField;privateJLabel passwordJlabel;privateJPasswordField passwordTextField;privateJButton submitButton;privateJButton resetButton;publicstaticvoidmain(String[] args){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{LoginFrm frame =newLoginFrm();
                    frame.setVisible(true);}catch(Exception e){
                    e.printStackTrace();}}});}publicLoginFrm(){setTitle("登录系统");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口setBounds(500,150,630,450);//设置页面大小setResizable(false);//设置页面不可拖拽改变大小getContentPane().setLayout(null);setVisible(true);
        
        titleLabel =newJLabel("员工管理系统");
        titleLabel.setFont(newFont("华文行楷",Font.PLAIN,30));
        titleLabel.setBounds(215,65,182,34);getContentPane().add(titleLabel);
        
        anameJlabel =newJLabel("账号:");
        anameJlabel.setFont(newFont("宋体",Font.PLAIN,20));
        anameTextField=newJTextField();
        anameJlabel.setBounds(155,152,67,30);
        anameTextField.setBounds(243,152,200,30);getContentPane().add(anameJlabel);getContentPane().add(anameTextField);
        
        passwordJlabel =newJLabel("密码:");
        passwordJlabel.setFont(newFont("宋体",Font.PLAIN,20));
        passwordTextField=newJPasswordField();
        passwordJlabel.setBounds(155,202,67,30);
        passwordTextField.setBounds(243,202,200,30);getContentPane().add(passwordJlabel);getContentPane().add(passwordTextField);
        
        resetButton =newJButton("重置");
        resetButton.setBounds(340,265,90,30);getContentPane().add(resetButton);
        resetButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent ae){resetValue(ae);}});
        
        submitButton =newJButton("登录");
        submitButton.setBounds(195,265,90,30);getContentPane().add(submitButton);
        submitButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent ae){loginAct(ae);}});}protectedvoidloginAct(ActionEvent ae){// TODO 自动生成的方法存根String aname=anameTextField.getText().toString();String password=passwordTextField.getText().toString();if(StringUtil.isEmpty(aname)){JOptionPane.showMessageDialog(this,"用户名不能为空!");return;}if(StringUtil.isEmpty(password)){JOptionPane.showMessageDialog(this,"密码不能为空!");return;}AdminDao adminDao =newAdminDao();Admin adminTmp =newAdmin();
        adminTmp.setName(aname);
        adminTmp.setPassword(password);Admin admin = adminDao.login(adminTmp);
        adminDao.closeDao();if(admin ==null){JOptionPane.showMessageDialog(this,"用户名或密码错误!");return;}JOptionPane.showMessageDialog(this,"欢迎"+admin.getName()+"登录本系统!");this.dispose();newMainFrm(admin).setVisible(true);}protectedvoidresetValue(ActionEvent ae){
        anameTextField.setText("");
        passwordTextField.setText("");}}

2.MainFrm

packagecom.view;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.util.List;importjava.util.Vector;importjavax.swing.ButtonGroup;importjavax.swing.JButton;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JRadioButton;importjavax.swing.JScrollPane;importjavax.swing.JTable;importjavax.swing.JTextArea;importjavax.swing.JTextField;importjavax.swing.table.DefaultTableModel;importcom.dao.StaffDao;importcom.util.StringUtil;importcom.model.Staff;importjava.awt.Color;importjava.awt.Font;@SuppressWarnings("serial")publicclassMainFrmextendsJFrame{privateJTable staffListTable;privateButtonGroup sexButtonGroup;privateJButton deleteStaffButton;privateJButton searchButton;privateJLabel ynameJlabel;privateJTextField ynameTextField;privateJTextField searchTextField;privateDefaultTableModel model;privateJScrollPane jscrollpane;privateJLabel sexJLabel;privateJRadioButtonMRadioButton;privateJRadioButtonFRadioButton;privateJLabel birthJlabel;privateJTextField birthTextField;privateJLabel homeJlabel;privateJTextField homeTextField;privateJLabel degreeJlabel;privateJComboBox degreeJComboBox;privateJLabel salaryJlabel;privateJTextField salaryTextField;privateJLabel infoJlabel;privateJTextArea infoTextArea;privateJButton addButton;privateJButton resetButton;privateJButton editButton;publicstaticObject userObject;publicMainFrm(Object userObject){this.userObject = userObject;setTitle("员工管理系统");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口setBounds(200,20,1200,700);//设置页面大小setResizable(false);//设置页面不可拖拽改变大小getContentPane().setLayout(null);setVisible(true);
        
        searchTextField =newJTextField();
        searchTextField.setBounds(250,10,180,30);getContentPane().add(searchTextField);
        searchTextField.setColumns(10);
        
        searchButton =newJButton("搜索");
        searchButton.setBounds(450,10,95,30);getContentPane().add(searchButton);
        searchButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent ae){searchStaff(ae);}});Object[] header ={"员工号","员工姓名","性别","出生日期","籍贯","学历","工资","备注"};
        model =newDefaultTableModel(header,0);
        staffListTable =newJTable(model);
        jscrollpane =newJScrollPane(staffListTable);
        jscrollpane.setBounds(10,50,800,600);getContentPane().add(jscrollpane);setTable(newStaff());//展示员工信息列表
        staffListTable.addMouseListener(newMouseAdapter(){@OverridepublicvoidmouseClicked(MouseEvent me){selectedTableRow(me);}});
        
        ynameJlabel =newJLabel("员工姓名:");
        ynameTextField=newJTextField();
        ynameJlabel.setBounds(860,50,100,30);
        ynameTextField.setBounds(950,50,200,30);getContentPane().add(ynameJlabel);getContentPane().add(ynameTextField);
        
        sexJLabel =newJLabel("性      别:");
        sexJLabel.setBounds(860,100,74,23);getContentPane().add(sexJLabel);MRadioButton=newJRadioButton("男");MRadioButton.setBounds(975,100,74,23);getContentPane().add(MRadioButton);FRadioButton=newJRadioButton("女");FRadioButton.setBounds(1051,100,74,23);getContentPane().add(FRadioButton);
        sexButtonGroup =newButtonGroup();
        sexButtonGroup.add(MRadioButton);
        sexButtonGroup.add(FRadioButton);MRadioButton.setSelected(true);
        
        birthJlabel =newJLabel("出生日期:");
        birthTextField=newJTextField();
        birthJlabel.setBounds(860,150,100,30);
        birthTextField.setBounds(950,150,200,30);getContentPane().add(birthJlabel);getContentPane().add(birthTextField);
        
        homeJlabel =newJLabel("籍      贯:");
        homeTextField=newJTextField();
        homeJlabel.setBounds(860,200,100,30);
        homeTextField.setBounds(950,200,200,30);getContentPane().add(homeJlabel);getContentPane().add(homeTextField);
        
        degreeJlabel =newJLabel("学      历:");
        degreeJComboBox =newJComboBox();
        degreeJlabel.setBounds(860,250,100,30);
        degreeJComboBox.setBounds(950,250,200,30);getContentPane().add(degreeJlabel);getContentPane().add(degreeJComboBox);
        
        salaryJlabel =newJLabel("工      资:");
        salaryTextField=newJTextField();
        salaryJlabel.setBounds(860,300,100,30);
        salaryTextField.setBounds(950,300,200,30);getContentPane().add(salaryJlabel);getContentPane().add(salaryTextField);
        
        infoJlabel =newJLabel("备      注:");
        infoTextArea=newJTextArea();
        infoJlabel.setBounds(860,350,100,30);
        infoTextArea.setBounds(950,350,200,80);getContentPane().add(infoJlabel);getContentPane().add(infoTextArea);JLabel remindLabel =newJLabel("出生日期应为YYYY-MM-DD或YYYY.MM.DD类型,否则添加失败");
        remindLabel.setFont(newFont("宋体",Font.PLAIN,13));
        remindLabel.setForeground(Color.RED);
        remindLabel.setBounds(820,440,360,32);getContentPane().add(remindLabel);
        
        addButton =newJButton("新增员工");
        addButton.setBounds(820,500,90,30);getContentPane().add(addButton);
        addButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent ae){staffAddAct(ae);}});setEduInfo();
        
        editButton =newJButton("修改信息");
        editButton.setBounds(910,500,90,30);getContentPane().add(editButton);
        editButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent ae){submitEditAct(ae);}});
        
        resetButton =newJButton("重置");
        resetButton.setBounds(1000,500,90,30);getContentPane().add(resetButton);
        resetButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent ae){resetValue(ae);}});
        
        deleteStaffButton =newJButton("删除员工");
        deleteStaffButton.setBounds(1090,500,90,30);getContentPane().add(deleteStaffButton);
        deleteStaffButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvent ae){deleteStaff(ae);}});}//查询员工信息protectedvoidsearchStaff(ActionEvent ae){// TODO 自动生成的方法存根Staff staff =newStaff();
            staff.setYname(searchTextField.getText().toString());setTable(staff);}//删除员工信息protectedvoiddeleteStaff(ActionEvent ae){// TODO 自动生成的方法存根int row = staffListTable.getSelectedRow();if(row ==-1){JOptionPane.showMessageDialog(this,"请选中要删除的数据!");return;}if(JOptionPane.showConfirmDialog(this,"您确定删除么?")!=JOptionPane.OK_OPTION){return;}StaffDao staffDao =newStaffDao();if(staffDao.delete(Integer.parseInt(staffListTable.getValueAt(row,0).toString()))){JOptionPane.showMessageDialog(this,"删除成功!");}else{JOptionPane.showMessageDialog(this,"删除失败!");}
        staffDao.closeDao();setTable(newStaff());}//列表展示员工信息publicvoidsetTable(Staff staff){    
        model =(DefaultTableModel) staffListTable.getModel();
        model.setRowCount(0);StaffDao staffDao=newStaffDao();List<Staff>StaffList= staffDao.getStaffList(staff);for(Staff s:StaffList){Vector v=newVector();
            v.add(s.getYno());
            v.add(s.getYname());
            v.add(s.getSex());
            v.add(s.getBirth());
            v.add(s.getHome());
            v.add(s.getDegree());
            v.add(s.getSalary());
            v.add(s.getInfo());
            model.addRow(v);}
        staffDao.closeDao();}//修改员工信息protectedvoidsubmitEditAct(ActionEvent ae){// TODO 自动生成的方法存根int row = staffListTable.getSelectedRow();if(row ==-1){JOptionPane.showMessageDialog(this,"请选中要修改的数据!");return;}String staffName = ynameTextField.getText().toString();String birth = birthTextField.getText().toString();String home = homeTextField.getText().toString();String degree =(String) degreeJComboBox.getSelectedItem();String sex =MRadioButton.isSelected()?MRadioButton.getText():FRadioButton.getText();Double salary =Double.parseDouble(salaryTextField.getText().toString());String info = infoTextArea.getText().toString();if(StringUtil.isEmpty(staffName)){JOptionPane.showMessageDialog(this,"请填写员工姓名!");return;}if(StringUtil.isEmpty(home)){JOptionPane.showMessageDialog(this,"请填写员工籍贯!");return;}Staff staff =newStaff();
    staff.setYname(staffName);
    staff.setYno(Integer.parseInt(staffListTable.getValueAt(row,0).toString()));if(MRadioButton.isSelected())staff.setSex(MRadioButton.getText().toString());if(FRadioButton.isSelected())staff.setSex(FRadioButton.getText().toString());
    staff.setSex(sex);
    staff.setBirth(birth);
    staff.setHome(home);
    staff.setDegree(degree);
    staff.setSalary(salary);
    staff.setInfo(info);StaffDao staffDao =newStaffDao();if(staffDao.update(staff)){JOptionPane.showMessageDialog(this,"更新成功!");}else{JOptionPane.showMessageDialog(this,"更新失败!");}
    staffDao.closeDao();setTable(newStaff());}//重置按钮privatevoidresetValue(ActionEvent ae){// TODO 自动生成的方法存根
            ynameTextField.setText("");
            birthTextField.setText("");
            infoTextArea.setText("");
            homeTextField.setText("");
            salaryTextField.setText("");
            degreeJComboBox.setSelectedIndex(0);
            sexButtonGroup.clearSelection();MRadioButton.setSelected(true);}//添加员工信息protectedvoidstaffAddAct(ActionEvent ae){// TODO Auto-generated method stubString staffName = ynameTextField.getText().toString();String birth = birthTextField.getText().toString();String home = homeTextField.getText().toString();Double salary =Double.parseDouble(salaryTextField.getText().toString());String info = infoTextArea.getText().toString();if(StringUtil.isEmpty(staffName)){JOptionPane.showMessageDialog(this,"请填写员工姓名!");return;}if(StringUtil.isEmpty(home)){JOptionPane.showMessageDialog(this,"请填写员工籍贯!");return;}String degree =(String) degreeJComboBox.getSelectedItem();String sex =MRadioButton.isSelected()?MRadioButton.getText():FRadioButton.getText();Staff staff =newStaff();
            staff.setYname(staffName);
            staff.setSex(sex);
            staff.setBirth(birth);
            staff.setHome(home);
            staff.setDegree(degree);
            staff.setSalary(salary);
            staff.setInfo(info);StaffDao staffDao =newStaffDao();if(staffDao.addStaff(staff)){JOptionPane.showMessageDialog(this,"添加成功!");setTable(newStaff());}else{JOptionPane.showMessageDialog(this,"添加失败!");}resetValue(ae);}//学历下拉框内容privatevoidsetEduInfo(){
            degreeJComboBox.addItem("文盲");
            degreeJComboBox.addItem("小学");
            degreeJComboBox.addItem("初中");
            degreeJComboBox.addItem("高中");
            degreeJComboBox.addItem("专科");
            degreeJComboBox.addItem("本科");
            degreeJComboBox.addItem("研究生");
            degreeJComboBox.addItem("博士");}//实现列表与员工信息内容联动privatevoidselectedTableRow(MouseEvent me){
            model =(DefaultTableModel) staffListTable.getModel();
            ynameTextField.setText(model.getValueAt(staffListTable.getSelectedRow(),1).toString());
            birthTextField.setText(model.getValueAt(staffListTable.getSelectedRow(),3).toString());String sex = model.getValueAt(staffListTable.getSelectedRow(),2).toString();
            sexButtonGroup.clearSelection();if(sex.equals(MRadioButton.getText()))MRadioButton.setSelected(true);if(sex.equals(FRadioButton.getText()))FRadioButton.setSelected(true);
            homeTextField.setText(model.getValueAt(staffListTable.getSelectedRow(),4).toString());
            degreeJComboBox.setSelectedItem(model.getValueAt(staffListTable.getSelectedRow(),5).toString());
            salaryTextField.setText(model.getValueAt(staffListTable.getSelectedRow(),6).toString());
            infoTextArea.setText(model.getValueAt(staffListTable.getSelectedRow(),7).toString());}}

运行效果图

1.登录界面
此页面使用来实现管理员的登录。管理员通过输入账号和密码进行登录,点击登录会触发登录判断事件,首先判断用户是否填写账号和密码,如果账号或密码为空会弹窗提示不能为空,需要用户重新输入,当输入均不为空时会获取文本框内容来判断是否和数据库中的管理员账号密码一致。当用户输入错误时可以直接点击重置按钮对文本框内容进行清空操作。
在这里插入图片描述
2.主界面
管理员登录成功后跳转到系统主界面,管理员可在此进行相应操作。此界面包括以下功能:
1)员工信息添加功能:该功能可以添加新员工信息。其中员工号是自动增加,无需手动添加,员工姓名和籍贯为必填项,性别通过使用JRadioButton和ButtonGroup组件限制用户只能选择性别为男或女,默认性别为男,学历通过使用JComboBox下拉框限制规范填写学历信息,默认学历为文盲,此外出生日期填写应规范为“YYYY-MM-DD”或“YYYY.MM.DD”模式,否则添加失败。
2)员工信息查询功能:该功能可以通过对员工姓名的模糊查询,查询到所需员工的基本信息。若需查询所有员工信息,可在不填写搜索框的情况下点击搜索,即可查看到全部员工的基本信息。
3)员工信息修改功能:该功能可以修改选中员工的基本信息,其中员工号是不可修改信息,员工姓名和籍贯为必填项,此外出生日期填写应规范为“YYYY-MM-DD”或“YYYY.MM.DD”模式,否则修改失败。
4)员工信息删除功能:该功能可以删除选中员工的基本信息。
在这里插入图片描述

标签: java 数据库 mysql

本文转载自: https://blog.csdn.net/KUMAKAIHA/article/details/128056664
版权归原作者 快跑有熊孩子 所有, 如有侵权,请联系我们删除。

“Java Swing大作业--简易员工管理系统”的评论:

还没有评论