员工管理系统(图形界面带数据库 )
目录
- 1. 功能介绍与展示(多图警告)
- 2. 代码设计
- 1 数据库
- 2 登录界面与验证
- 3 主界面
- 4 信息更改
- 5 信息展示
Tip: 这学期Java课要做一个员工管理系统的项目,刚听到这个题目的时候,我们刚学完一些面向过程的内容,对于图形化编程几乎毫无了解,后来在各种借鉴自学中知道了各种swing控件、布局的使用、数据库操作、事件。对于初学者来说,是一个不错的练手之作。
环境:NetBeans 11.2 、Mysql 8.0.17
希望大家多多支持 谢谢 源码在这 Github链接
1. 功能介绍与展示(多图警告)
该系统的功能简介:
1.资料管理:修改、添加信息(薪水仅可由管理员修改)
2.信息显示:显示所有用户的公开信息。
3.员工管理:添加、删除用户。
4:退出系统:正常退出程序。
登录界面:
主界面的区别在于 只有管理员可以进行员工管理
主界面(管理员)
主界面(普通用户)
信息修改
密码更改
信息展示
员工管理
2. 代码设计
1 数据库
我的数据库采用的是Mysql 8.0.17
JDBC驱动程序 Type 4: Native-protocol driver(本地协议驱动程序),即使用不同数据库时提供对应的驱动jar包
其具体驱动过程:
应用程序→JDBC Driver→Database Engine→Database
在使用驱动jar包时,注意到,对于mysql版本8.0以上与8.0一下,使用的jar包和连接语句是不同的。
mysql 8.0以上版本驱动下载mysql-connector-java-8.0.16.jar
mysql 8.0以下版本驱动下载mysql-connector-java-5.1.39-bin.jar
mysql数据库结构
数据库连接类
package human.resoruce.dbconnection;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;//数据库连接
public class DbConnect {static final String DB_URL = "jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC"; static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; // Mysql version lower than 8.0
// static final String DB_URL = "jdbc:mysql://localhost:3306/mydb";
// static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String user = "root"; //根据本地mysql账户密码修改static final String password = "root"; public Connection conn = null; public PreparedStatement pst = null; public DbConnect(String sql) { try { //注册JDBC驱动Class.forName(JDBC_DRIVER);//打开链接conn = DriverManager.getConnection(DB_URL,user,password); pst = conn.prepareStatement(sql);} catch(SQLException e){ JOptionPane.showMessageDialog(null, "Connection is not Opened ! " + e.getMessage()); } catch (ClassNotFoundException ex) {JOptionPane.showMessageDialog(null, "Class not found ! " + ex.getMessage()); }} public void close() { try { this.conn.close(); this.pst.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) {}
}
2 登录界面与验证
登录界面这里用的是非图形化编程,涉及到添加背景图片,查阅资料后使用JLayerdPane控制层级间的优先关系,将含有背景图片的层置底,再将其余控件加入相应面板后添加至底层。需要注意的是,密码框用的是passwordText,在获取时可以这样使用new String(passwordText.getPassword())
下列代码确保窗口显示居中
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((dim.width - this.getWidth()) / 2, (dim.height - this.getHeight()) / 2);
该行代码表示,点击右上角叉时关闭程序
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
该行使窗口大小不能改变,主要原因是发现手动调整窗口大小时,其余控件的大小与相对位置没有变化,于是干脆设置不可变大小。
this.setResizable(false);
登录界面完整代码
package human.resource.login;import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;//Login Frame
public class Login extends JFrame { private JLayeredPane pane=new JLayeredPane();//最底层层级面板private JPanel backPanel=new JPanel();//背景图片private JPanel headPanel=new JPanel();//标题 private JPanel panel1 =new JPanel();//帐号private JPanel panel2 =new JPanel();//密码private JPanel panel3 =new JPanel();//按钮private JLabel back;//背景图片private JLabel headLight=new JLabel("Human Resource Management");//标题private JLabel userLabel =new JLabel("username:");private JLabel passwordLabel = new JLabel("password:");private JTextField userText = new JTextField();private JPasswordField passwordText = new JPasswordField();private JButton but1 = new JButton("Login");ImageIcon image;@SuppressWarnings("unchecked")public Login() {image=new ImageIcon(getClass().getResource("/Photos/1.jpg"));headLight.setOpaque(false);userLabel.setOpaque(false);userText.setOpaque(false);passwordLabel.setOpaque(false);passwordText.setOpaque(false);backPanel.setOpaque(false);headPanel.setOpaque(false);panel1.setOpaque(false);panel2.setOpaque(false);panel3.setOpaque(false);//background photoback=new JLabel(image);backPanel.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());backPanel = (JPanel)this.getContentPane(); backPanel.add(back);//titleheadLight.setFont(new Font("黑体",Font.BOLD,35));headPanel.setLayout(new GridLayout(1,1));headPanel.setBounds(10,40,500,200);headPanel.add(headLight);//useruserLabel.setFont(new Font("黑体",Font.BOLD,40));userText.setFont(new Font("黑体",Font.BOLD,40));panel1.setLayout(new GridLayout(2,1)); panel1.setBounds(130, 240, 220, 70); panel1.add(userLabel); panel1.add(userText); //passwordpasswordLabel.setFont(new Font("黑体",Font.BOLD,40));passwordText.setFont(new java.awt.Font("Yu Gothic Medium", 0, 30));panel2.setLayout(new GridLayout(2,1)); panel2.setBounds(130, 330, 220, 70); panel2.add(passwordLabel); panel2.add(passwordText); //buttionbut1.setSize(300,60);but1.setFont(new Font("黑体",Font.BOLD,20));panel3.setLayout(new GridLayout(1,2)); panel3.setBounds(50, 500, 400, 50); panel3.add(but1); pane.add(backPanel,JLayeredPane.DEFAULT_LAYER);pane.add(headPanel, JLayeredPane.MODAL_LAYER);pane.add(panel1,JLayeredPane.MODAL_LAYER);pane.add(panel2,JLayeredPane.POPUP_LAYER);pane.add(panel3,JLayeredPane.DRAG_LAYER);//Login eventbut1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new Userverify(userText.getText(),new String(passwordText.getPassword()));userText.setText("");passwordText.setText("");}});this.setSize(image.getIconWidth(),image.getIconHeight());Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();this.setLocation((dim.width - this.getWidth()) / 2, (dim.height - this.getHeight()) / 2);this.setTitle("Human Resource Management");setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);this.setLayeredPane(pane);this.setResizable(false);this.setVisible(true);}public static void main(String[] args) {Login login = new Login();}}
登录验证
思路:从登录界面传递输入的账号密码,与从数据库提取的账号密码进行比对,并按权限调用主界面并传递权限数据,账号密码错误弹出提示。
//acount verify
public class Userverify {String sql = null; DbConnect db = null;ResultSet ret = null; public Userverify(String user,String pass) {//select spicific accoutsql = "select username,password,authority from person where username="+'"'+user+'"';db = new DbConnect(sql);try { //ret will recieve query answersret = db.pst.executeQuery();while (ret.next()) { String u = ret.getString(1); //username in dbString p = ret.getString(2); //password in dbint authority=ret.getInt(3); if(u.equals(user)&&p.equals(pass)&&authority==2) {MainFrame mainFrame = new MainFrame(u,1); //administration} else if(u.equals(user)&&p.equals(pass)&&authority==1){MainFrame mainFrame = new MainFrame(u,0); //normal user}else {JOptionPane.showMessageDialog(null,"Account or password is wrong !!!","错误",JOptionPane.ERROR_MESSAGE);}}ret.close(); db.close();} catch (SQLException e) { e.printStackTrace(); }catch(Exception e1){e1.printStackTrace();}}public static void main(String[] args) {}
}
3 主界面
主界面这里是用netbeans的图形化界面编程,一个菜单栏,若干选项。打开界面后利用账户名使用select查询数据库,显示当前账户信息。
由于User Manage是菜单不是按钮,所以设置的是ButtonMouseClicked事件,当鼠标点击,打开新窗口,如果是普通用户,初始化为setEnabled(false),但仍可通过点击打开新窗口,所以在事件中还需再判断一次权限。
这里使用这个表示,自动隐藏并释放该窗体。但继续运行应用程序,可以从登陆界面再次登陆,同理,其它的二级界面都应这样设置
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
public class MainFrame extends javax.swing.JFrame {String user;int right;//right=1 admin right =0 normalpublic MainFrame(String user,int right) {this.user=user;this.right=right;initComponents();showInfo(user);//normal users could only check and change their own infoif(right==0){userManageButton.setEnabled(false);}Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();this.setLocation((dim.width - this.getWidth()) / 2, (dim.height - this.getHeight()) / 2);this.setTitle("HRM");setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);this.setResizable(false);this.setVisible(true);}@SuppressWarnings("unchecked") private void initComponents(){}//略 自动生成private void passChangeButtionActionPerformed(java.awt.event.ActionEvent evt) { new PassChange(user,right);this.dispose();} private void changeInfoButtonActionPerformed(java.awt.event.ActionEvent evt) { new InfoChange(user,right);this.dispose();} private void userManageButtonMouseClicked(java.awt.event.MouseEvent evt) { if(right==1)new UserManage();} private void exitButtonMouseClicked(java.awt.event.MouseEvent evt) { System.exit(0);} private void infoSearchButtonMouseClicked(java.awt.event.MouseEvent evt) { InfoShow1 infoShow = new InfoShow1(user);} private void showInfo(String u){}//略
4 信息更改
信息修改这里说白了有两个难点,一个是从文本框中获取字符串数据,并转换为对应格式的数据,二是sql语句的书写。
需要注意的是,薪水只能由管理员更改。
文本框信息提取
注意到,在提取数字时,如果数据为空,直接转换保存会报错,所有可以先判断有没有数据,再进行Double.parseDouble(),或Integer.parseInt()的格式转换。
信息提取完后,判断是否有空文本,并进行警告,确定所有数据非空后弹出确认框,进行数据库操作。
private void changeButtonActionPerformed(java.awt.event.ActionEvent evt) { //data gatherphone=phoneText.getText();position=positText.getText();skill=skillText.getText();city=cityText.getText();name=nameText.getText();experience=experienceText.getText();String u=salaryText.getText();if(!u.isEmpty()){double i=Double.parseDouble(u);this.salary=i;}u=yearText.getText();if(!u.isEmpty()){int i=Integer.parseInt(u);this.year=i;}if(phone.isEmpty()||position.isEmpty()||skill.isEmpty()||city.isEmpty()||skill.isEmpty()||name.isEmpty()||experience.isEmpty()){JOptionPane.showMessageDialog(this,"One of the information is empty,please check again","Wrong",JOptionPane.WARNING_MESSAGE);new InfoChange(username,authority);this.dispose();}else{int n = JOptionPane.showConfirmDialog(null, "Comfirm?", "Comfirm Frame", JOptionPane.YES_NO_OPTION);if (n == JOptionPane.YES_OPTION) {dbOperation db=new dbOperation();db.change();new MainFrame(username,authority);this.dispose();}}}
数据库操作
在写数据库语言时要注意,如果是字符串数据,要在变量两边加上‘’号,而数字不需要。
private class dbOperation{String sql = null; DbConnect db = null;ResultSet ret = null; //update info in mysqlvoid change(){try {sql="update person set name ='"+name+"',phone ='"+phone+"',position ='"+position+"',salary ="+salary+",skill ='"+skill+"',city ='"+city+"',workyear ="+year+",experience ='"+experience+"'";db=new DbConnect(sql);db.pst.executeUpdate(sql); db.close();} catch (SQLException e) {e.printStackTrace();}}void show(){} //当前用户信息显示 略}
5 信息展示
信息展示使用JScrollPane滚动面板容纳数据表,表的生成使用 DefaultTableModel,如model=new DefaultTableModel(data, t)。其中,data是二维向量,储存数据,t是表头。
public class InfoShow1 extends JFrame{String username;JTable table;DefaultTableModel model;public InfoShow1(String user) {this.username=user;this.setSize(800,550);Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();this.setLocation((dim.width - this.getWidth()) / 2, (dim.height - this.getHeight()) / 2);this.setTitle("Info Show");setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);this.setResizable(false);table=new JTable();table.setBounds(10,100,900,400);model=new DefaultTableModel();this.showInfo();table.setModel(model);JScrollPane sp=new JScrollPane(table);sp.setBounds(50,100,900,400);this.add(sp);this.setVisible(true);} public void showInfo() {String sql=null;DbConnect db=null;ResultSet ret=null;sql="select name,salary,skill,workyear,experience from person"; //查询db=new DbConnect(sql);try {ret=db.pst.executeQuery();ResultSetMetaData rsmd=ret.getMetaData();int count =rsmd.getColumnCount();Vector t=new Vector();for(int i=1;i<=count;i++) {//table headt.add(rsmd.getColumnName(i));}Vector data=new Vector();//二维向量while(ret.next()) {Vector v1=new Vector();for(int i=0;i<count;i++) {v1.add(ret.getString(i+1));}data.add(v1); //v1表示一行数据}model=new DefaultTableModel(data, t); //生成表ret.close();db.close();} catch (SQLException e) {e.printStackTrace();}}public static void main(String user) {InfoShow1 infoShow1 = new InfoShow1(user);}
}
员工管理系统(图形界面带数据库 )相关推荐
- 分享一套完整的汽车维修订单管理系统源码 带数据库文档
完整的汽车维修管理系统源码带数据库文档 开发环境为Microsoft Visual Studio 2012以上版本,数据库为SQLServer2008 R2,使用C#语言开发. 一个完整的汽车维修管理 ...
- jsp员工管理系统Myeclipse开发mysql数据库web结构java编程计算机网页项目
一.源码特点 jsp 员工管理系统 是一套完善的web设计系统,对理解JSP java编程开发语言有帮助,系统具有完整的源代码和数据库,系统主要采用B/S模式开发.开发环境为TOMCAT7.0,M ...
- asp.net员工管理系统VS开发sqlserver数据库web结构c#编程计算机网页项目
一.源码特点 ASP.NET员工管理系统是一套完善的计算机web设计系统,系统具有完整的源代码和数据库,系统主要采用B/S模式开发.开发环境为vs2010,数据库为sqlserver20 ...
- java计算机毕业设计酒店员工管理系统源码+mysql数据库+系统+lw文档+部署
java计算机毕业设计酒店员工管理系统源码+mysql数据库+系统+lw文档+部署 java计算机毕业设计酒店员工管理系统源码+mysql数据库+系统+lw文档+部署 本源码技术栈: 项目架构:B/S ...
- java计算机毕业设计HTML5企业员工管理系统源码+mysql数据库+系统+lw文档+部署
java计算机毕业设计HTML5企业员工管理系统源码+mysql数据库+系统+lw文档+部署 java计算机毕业设计HTML5企业员工管理系统源码+mysql数据库+系统+lw文档+部署 本源码技术栈 ...
- Java学习--网络,图形界面和数据库
深入理解java2:软件开发常用实用技术 1文件及目录:java.io 1.1基本操作 File f; f=new File("a.txt");//当前目录下的文件 f=new F ...
- MySQL图形界面创建数据库
创建数据库 第一步 创建数据库的方法有两种 在Schemas栏空白地方点击右键箭头Create Schema 2.或者使用工具栏上的这个图标 第二步 完成上面的步骤,会弹出来下面的界面,在Na ...
- python实现图形界面设计+数据库(pyodbc)教材征订系统
2022.06.17-13.34.07 依赖的库函数 wxPython 4.1.1 pyodbc 4.0.30 pip 22.0.3 tip:如何使用pip安装特定版本的python第三方包参考博文 ...
- 抽卡模拟系统(包含图形界面,结果可存储到数据库中)
软件工程的大作业,要求写一个系统.自己平时玩过一些抽卡手游,类似于<原神>.<FGO>之类的,因此想试一试写一个类似的程序. 这篇文章受到了这个讲解抽卡机制的b站视频的启发: ...
最新文章
- TCP/IP:IP选项处理
- 博创提供专业的解决方案---宝钢高炉改造无线监控
- 一位软件工程师的6年总结(转)
- stk 坐标系_STK中文用户手册.pdf
- Java相关资料分享(视频+电子书籍)
- 分类法过时了吗?【ZZ】
- Sublime Text 3 初试牛刀
- spring mvc学习(54):简单异常处理
- 深度学习(10)-- Capsules Networks(CapsNet)
- React之props的简写方式和报错示范
- Sharepoint Server 2007结合AD RMS提高企业信息安全
- 热烈庆祝博客排名进入前一万(8448)
- Qt + 运动控制 (固高运动控制卡)【2】运动控制卡初始化和关闭
- 16QAM 硬判决(Matlab)
- ISO14001认证
- shawn0102_播客:留下每年35万美元的工作以学习编码-Shawn Wang访谈
- ISO14229之概述
- 高校最美图书馆!飘在水上?
- G1垃圾回收器在并发场景调优
- 饿了么美团外卖cps返利系统外卖返利公众号搭建cps系统小程序SaaS源码