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