程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 數據庫-關於jsp空指針異常,下面是全部代碼

數據庫-關於jsp空指針異常,下面是全部代碼

編輯:編程解疑
關於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)

圖片說明

最佳回答:


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();
                } 
            }
%>
wheat235
wheat235
q763497695
q763497695
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved