at com.wgh.tools.ConnDB.executeQuery(ConnDB.java:56)
at com.wgh.dao.DiaryDao.queryDiary(DiaryDao.java:28)
NPE 發生在 executeQuery 第 56 行,通常是 Connection 或 Statement 為 null。**這不是資料庫本身的錯誤**,而是 Java 程式碼未正確處理連線失敗或資源為 null 的情況。
ConnDB.java:56 確認哪個物件為 null(conn、stmt、rs)。getConnection() 實作:是否捕獲 SQLException 並回傳 null?是否印出錯誤日誌?executeQuery 入口加入 if (conn == null) throw new SQLException("conn is null"); 讓錯誤更明確。public ResultSet executeQuery(String sql) throws SQLException {
Connection conn = getConnection();
if (conn == null) throw new SQLException("Connection is null, check getConnection()");
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
return rs; // 注意:try-with-resources 會關閉 stmt/rs,這裡需調整為手動管理或複製資料
}
}
實際專案建議:DAO 層回傳 List<Entity> 而非 ResultSet,在 finally 關閉所有資源。