0

Here is my code. Connection to database is working. When the statement posts = s.executeQuery(query); is executed it is not even entering into loop and executing from finally. unfortunately there is no exception thrown.

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
            + "Transitional//EN\">\n";
    String title = "Trend Blog";
    out.println(docType + "<HTML>\n" + "<HEAD><TITLE>" + title
            + "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n"
            + "<H1 ALIGN=CENTER>" + title + "</H1>\n");
    Connection connection = null;
    ResultSet posts = null;
    ConnectMySql connectMySql = new ConnectMySql();
    String query = "select title,post from posts order by desc number";
    try {
        connection = connectMySql.getConnection("jdbc");
        Statement s = connection.createStatement();
        posts = s.executeQuery(query);
        int i = 1;
        while (posts.next()) {
            String postTitle = posts.getString(1);
            String post = posts.getString(2);

            HttpSession session = request.getSession();
            out.println("<h4>Welcome " + session.getAttribute("username")
                    + "</h4>");
            out.println("<p>");
            out.print("<h3><strong>" + i + "." + postTitle
                    + "</strong></h3>");
            out.println("<p>" + post + "</p>");
            out.print("</p><hr>");
            out.print("</body></html>");

        }

    } catch (SQLException e) {

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

}
Lucky
  • 106
  • 3
  • 9

1 Answers1

1

You are catching first exception block, however you are not printing to see what exception it has thrown.

So in your first exception block do as

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

    } finally {
......

In second catch, it wouldn't have thrown any exception if an exception might have occcured in first try-catch block.

Jacob
  • 14,463
  • 65
  • 207
  • 320
  • thank you for your reply. I identified the problem. It was to place desc after number in query. – Lucky Sep 17 '14 at 05:43