Servlet代码错误:http 500内部服务器错误

时间:2017-04-08 03:17:17

标签: java mysql servlets jdbc

我试图运行以下代码,但它总是导致" http 500内部服务器错误"

有人可以帮我调试此错误

我刚开始学习Servlets和JSP ..如果我错过了问题中的任何细节,请原谅。查看MYSQL数据库错误日志,我找到了以下条目:

  

中止与db的连接44:' sakila'用户:' root'主持人:' localhost' (读取通信包时出错)

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();
    final String DB_URL="jdbc:mysql://localhost:3306/sakila";
    final String user="root";
    final String password="pass1234";
    Statement stmt=null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        out.println("Cannot load driver");
    }
    Connection conn=null;

    try {
        conn=DriverManager.getConnection(DB_URL,user,password);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        out.println("Cannot Connect to Database");
    }

    //out.print("Connected to Database");
    out.println("<html>");
    out.println("<body>");
    String str= "SELECT actor_id, first_name last_name FROM temp where actor_id='1';";

    try {
        ResultSet rs=stmt.executeQuery(str);
        while(rs.next()){
            out.println(rs.getString("actor_id"));

        }

    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }       
    /*
    try {
        ResultSet rs;
        rs = stmt.executeQuery(str);
        while(rs.next()){
            int i=rs.getInt("actor_id");
            String fn= rs.getString("first_name");
            String ln=rs.getString("last_name");

            out.print(i+"::");
            out.print(fn+"::");
            out.print(ln+"::");
        }
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    */
    out.print("hkshfdkhfakfshdkha");




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

    out.println("</body>");
    out.println("</html>");


}

2 个答案:

答案 0 :(得分:2)

如果您检查应用程序日志,我会假设您将看到由

生成的堆栈跟踪
e1.printStackTrace();

这是因为你的SQL语法有错误。

SELECT actor_id, first_name last_name FROM temp where actor_id='1';
                          /\ add missing comma

在旁注 - 您不应该在每个请求中建立与数据库的连接。这会减慢一切。

相反,您应该使用连接池,我建议C3P0

数据库日志突然终止的原因是,您的应用程序抛出异常并放弃连接而未正确关闭它。

答案 1 :(得分:2)

  

中止与db的连接44:&#39; sakila&#39;用户:&#39; root&#39;主持人:&#39; localhost&#39;   (读取通信包时出错)

此错误跟踪出现在您的控制台中,因为您尝试在每个doGet()请求上建立与mysql数据库的新数据库连接,而不正确关闭数据库连接。

这就是为什么每当发生通信错误时,它会增加Aborted_clients或Aborted_connects的状态计数器,它会描述由于客户端在没有正确关闭连接而死亡的情况下中止的连接数以及连接到MySQL的失败尝试次数服务器(分别)。

出于导致此问题的各种原因,以下是您可能想要检查的一些重要原因。

  • 客户端连接成功但终止不正确(可能 与未正确关闭连接有关)
  • 客户端睡眠时间超过定义的wait_timeout或 interactive_timeout秒(最终导致连接到 睡眠等待wait_timeout秒,然后强行连接 由MySQL服务器关闭)
  • 客户端异常终止或超过max_allowed_pa​​cket 查询

正如@Matt Clark提到的那样,你应该采用连接池机制来避免这个问题,并且还要遵循与数据库接口的最佳实践。