public ResultSet executeQuery(String sql) {
try { // 捕捉異常
conn = getConnection(); // 調用getConnection()方法構造connection對象的一個實例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {
System.err.println(ex.getMessage()); // 輸出異常消息
}
return rs; // 返回結果集對象
}
報錯:
java.lang.NullPointerException
com.wgh.tools.ConnDB.executeQuery(ConnDB.java:56)
com.wgh.dao.DiaryDao.queryDiary(DiaryDao.java:28)
com.wgh.servlet.DiaryServlet.listAllDiary(DiaryServlet.java:249)
com.wgh.servlet.DiaryServlet.doPost(DiaryServlet.java:52)
com.wgh.servlet.DiaryServlet.doGet(DiaryServlet.java:66)
javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.wgh.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:24)
下面這是56行:
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
conn = getConnection(); // 調用getConnection()方法構造connection對象的一個實例conn
if(conn != null){
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}