java web 一直在报空指针异常,求大神看看 5C

java部分代码(java代码部分已经测试过,单独运行没有问题。):

    package db;

import java.sql.*;

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;
}

public void closeConnection(){
    try {
        if(rs==null){
            rs.close();

        }
    } catch (Exception e) {
        // TODO: handle exception
    }

    try {
        if(stmt==null){
            stmt.close();

        }
    } catch (Exception e) {
        // TODO: handle exception
    }

    try {
        if(con==null){
            con.close();

        }
    } catch (Exception e) {
        // TODO: handle exception
    }
}


public static void main(String[] args) {
    DBmanage cc=new DBmanage();
    String sql01="select * from admin01";
    ResultSet rss=null;
    getConnection();
    rss=cc.doQuery(sql01);
    try {
        while(rss.next()){
            System.out.print(rss.getString("id")+"  ");
            System.out.print(rss.getString("name")+"  ");
            System.out.println(rss.getString("sex"));

        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}
jsp代码部分:
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="db.DBmanage"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">







<%
request.setCharacterEncoding("utf-8");
    while (rs.next()) {

            System.out.print(rs.getString("id") + "  ");
            System.out.print(rs.getString("name") + "  ");
            System.out.println(rs.getString("sex"));

        }
        DB.closeConnection();
    } catch (SQLException e) {
        out.println("conn:" + e.getMessage());
    }
%>


报错部分:

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:
24:

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)

采用纯JDBC方式连接数据库,java部分已经测试通过。

2个回答

没有导入SQL的驱动包吧

关闭资源close()那里,应该是不等于空的时候才关闭吧

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问