程序如下
5.public class DBConBean {
private static final String URL = "jdbc:mysql://127.0.0.1:3306/bank";
private static final String USER = "root";
private static final String PASSWORD = "";
private static Connection conn = null;
Statement stat = null;
PreparedStatement pstat = null;
ResultSet rs = null;
int rs_1 = 0;
public static Connection getConnection() {
try {
//加載驅動程序
Class.forName("com.mysql.jdbc.Driver");
//獲得數據庫連接
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public ResultSet query (String sql) {
try {
conn = getConnection();
37. stat = conn.createStatement();
rs = stat.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
報錯如下
type Exception report
message java.lang.NullPointerException
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:477)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NullPointerException
com.zy.dao.DBConBean.query(DBConBean.java:37)
org.apache.jsp.Page_005fuser_jsp._jspService(Page_005fuser_jsp.java:161)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.65 logs.
求大神解答
斷點一下看看conn是不是為空,如果為空就可以往2個方向找錯誤,1,有沒有jdbc的驅動包,2、用戶名密碼和URL是不是對,你這裡可以在本地用數據庫的客戶端看看能不能臉上,有沒有這個數據庫。