JSP - 服务器遇到内部错误,导致无法完成此请求

时间:2016-07-21 17:52:49

标签: java mysql sql database jsp

我在第52行收到错误,我不知道发生了什么。我已安装MySQL驱动程序,并且Actor类中的数据库连接设置正常。我只是在学习JSP而且我无法自己解决它。谢谢你的帮助。

日志:

type Exception report

message An exception occurred processing JSP page /index.jsp at line 52

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 52

49:         %>
50:         <% 
51:             Actor actor = new Actor();
52:             ResultSet actors = actor.getActors();
53:             %>
54:     </body>
55: </html>


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
org.apache.jsp.index_jsp$Actor.getActors(index_jsp.java:48)
org.apache.jsp.index_jsp._jspService(index_jsp.java:166)
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)

代码:

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Selecting Data</title>
    </head>
    <body>
        <h1>Selecting Data</h1>
         <%!
            public class Actor {
        String URL = "jdbc:mysql://localhost:8080/sakila";
        String USERNAME = "root";
        String PASSWORD = "blabla";

        Connection connection = null;
        PreparedStatement selectActors = null;
        ResultSet resultSet = null;

        public Actor() {

            try {

                connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

                selectActors = connection.prepareStatement(
                        "SELECT actor_id, first_name, last_name FROM actor");

            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        public ResultSet getActors() {

            try {
                resultSet = selectActors.executeQuery();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return resultSet;
        }

    }
    %>
    <% 
        Actor actor = new Actor();
        ResultSet actors = actor.getActors();
        %>
</body>

0 个答案:

没有答案