conn_db.jsp<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*" %>
<jsp:useBean id="connDbBean" scope="page" class="student.conn"></jsp:useBean>
<html>
  <head>
    <title>text db connection</title>
  </head>
  
  <body>
   <tr>
   <td width=50%>
   <div align="center"><b>name</b></div>
   </td>
   <td width=25%>
   <div align="center"><b>sex</b></div>
   </td>
   <td width=25%>
   <div align="center"><b>score</b></div>
   </td>
   </tr>
   <%
   ResultSet RS_result = connDbBean.executeQuery("select * from table1");
   String studentName="";
   String studentSex="";
   int studentScore=0;
   while (RS_result.next())
   {
   studentName = RS_result.getString("name");
   studentSex = RS_result.getString("sex");
   studentScore = RS_result.getInt("score");
    %>
     <tr>
     <td width=50%>
     <div align="center"><%=studentName %></div>
     </td>
<td width=25%>
<div align="center"><%=studentSex %></div>
</td>
<td width=25%>
<div align="center"><%=studentScore %></div>
</td>
     </tr>
<%
}
RS_result.close();
 %>
  </body>
</html>
conn.javapackage student;import java.sql.*;public class conn 
{
String sDBDriver = "com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/student?user=root&password=00000000";
Connection connect = null;
ResultSet rs = null;

public conn()
{
try {
Class.forName(sDBDriver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

}
public ResultSet executeQuery(String sql)
{
try
{
connect = DriverManager.getConnection(url);
Statement stmt = connect.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
ex.printStackTrace();
}
return rs;
}
}
给我报错是以下内容:
空指针,看不懂啊!!!exception org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause java.lang.NullPointerException
org.apache.jsp.conn_005fdb_jsp._jspService(conn_005fdb_jsp.java:88)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.求大神解释一下,感觉也可能是数据库弄错了?纠结……

解决方案 »

  1.   

    public ResultSet executeQuery(String sql)
    换这个方法名试下,
      

  2.   

    我没找到你代码中的错误,不知道你从什么地方报出来的空指针。建议你到%TOMCAT_HOME%\work\Catalina\localhost目录下,找到你对应页面的Java文件,确定一下88行对应的是什么,然后比较容易帮你找错误~~// 这是我写的一个数据库连接,而且能打印出信息
    public ResultSetDemo() {
    try {
    Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    }

    public void text() {
    try {
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=root");
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select * from user");
    while (rs.next()) {
    System.out.println("row_data:" + rs.getString("user_name"));
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    System.out.println("OK");
    }
      

  3.   

    org.apache.jsp.conn_005fdb_jsp._jspService(conn_005fdb_jsp.java:88)
    对应代码是  while (RS_result.next())    RS_result 是null
    2种可能 ,1 数据库没连上 stmt 就没成功,   2 数据库脸上了 表不存在  ResultSet rs = stmt.executeQuery("select * from user"); 执行sql 出错 RS_result 也是null