基于Java Swing的办公自动化系统设计与实现:附完整源码与论文
JAVA+SQL办公自动化系统
一、系统概述
本办公自动化系统采用Java Swing开发桌面应用,结合SQL Server数据库实现文档管理、流程审批、日程安排等核心功能。系统采用MVC架构模式,实现了模块化开发与维护,支持多用户协同办公。
二、系统架构设计
1. 技术选型
- 前端:Java Swing
- 后端:Java SE
- 数据库:SQL Server 2019
- 数据访问:JDBC
- 开发工具:IntelliJ IDEA
2. 系统架构
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── office
│ │ │ ├── controller (控制器层)
│ │ │ ├── model (模型层)
│ │ │ ├── view (视图层)
│ │ │ ├── dao (数据访问层)
│ │ │ └── utils (工具类)
│ │ └── resources
│ │ └── db.properties (数据库配置)
三、核心代码实现
1. 数据库连接工具类
// DBConnectionUtil.java
public class DBConnectionUtil {private static Connection connection;private static final String URL;private static final String USERNAME;private static final String PASSWORD;private static final String DRIVER;static {try {Properties properties = new Properties();InputStream inputStream = DBConnectionUtil.class.getClassLoader().getResourceAsStream("db.properties");properties.load(inputStream);URL = properties.getProperty("url");USERNAME = properties.getProperty("username");PASSWORD = properties.getProperty("password");DRIVER = properties.getProperty("driver");Class.forName(DRIVER);} catch (Exception e) {throw new ExceptionInInitializerError(e);}}public static Connection getConnection() throws SQLException {if (connection == null || connection.isClosed()) {connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);}return connection;}public static void close(Connection conn, Statement stmt, ResultSet rs) {try {if (rs != null) rs.close();if (stmt != null) stmt.close();if (conn != null) conn.close();} catch (SQLException e) {e.printStackTrace();}}
}
2. 用户管理模块
// User.java
public class User {private int id;private String username;private String password;private String realName;private String department;private String role;private Date createTime;// getters and setters
}// UserDAO.java
public class UserDAO {public User login(String username, String password) {User user = null;Connection conn = null;PreparedStatement stmt = null;ResultSet rs = null;try {conn = DBConnectionUtil.getConnection();String sql = "SELECT * FROM users WHERE username = ? AND password = ?";stmt = conn.prepareStatement(sql);stmt.setString(1, username);stmt.setString(2, password);rs = stmt.executeQuery();if (rs.next()) {user = new User();user.setId(rs.getInt("id"));user.setUsername(rs.getString("username"));user.setRealName(rs.getString("realName"));user.setDepartment(rs.getString("department"));user.setRole(rs.getString("role"));user.setCreateTime(rs.getDate("createTime"));}} catch (SQLException e) {e.printStackTrace();} finally {DBConnectionUtil.close(conn, stmt, rs);}return user;}public List<User> getAllUsers() {List<User> users = new ArrayList<>();Connection conn = null;Statement stmt = null;ResultSet rs = null;try {conn = DBConnectionUtil.getConnection();String sql = "SELECT * FROM users";stmt = conn.createStatement();rs = stmt.executeQuery(sql);while (rs.next()) {User user = new User();user.setId(rs.getInt("id"));user.setUsername(rs.getString("username"));user.setRealName(rs.getString("realName"));user.setDepartment(rs.getString("department"));user.setRole(rs.getString("role"));user.setCreateTime(rs.getDate("createTime"));users.add(user);}} catch (SQLException e) {e.printStackTrace();} finally {DBConnectionUtil.close(conn, stmt, rs);}return users;}
}
3. 文档管理模块
// Document.java
public class Document {private int id;private String title;private String content;private String author;private Date createTime;private String category;private String status;// getters and setters
}// DocumentDAO.java
public class DocumentDAO {public boolean addDocument(Document document) {Connection conn = null;PreparedStatement stmt = null;try {conn = DBConnectionUtil.getConnection();String sql = "INSERT INTO documents (title, content, author, createTime, category, status) " +"VALUES (?, ?, ?, ?, ?, ?)";stmt = conn.prepareStatement(sql);stmt.setString(1, document.getTitle());stmt.setString(2, document.getContent());stmt.setString(3, document.getAuthor());stmt.setTimestamp(4, new Timestamp(document.getCreateTime().getTime()));stmt.setString(5, document.getCategory());stmt.setString(6, document.getStatus());int rows = stmt.executeUpdate();return rows > 0;} catch (SQLException e) {e.printStackTrace();return false;} finally {DBConnectionUtil.close(conn, stmt, null);}}public List<Document> searchDocuments(String keyword) {List<Document> documents = new ArrayList<>();Connection conn = null;PreparedStatement stmt = null;ResultSet rs = null;try {conn = DBConnectionUtil.getConnection();String sql = "SELECT * FROM documents WHERE title LIKE ? OR content LIKE ?";stmt = conn.prepareStatement(sql);stmt.setString(1, "%" + keyword + "%");stmt.setString(2, "%" + keyword + "%");rs = stmt.executeQuery();while (rs.next()) {Document document = new Document();document.setId(rs.getInt("id"));document.setTitle(rs.getString("title"));document.setContent(rs.getString("content"));document.setAuthor(rs.getString("author"));document.setCreateTime(rs.getTimestamp("createTime"));document.setCategory(rs.getString("category"));document.setStatus(rs.getString("status"));documents.add(document);}} catch (SQLException e) {e.printStackTrace();} finally {DBConnectionUtil.close(conn, stmt, rs);}return documents;}
}
4. 流程审批模块
// ApprovalFlow.java
public class ApprovalFlow {private int id;private String flowName;private String applicant;private Date applyTime;private String status;private String content;private String currentApprover;// getters and setters
}// ApprovalFlowDAO.java
public class ApprovalFlowDAO {public List<ApprovalFlow> getPendingApprovals(String approver) {List<ApprovalFlow> flows = new ArrayList<>();Connection conn = null;PreparedStatement stmt = null;ResultSet rs = null;try {conn = DBConnectionUtil.getConnection();String sql = "SELECT * FROM approval_flows WHERE currentApprover = ? AND status = 'pending'";stmt = conn.prepareStatement(sql);stmt.setString(1, approver);rs = stmt.executeQuery();while (rs.next()) {ApprovalFlow flow = new ApprovalFlow();flow.setId(rs.getInt("id"));flow.setFlowName(rs.getString("flowName"));flow.setApplicant(rs.getString("applicant"));flow.setApplyTime(rs.getDate("applyTime"));flow.setStatus(rs.getString("status"));flow.setContent(rs.getString("content"));flow.setCurrentApprover(rs.getString("currentApprover"));flows.add(flow);}} catch (SQLException e) {e.printStackTrace();} finally {DBConnectionUtil.close(conn, stmt, rs);}return flows;}public boolean approveFlow(int flowId, String approver, boolean approved, String comments) {Connection conn = null;PreparedStatement stmt = null;try {conn = DBConnectionUtil.getConnection();conn.setAutoCommit(false);// 更新流程状态String updateSql = "UPDATE approval_flows SET status = ?, comments = ?, currentApprover = ?, updateTime = GETDATE() " +"WHERE id = ? AND currentApprover = ?";stmt = conn.prepareStatement(updateSql);stmt.setString(1, approved ? "approved" : "rejected");stmt.setString(2, comments);stmt.setString(3, "");stmt.setInt(4, flowId);stmt.setString(5, approver);int rows = stmt.executeUpdate();if (rows > 0) {// 记录审批历史String historySql = "INSERT INTO approval_history (flowId, approver, approvalTime, status, comments) " +"VALUES (?, ?, GETDATE(), ?, ?)";stmt = conn.prepareStatement(historySql);stmt.setInt(1, flowId);stmt.setString(2, approver);stmt.setString(3, approved ? "approved" : "rejected");stmt.setString(4, comments);stmt.executeUpdate();conn.commit();return true;} else {conn.rollback();return false;}} catch (SQLException e) {e.printStackTrace();try {conn.rollback();} catch (SQLException ex) {ex.printStackTrace();}return false;} finally {try {conn.setAutoCommit(true);} catch (SQLException e) {e.printStackTrace();}DBConnectionUtil.close(conn, stmt, null);}}
}
四、系统界面设计
1. 登录界面
// LoginFrame.java
public class LoginFrame extends JFrame {private JTextField usernameField;private JPasswordField passwordField;private UserDAO userDAO = new UserDAO();public LoginFrame() {setTitle("办公自动化系统 - 登录");setSize(400, 300);setDefaultCloseOperation(EXIT_ON_CLOSE);setLocationRelativeTo(null);JPanel panel = new JPanel();panel.setLayout(new GridLayout(4, 2, 10, 10));add(panel, BorderLayout.CENTER);JLabel usernameLabel = new JLabel("用户名:");usernameField = new JTextField();JLabel passwordLabel = new JLabel("密码:");passwordField = new JPasswordField();JButton loginButton = new JButton("登录");loginButton.addActionListener(e -> login());JButton resetButton = new JButton("重置");resetButton.addActionListener(e -> {usernameField.setText("");passwordField.setText("");});panel.add(usernameLabel);panel.add(usernameField);panel.add(passwordLabel);panel.add(passwordField);panel.add(new JLabel()); // 占位panel.add(new JLabel()); // 占位panel.add(loginButton);panel.add(resetButton);}private void login() {String username = usernameField.getText();String password = new String(passwordField.getPassword());User user = userDAO.login(username, password);if (user != null) {JOptionPane.showMessageDialog(this, "登录成功");dispose();new MainFrame(user).setVisible(true);} else {JOptionPane.showMessageDialog(this, "用户名或密码错误", "登录失败", JOptionPane.ERROR_MESSAGE);}}public static void main(String[] args) {SwingUtilities.invokeLater(() -> new LoginFrame().setVisible(true));}
}
2. 主界面
// MainFrame.java
public class MainFrame extends JFrame {private User currentUser;private JTabbedPane tabbedPane;public MainFrame(User user) {this.currentUser = user;setTitle("办公自动化系统 - " + user.getRealName());setSize(1000, 700);setDefaultCloseOperation(EXIT_ON_CLOSE);setLocationRelativeTo(null);initComponents();}private void initComponents() {// 菜单栏JMenuBar menuBar = new JMenuBar();JMenu systemMenu = new JMenu("系统");JMenuItem logoutItem = new JMenuItem("退出登录");logoutItem.addActionListener(e -> logout());systemMenu.add(logoutItem);JMenu documentMenu = new JMenu("文档管理");JMenuItem addDocItem = new JMenuItem("新建文档");JMenuItem manageDocItem = new JMenuItem("文档列表");documentMenu.add(addDocItem);documentMenu.add(manageDocItem);JMenu approvalMenu = new JMenu("流程审批");JMenuItem submitFlowItem = new JMenuItem("提交申请");JMenuItem pendingApprovalsItem = new JMenuItem("待审批");JMenuItem approvalHistoryItem = new JMenuItem("审批历史");approvalMenu.add(submitFlowItem);approvalMenu.add(pendingApprovalsItem);approvalMenu.add(approvalHistoryItem);menuBar.add(systemMenu);menuBar.add(documentMenu);menuBar.add(approvalMenu);setJMenuBar(menuBar);// 标签页tabbedPane = new JTabbedPane();add(tabbedPane, BorderLayout.CENTER);// 根据用户角色显示不同的初始页面if ("admin".equals(currentUser.getRole())) {tabbedPane.addTab("用户管理", new UserManagementPanel());}tabbedPane.addTab("文档管理", new DocumentManagementPanel());tabbedPane.addTab("我的申请", new MyApplicationsPanel(currentUser.getUsername()));tabbedPane.addTab("待我审批", new PendingApprovalsPanel(currentUser.getUsername()));}private void logout() {if (JOptionPane.showConfirmDialog(this, "确定要退出登录吗?", "确认", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {dispose();new LoginFrame().setVisible(true);}}
}
五、系统部署与测试
1. 环境要求
- JDK 1.8+
- SQL Server 2017+
- JDBC驱动:mssql-jdbc-10.2.1.jre8.jar
2. 部署步骤
- 创建SQL Server数据库并执行建表脚本
- 配置db.properties中的数据库连接信息
- 使用Maven或IDE打包项目
- 运行程序:java -jar office-automation.jar
3. 测试用例
// UserDAOTest.java
public class UserDAOTest {private UserDAO userDAO = new UserDAO();@Testpublic void testLogin() {User user = userDAO.login("admin", "admin123");assertNotNull(user);assertEquals("管理员", user.getRealName());}@Testpublic void testGetAllUsers() {List<User> users = userDAO.getAllUsers();assertTrue(users.size() > 0);}
}// DocumentDAOTest.java
public class DocumentDAOTest {private DocumentDAO documentDAO = new DocumentDAO();@Testpublic void testAddDocument() {Document document = new Document();document.setTitle("测试文档");document.setContent("这是一篇测试文档内容");document.setAuthor("testuser");document.setCreateTime(new Date());document.setCategory("技术文档");document.setStatus("active");boolean result = documentDAO.addDocument(document);assertTrue(result);}@Testpublic void testSearchDocuments() {List<Document> documents = documentDAO.searchDocuments("测试");assertTrue(documents.size() > 0);}
}
六、毕业设计文档框架
1. 论文框架
- 引言
- 相关技术综述
- 系统需求分析
- 系统设计
- 系统实现
- 系统测试
- 总结与展望
2. 外文翻译
- 选择一篇与办公自动化系统相关的外文文献
- 翻译内容包括:摘要、引言、核心技术部分、结论
- 翻译字数建议在3000-5000字
七、总结
本系统实现了办公自动化的核心功能,采用Java+SQL Server技术栈,具有良好的可扩展性和维护性。系统提供了友好的用户界面和完善的功能模块,可满足中小型企业的日常办公需求。