Swing用MVC模型实现企业考勤系统,实现三个功能,并连接数据库,展示代码

时间: 2024-03-15 11:47:01 浏览: 14
好的,以下是一个简单的Swing MVC模式的企业考勤系统示例,实现了三个功能(添加员工、查看员工列表、查看员工考勤记录),并连接MySQL数据库展示代码: Model类: ```java public class EmployeeModel { private int id; private String name; private String department; private String position; private String tel; public EmployeeModel(int id, String name, String department, String position, String tel) { this.id = id; this.name = name; this.department = department; this.position = position; this.tel = tel; } // getter and setter methods } public class AttendanceModel { private int id; private int employeeId; private Date date; private String time; public AttendanceModel(int id, int employeeId, Date date, String time) { this.id = id; this.employeeId = employeeId; this.date = date; this.time = time; } // getter and setter methods } ``` View类: ```java public class EmployeeView extends JPanel { private JTextField tfName; private JTextField tfDepartment; private JTextField tfPosition; private JTextField tfTel; private JButton btnAdd; private JLabel lblStatus; public EmployeeView() { // UI components initialization // ... } public void addAddButtonListener(ActionListener listener) { btnAdd.addActionListener(listener); } // getter methods for input fields public String getName() { return tfName.getText().trim(); } public String getDepartment() { return tfDepartment.getText().trim(); } public String getPosition() { return tfPosition.getText().trim(); } public String getTel() { return tfTel.getText().trim(); } // display status message public void setStatus(String message) { lblStatus.setText(message); } // clear input fields public void clearFields() { tfName.setText(""); tfDepartment.setText(""); tfPosition.setText(""); tfTel.setText(""); } } public class EmployeeListView extends JPanel { private JTable table; private DefaultTableModel tableModel; public EmployeeListView() { // UI components initialization // ... tableModel = new DefaultTableModel(); tableModel.addColumn("ID"); tableModel.addColumn("Name"); tableModel.addColumn("Department"); tableModel.addColumn("Position"); tableModel.addColumn("Tel"); table.setModel(tableModel); } // update employee list table public void updateTable(List<EmployeeModel> employees) { tableModel.setRowCount(0); for (EmployeeModel employee : employees) { Object[] rowData = {employee.getId(), employee.getName(), employee.getDepartment(), employee.getPosition(), employee.getTel()}; tableModel.addRow(rowData); } } // get selected employee's ID public int getSelectedEmployeeId() { int row = table.getSelectedRow(); if (row >= 0) { return (int) tableModel.getValueAt(row, 0); } return -1; } } public class AttendanceView extends JPanel { private JTable table; private DefaultTableModel tableModel; private JLabel lblStatus; public AttendanceView() { // UI components initialization // ... tableModel = new DefaultTableModel(); tableModel.addColumn("ID"); tableModel.addColumn("Employee ID"); tableModel.addColumn("Date"); tableModel.addColumn("Time"); table.setModel(tableModel); } // update attendance table public void updateTable(List<AttendanceModel> attendances) { tableModel.setRowCount(0); for (AttendanceModel attendance : attendances) { Object[] rowData = {attendance.getId(), attendance.getEmployeeId(), attendance.getDate(), attendance.getTime()}; tableModel.addRow(rowData); } } // display status message public void setStatus(String message) { lblStatus.setText(message); } } ``` Controller类: ```java public class EmployeeController { private EmployeeView view; private EmployeeDAO dao; public EmployeeController(EmployeeView view, EmployeeDAO dao) { this.view = view; this.dao = dao; view.addAddButtonListener(new AddButtonListener()); } // add employee button listener class AddButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String name = view.getName(); String department = view.getDepartment(); String position = view.getPosition(); String tel = view.getTel(); if (name.isEmpty() || department.isEmpty() || position.isEmpty() || tel.isEmpty()) { view.setStatus("Please fill in all fields!"); return; } EmployeeModel employee = new EmployeeModel(0, name, department, position, tel); boolean success = dao.addEmployee(employee); if (success) { view.setStatus("Employee added successfully!"); view.clearFields(); } else { view.setStatus("Failed to add employee!"); } } } } public class EmployeeListController { private EmployeeListView view; private EmployeeDAO dao; public EmployeeListController(EmployeeListView view, EmployeeDAO dao) { this.view = view; this.dao = dao; view.updateTable(dao.getAllEmployees()); } } public class AttendanceController { private AttendanceView view; private AttendanceDAO dao; public AttendanceController(AttendanceView view, AttendanceDAO dao) { this.view = view; this.dao = dao; view.updateTable(dao.getAllAttendances()); } } ``` DAO类: ```java public class EmployeeDAO { private Connection conn; public EmployeeDAO(String url, String user, String password) { try { conn = DriverManager.getConnection(url, user, password); } catch (SQLException e) { e.printStackTrace(); } } // add employee to database public boolean addEmployee(EmployeeModel employee) { String sql = "INSERT INTO employees (name, department, position, tel) VALUES (?, ?, ?, ?)"; try { PreparedStatement stmt = conn.prepareStatement(sql); stmt.setString(1, employee.getName()); stmt.setString(2, employee.getDepartment()); stmt.setString(3, employee.getPosition()); stmt.setString(4, employee.getTel()); int rows = stmt.executeUpdate(); return rows > 0; } catch (SQLException e) { e.printStackTrace(); return false; } } // get all employees from database public List<EmployeeModel> getAllEmployees() { List<EmployeeModel> employees = new ArrayList<>(); String sql = "SELECT * FROM employees"; try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String department = rs.getString("department"); String position = rs.getString("position"); String tel = rs.getString("tel"); EmployeeModel employee = new EmployeeModel(id, name, department, position, tel); employees.add(employee); } } catch (SQLException e) { e.printStackTrace(); } return employees; } } public class AttendanceDAO { private Connection conn; public AttendanceDAO(String url, String user, String password) { try { conn = DriverManager.getConnection(url, user, password); } catch (SQLException e) { e.printStackTrace(); } } // get all attendance records from database public List<AttendanceModel> getAllAttendances() { List<AttendanceModel> attendances = new ArrayList<>(); String sql = "SELECT * FROM attendance"; try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); int employeeId = rs.getInt("employee_id"); Date date = rs.getDate("date"); String time = rs.getString("time"); AttendanceModel attendance = new AttendanceModel(id, employeeId, date, time); attendances.add(attendance); } } catch (SQLException e) { e.printStackTrace(); } return attendances; } } ``` Main类: ```java public class Main { public static void main(String[] args) { // database connection information String url = "jdbc:mysql://localhost:3306/mydatabase"; String user = "root"; String password = "password"; // create database access objects EmployeeDAO employeeDAO = new EmployeeDAO(url, user, password); AttendanceDAO attendanceDAO = new AttendanceDAO(url, user, password); // create views EmployeeView employeeView = new EmployeeView(); EmployeeListView employeeListView = new EmployeeListView(); AttendanceView attendanceView = new AttendanceView(); // create controllers EmployeeController employeeController = new EmployeeController(employeeView, employeeDAO); EmployeeListController employeeListController = new EmployeeListController(employeeListView, employeeDAO); AttendanceController attendanceController = new AttendanceController(attendanceView, attendanceDAO); // create main frame and add views JFrame frame = new JFrame("Enterprise Attendance System"); JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.addTab("Add Employee", employeeView); tabbedPane.addTab("Employee List", employeeListView); tabbedPane.addTab("Attendance Records", attendanceView); frame.add(tabbedPane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } } ```

相关推荐

最新推荐

recommend-type

java(swing)+ mysql实现学生信息管理系统

主要分享了java mysql实现学生信息管理系统的源码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Java swing读取txt文件实现学生考试系统

主要为大家详细介绍了Java swing读取txt文件实现学生考试系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

基于java swing实现答题系统

主要为大家详细介绍了基于java swing实现答题系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Java编程实现swing圆形按钮实例代码

主要介绍了Java编程实现swing圆形按钮实例代码,涉及两个简单的Java实现按钮的代码,其中一个具有侦测点击事件的简单功能,具有一定借鉴价值,需要的朋友可以参考。
recommend-type

基于Java swing组件实现简易计算器

主要介绍了基于Java swing组件实现简易计算器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
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

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

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