员管理系统 java_员工管理系统java版

时间: 2023-10-18 22:16:23 浏览: 50
员工管理系统是一个比较常见的管理软件,可以帮助企业和组织对员工进行管理。Java语言是一种非常流行的编程语言,也可以用来开发员工管理系统。 在Java中,可以使用Swing或JavaFX等GUI库来创建用户界面,使用JDBC或ORM框架来连接数据库,实现数据的存储和读取。以下是一个简单的员工管理系统的Java代码示例: ```java import java.sql.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class EmployeeManagementSystem extends JFrame implements ActionListener { private JLabel titleLabel, nameLabel, ageLabel, genderLabel, positionLabel; private JTextField nameField, ageField, positionField; private JRadioButton maleRadioButton, femaleRadioButton; private ButtonGroup genderButtonGroup; private JButton addButton, updateButton, deleteButton, clearButton; private JList employeeList; private DefaultListModel employeeListModel; private Connection conn; private Statement stmt; private ResultSet rs; public EmployeeManagementSystem() { setTitle("Employee Management System"); // Initialize GUI components titleLabel = new JLabel("Employee Management System"); nameLabel = new JLabel("Name:"); ageLabel = new JLabel("Age:"); genderLabel = new JLabel("Gender:"); positionLabel = new JLabel("Position:"); nameField = new JTextField(20); ageField = new JTextField(3); positionField = new JTextField(20); maleRadioButton = new JRadioButton("Male", true); femaleRadioButton = new JRadioButton("Female", false); genderButtonGroup = new ButtonGroup(); genderButtonGroup.add(maleRadioButton); genderButtonGroup.add(femaleRadioButton); addButton = new JButton("Add"); updateButton = new JButton("Update"); deleteButton = new JButton("Delete"); clearButton = new JButton("Clear"); employeeListModel = new DefaultListModel(); employeeList = new JList(employeeListModel); // Add GUI components to the frame setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; gbc.anchor = GridBagConstraints.CENTER; gbc.insets = new Insets(10, 0, 10, 0); add(titleLabel, gbc); gbc.gridy++; gbc.gridwidth = 1; gbc.anchor = GridBagConstraints.EAST; gbc.insets = new Insets(5, 5, 5, 5); add(nameLabel, gbc); gbc.gridx++; gbc.anchor = GridBagConstraints.WEST; add(nameField, gbc); gbc.gridy++; gbc.gridx = 0; gbc.anchor = GridBagConstraints.EAST; add(ageLabel, gbc); gbc.gridx++; gbc.anchor = GridBagConstraints.WEST; add(ageField, gbc); gbc.gridy++; gbc.gridx = 0; gbc.anchor = GridBagConstraints.EAST; add(genderLabel, gbc); gbc.gridx++; gbc.anchor = GridBagConstraints.WEST; add(maleRadioButton, gbc); gbc.gridx++; add(femaleRadioButton, gbc); gbc.gridy++; gbc.gridx = 0; gbc.anchor = GridBagConstraints.EAST; add(positionLabel, gbc); gbc.gridx++; gbc.anchor = GridBagConstraints.WEST; add(positionField, gbc); gbc.gridy++; gbc.gridx = 0; gbc.anchor = GridBagConstraints.CENTER; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(10, 0, 10, 0); add(addButton, gbc); gbc.gridx++; add(updateButton, gbc); gbc.gridy++; gbc.gridx = 0; add(deleteButton, gbc); gbc.gridx++; add(clearButton, gbc); gbc.gridy++; gbc.gridx = 0; gbc.gridwidth = 2; gbc.fill = GridBagConstraints.BOTH; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.insets = new Insets(10, 5, 5, 5); add(new JScrollPane(employeeList), gbc); // Add action listeners to the buttons addButton.addActionListener(this); updateButton.addActionListener(this); deleteButton.addActionListener(this); clearButton.addActionListener(this); // Connect to the database try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/employee", "root", ""); stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM employees"); while (rs.next()) { employeeListModel.addElement(rs.getString("name")); } rs.close(); stmt.close(); conn.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } setSize(500, 500); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == addButton) { String name = nameField.getText(); int age = Integer.parseInt(ageField.getText()); String gender = maleRadioButton.isSelected() ? "Male" : "Female"; String position = positionField.getText(); employeeListModel.addElement(name); // Insert the new employee into the database try { conn = DriverManager.getConnection("jdbc:mysql://localhost/employee", "root", ""); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO employees (name, age, gender, position) VALUES (?, ?, ?, ?)"); pstmt.setString(1, name); pstmt.setInt(2, age); pstmt.setString(3, gender); pstmt.setString(4, position); pstmt.executeUpdate(); pstmt.close(); conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } else if (e.getSource() == updateButton) { int selectedIndex = employeeList.getSelectedIndex(); String name = nameField.getText(); int age = Integer.parseInt(ageField.getText()); String gender = maleRadioButton.isSelected() ? "Male" : "Female"; String position = positionField.getText(); employeeListModel.setElementAt(name, selectedIndex); // Update the employee in the database try { conn = DriverManager.getConnection("jdbc:mysql://localhost/employee", "root", ""); PreparedStatement pstmt = conn.prepareStatement("UPDATE employees SET name=?, age=?, gender=?, position=? WHERE name=?"); pstmt.setString(1, name); pstmt.setInt(2, age); pstmt.setString(3, gender); pstmt.setString(4, position); pstmt.setString(5, (String) employeeListModel.getElementAt(selectedIndex)); pstmt.executeUpdate(); pstmt.close(); conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } else if (e.getSource() == deleteButton) { int selectedIndex = employeeList.getSelectedIndex(); employeeListModel.removeElementAt(selectedIndex); // Delete the employee from the database try { conn = DriverManager.getConnection("jdbc:mysql://localhost/employee", "root", ""); PreparedStatement pstmt = conn.prepareStatement("DELETE FROM employees WHERE name=?"); pstmt.setString(1, (String) employeeListModel.getElementAt(selectedIndex)); pstmt.executeUpdate(); pstmt.close(); conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } else if (e.getSource() == clearButton) { nameField.setText(""); ageField.setText(""); maleRadioButton.setSelected(true); positionField.setText(""); employeeList.clearSelection(); } } public static void main(String[] args) { new EmployeeManagementSystem(); } } ``` 这个员工管理系统使用了MySQL数据库来存储员工的信息。运行程序后,可以添加、更新、删除和清空员工信息,同时也可以从数据库中读取已有的员工信息并显示在界面上。

