q763497695
q763497695
2016-12-27 09:19

关于jsp空指针异常,下面是全部代码

5
  • exception
  • java
  • 数据库
  • 异常
  • 指针

在debug里出错的地方已经注释标明,在这里谢谢大家了,在线等,很急的,有什么可以问,马上回!谢谢指导!

 package yang;
import java.sql.*;
public class JDBConnection {
    private final String dbDrive="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private final String url="jdbc:sqlserver://localhost:1433;DatabastName=yang";
    private final String userName="sa";
    private final String password="*******";
    private Connection con=null;
    public JDBConnection(){                                 //构造方法加载驱动
        try {
            Class.forName(dbDrive).newInstance();
        } catch (Exception ex) {
            System.out.println("数据库驱动加载失败:"+ex);
        }
    }
    public boolean creatConnection(){                       //数据库连接
        try {
            con = DriverManager.getConnection(url,userName,password);
            con.setAutoCommit(true);
        } catch (SQLException e) {
            System.out.println("数据库连接失败:"+e);
        }
        return true;
    }
    public boolean executeUpdate(String sql){               //对数据表增,删,改
        if(con==null){
            creatConnection();
        }
        try {
            Statement stmt = con.createStatement();
            int iCount = stmt.executeUpdate(sql);
            System.out.println("操作成功,所影响的记录数为"+ String.valueOf(iCount));
            return true;
        } catch (SQLException e) {
            return false;
        }       
    }
    public ResultSet executeQuery(String sql){              //对数据表查询
        ResultSet rs=null;                                               //在debug里是这里出错
        try {
            if(con==null){
                creatConnection();
            }
            Statement stmt = con.createStatement();
             rs = stmt.executeQuery(sql);
            return null;
        } catch (SQLException e) {
            //e.printStackTrace();

        }
        return rs ;

    }
    public void closeConnection(){                          //关闭数据库连接
        if(con==null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

下面是jsp代码

 <%@page import="java.sql.ResultSet"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  <jsp:directive.page import="yang.JDBConnection"/>
   <%
    JDBConnection connection = new JDBConnection();
    String sql = "use yang select*from users";
    ResultSet rs = connection.executeQuery(sql);
   %>
  <body>
    <table>
    <!-- 以下代码显示用户信息 -->
    <tr>
            <td>id号</td><td>姓名</td>
            <td>性别</td><td>生日</td>
            <td>籍贯</td><td>电话</td>
        </tr>

    <%
            try{
        while(rs.next()){
        %>
        <tr>
            <td><%=rs.getInt(1)%></td><td><%=rs.getString(2)%></td>
            <td><%=rs.getString(3)%></td><td><%=rs.getString(4)%></td>
            <td><%=rs.getString(5)%></td><td><%=rs.getString(6)%></td>
        </tr>
    <%
    }}catch(Exception e){
    e.printStackTrace();
    }
    finally{
    connection.closeConnection();
    } 
    %>
    </table>
    <form action="addUsers.jsp" id="form1" name="form1" method = "post">
        <table>
            <tr>
                <td>姓名:</td>
                <td><input name="name" type="text" id="name" size="20"/></td>
                <td>生日:</td>
                <td><input name="birthday" type="text" id="birthday" size="11"/></td>
            </tr>
            <tr>
                <td>性别:</td>
                <td><input name="sex" type="radio" value="男" checked="checked"/>
                    男<input name="sex" type="radio" value="女"/>女</td>
                <td>电话:</td>
                <td><input name="tel" type="text" id="tel" size="11"/></td>
            </tr>
            <tr>
                <td>籍贯:</td>
                <td><input name="address" type="text" id="address"/></td>
                <td>&nbsp;</td>
            </tr>
        </table>
            <input type="submit" name="Submit" value="增加"\>
            <input type="reset" name="Submit2" value="重置"/>
    </form>
  </body>
</html>

这个是异常信息

 java.lang.NullPointerException
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:114)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:744)

图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

6条回答

为你推荐

换一换