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);
}
}
```