相关推荐

最新推荐

recommend-type

基于java的超市管理系统.docx

本文着重论述超市管理方面的有关超市进货、销售、库存管理、人员管理等内容,紧紧围绕超市管理系统的实际运作展开具体论述。 超市管理系统主要完成的功能有:商品基本信息的录入、修改和查询;商品销售信息的录入、...
recommend-type

基于Java EE的工资管理系统的论文

本毕业设计的内容是设计并且实现一个基于Java Web技术的工资管理系统,故而系统主要以JavaEE技术作为开发基础,主要使用了Spring+SpringMvc+Mybatis等多种框架的结合使用,用myeclipse2014作为开发工具,以MYSQL作为...
recommend-type

JavaWeb设计工资管理系统(报告书)

针对这些要求,设计本月工资信息管理系统。该系统主要包括职工信息、工资级别和工资金额、公司福利表等。 ⒈员工基本状况 包括员工号、员工姓名、性别、所在部门、工资级别、工资等级等。 2.工资级别和工资金额 ...
recommend-type

集团版本HR系统解决方案(超详细).docx

5.2.2. 员工关系管理 34 5.2.3. 合同管理 41 5.2.4. 薪酬福利管理 42 5.2.5. 考勤休假管理 50 5.2.6. 培训管理 53 5.2.7. 招聘管理 58 5.2.8. 绩效管理 62 5.2.9. 报表中心 68 5.2.10. 员工自助 71 5.2.11. 业务后台...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依