javaweb一直报空指针异常,java文件单独运行时没有问题的,但是Jsp一引用就报取不到值
jsp代码:try{DBmanageDB=newDBmanage();Stringsql="selecttop3*fromadmin";ResultSetrs=DB.do...
jsp代码:
try {DBmanage DB = new DBmanage();
String sql = "select top 3 * from admin";
ResultSet rs = DB.doQuery(sql);
request.setCharacterEncoding("utf-8");
while (rs.next()) {
%><%
<p><%=rs.getString(1)%></p>
<p><%=rs.getString(2)%></p>
<p><%=rs.getString(3)%></p>
}
DB.closeConnection();
} catch (SQLException e) {
out.println("conn:" + e.getMessage());
}
%>
java代码:
public class DBmanage {
public Connection con=null;
public Statement stmt=null;
public ResultSet rs=null;
private static String dbDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String dburl="jdbc:sqlserver://localhost:1433;DatabaseName=student";
private static String dbname="sa";
private static String dbpassword="12345";
public static Connection getConnection(){
Connection conn=null;
try {
Class.forName(dbDriver);
conn=DriverManager.getConnection(dburl, dbname, dbpassword);
System.out.println("连接成功");
} catch (Exception e) {
// TODO: handle exception
System.err.println("连接失败");
}
return conn;
}
public ResultSet doQuery(String sql){
try {
con=DBmanage.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("连接失败"+e.getMessage());
}
return rs;
}
报错:
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 21
18: try {
19: DBmanage DB = new DBmanage();
20: String sql = "select top 3 * from admin";
21: ResultSet rs = DB.doQuery(sql);
22: %>
23: <meta http-equiv="pragma" content="no-cache">
24: <meta http-equiv="cache-control" content="no-cache">
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:588)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
root cause
java.lang.NullPointerException
db.DBmanage.doQuery(DBmanage.java:32)
org.apache.jsp.index_jsp._jspService(index_jsp.java:140)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 展开
try {DBmanage DB = new DBmanage();
String sql = "select top 3 * from admin";
ResultSet rs = DB.doQuery(sql);
request.setCharacterEncoding("utf-8");
while (rs.next()) {
%><%
<p><%=rs.getString(1)%></p>
<p><%=rs.getString(2)%></p>
<p><%=rs.getString(3)%></p>
}
DB.closeConnection();
} catch (SQLException e) {
out.println("conn:" + e.getMessage());
}
%>
java代码:
public class DBmanage {
public Connection con=null;
public Statement stmt=null;
public ResultSet rs=null;
private static String dbDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String dburl="jdbc:sqlserver://localhost:1433;DatabaseName=student";
private static String dbname="sa";
private static String dbpassword="12345";
public static Connection getConnection(){
Connection conn=null;
try {
Class.forName(dbDriver);
conn=DriverManager.getConnection(dburl, dbname, dbpassword);
System.out.println("连接成功");
} catch (Exception e) {
// TODO: handle exception
System.err.println("连接失败");
}
return conn;
}
public ResultSet doQuery(String sql){
try {
con=DBmanage.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
System.out.println("连接失败"+e.getMessage());
}
return rs;
}
报错:
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 21
18: try {
19: DBmanage DB = new DBmanage();
20: String sql = "select top 3 * from admin";
21: ResultSet rs = DB.doQuery(sql);
22: %>
23: <meta http-equiv="pragma" content="no-cache">
24: <meta http-equiv="cache-control" content="no-cache">
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:588)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
root cause
java.lang.NullPointerException
db.DBmanage.doQuery(DBmanage.java:32)
org.apache.jsp.index_jsp._jspService(index_jsp.java:140)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 展开
1个回答
2017-04-03
展开全部
连接数据库成功了么
检查 DBmanage的代码
~~~~~~~~~~
检查 DBmanage的代码
~~~~~~~~~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |