简体   繁体   中英

database try-catch block code does not execute

I want to connect to the MySQL database through try-catch method. But my eclipse console keeps on showing "GOT AN ERROR" which is in my catch block. Please help me through this as I am new to java and exception handling. Following is my code.

public class MyServlet extends HttpServlet{  

    private static final long serialVersionUID = 1L;        
     protected  void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
        String task;
        String tm;

         task = request.getParameter("task");
         tm= request.getParameter("reminder_time");
        // System.out.println("task = "+task+"time ="+tm);
         try {
             SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
                Date parsedDate = dateFormat.parse(tm);
                java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
            //System.out.println("Time=");
            Connection con =DriverManager.getConnection(  
            "jdbc:mysql://localhost:3306/reminder","xyz","xyz123");  
             String INSERT_RECORD = "insert into ToDo values(?, ?)";
                    PreparedStatement pstmt = null;
                      pstmt.close();
                      pstmt = con.prepareStatement(INSERT_RECORD);
                      pstmt.close();
                      pstmt.setString(1, task);
                      pstmt.close();
                      //pstmt.setDate(2, timestamp);
                      pstmt.close();
                      int rs= pstmt.executeUpdate();

            if(rs!=0){

//              response.sendRedirect("success.html");
//              return;
                PrintWriter out= response.getWriter();
                out.println("Successfull");     
                pstmt.close();
                con.close();

            }
            else{
            //  response.sendRedirect("error.html");
                PrintWriter out= response.getWriter();
                out.println("Failed");
            }

        } 
         catch (Exception e) {
            System.out.println("Got an ERROR");
        }
     }
}  

Thanks in advance.

To find out the database access failure that invoked your catch clause try code like this.

catch (SqlException e) {
    System.out.println("database exception");
    System.out.println(e); 
    e.printStackTrace(); 
}
catch (Exception e)
    System.out.println("unexpected exception");
    System.out.println(e); 
    e.printStackTrace(); 
    throw;
}

You need the line number that generated the exception, and printStackTrace() shows it to you. Or, in Eclipse's debugger, set a breakpoint at your exception handler and examine the exception.

Your code shows this:

            PreparedStatement pstmt = null;
            pstmt.close();

The second of those lines generates an exception, because you cannot call an method without an object.

Pro tip : In many scenarios in the real world, failure to complete a connection is very different from failure to complete an INSERT query or some other query. Connection problems usually mean the database server is shut down or your network is working incorrectly. Query problems often mean you have some kind of problem in your program or database logic. You might choose to use two try/catch blocks, one for the connection and another for the query.

Pro tip : Always indent your code to show nesting correctly. Eclipse (and most IDEs) do this for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM