q763497695 2016-12-27 01:19 采纳率: 100%
浏览 1682
已采纳

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

在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条回答 默认 最新

  • wheat235 2016-12-27 02:34
    关注

    public ResultSet executeQuery(String sql) {

    ResultSet rs=null;

    try {
    if(con==null){
    creatConnection();
    }
    Statement stmt = con.createStatement();
    rs = stmt.executeQuery(sql);
    } catch (SQLException e) {
    //e.printStackTrace();

        }
        return rs ;
    
    }
    
    
    
    
            <%
                        if (null != rs) {
                            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();
                    } 
                }
    %>
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部