From eee30f7def04bbf77359bf73f091d4c36c748838 Mon Sep 17 00:00:00 2001 From: GuanM <30427262+sxhoio@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:23:42 +0800 Subject: [PATCH] =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Java/TodoListApp/src/DatabaseManager.java | 43 ++++++++++++++++++----- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/Java/TodoListApp/src/DatabaseManager.java b/Java/TodoListApp/src/DatabaseManager.java index 2105d8b..9a5482e 100644 --- a/Java/TodoListApp/src/DatabaseManager.java +++ b/Java/TodoListApp/src/DatabaseManager.java @@ -3,8 +3,11 @@ package TodoListApp.src; import java.sql.*; import java.util.ArrayList; import java.util.List; +import java.util.logging.Logger; +import java.util.logging.Level; public class DatabaseManager { + private static final Logger LOGGER = Logger.getLogger(DatabaseManager.class.getName()); private static final String URL = "jdbc:mysql://localhost:3306/TodoList"; private static final String USER = "root"; private static final String PASSWORD = "123456"; @@ -13,23 +16,27 @@ public class DatabaseManager { try { Class.forName("com.mysql.cj.jdbc.Driver"); } catch (ClassNotFoundException e) { - e.printStackTrace(); + LOGGER.log(Level.SEVERE, "MySQL JDBC 驱动程序未找到", e); + throw new RuntimeException("无法初始化数据库连接", e); } } - public void addTodo(String description) throws SQLException { + public void addTodo(String description) { String sql = "INSERT INTO todos (description) VALUES (?)"; - try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); + try (Connection conn = getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql)) { pstmt.setString(1, description); pstmt.executeUpdate(); + } catch (SQLException e) { + LOGGER.log(Level.SEVERE, "添加待办事项时发生错误", e); + throw new RuntimeException("无法添加待办事项", e); } } - public List getAllTodos() throws SQLException { + public List getAllTodos() { List todos = new ArrayList<>(); String sql = "SELECT * FROM todos"; - try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); + try (Connection conn = getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) { while (rs.next()) { @@ -37,25 +44,43 @@ public class DatabaseManager { String description = rs.getString("description"); todos.add(new TodoItem(id, description)); } + } catch (SQLException e) { + LOGGER.log(Level.SEVERE, "获取所有待办事项时发生错误", e); + throw new RuntimeException("无法获取待办事项列表", e); } return todos; } - public void removeTodo(int id) throws SQLException { + public void removeTodo(int id) { String sql = "DELETE FROM todos WHERE id = ?"; - try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); + try (Connection conn = getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql)) { pstmt.setInt(1, id); pstmt.executeUpdate(); + } catch (SQLException e) { + LOGGER.log(Level.SEVERE, "删除待办事项时发生错误", e); + throw new RuntimeException("无法删除待办事项", e); } } - public void markAsCompleted(int id) throws SQLException { + public void markAsCompleted(int id) { String sql = "UPDATE todos SET completed = true WHERE id = ?"; - try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); + try (Connection conn = getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql)) { pstmt.setInt(1, id); pstmt.executeUpdate(); + } catch (SQLException e) { + LOGGER.log(Level.SEVERE, "标记待办事项为完成时发生错误", e); + throw new RuntimeException("无法标记待办事项为完成", e); + } + } + + private Connection getConnection() throws SQLException { + try { + return DriverManager.getConnection(URL, USER, PASSWORD); + } catch (SQLException e) { + LOGGER.log(Level.SEVERE, "无法建立数据库连接", e); + throw e; } } }