【问题标题】:database try-catch block code does not execute数据库 try-catch 块代码不执行
【发布时间】:2019-07-30 09:57:59
【问题描述】:

我想通过 try-catch 方法连接到 MySQL 数据库。但是我的 Eclipse 控制台继续显示我的 catch 块中的“GOT AN ERROR”。请帮助我解决这个问题,因为我是 Java 和异常处理的新手。以下是我的代码。

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");
        }
     }
}  

提前致谢。

【问题讨论】:

  • 致电e.printStackTrace() 以获取有关异常的更多详细信息(或至少打印e.getMessage())。此外,不确定在执行查询之前关闭语句是否是个好主意。
  • 将此System.out.println("Got an ERROR: "+ e.getMessage); 和下一行e.printStackTrace() 添加到您的catch 块中。这将为您提供错误原因和堆栈跟踪。然后你会看到出了什么问题。
  • 所有那些 PreparedStatement 关闭都会导致问题。尝试关闭 PreparedStatement 很好,但最好将关闭调用移动到 finally 块中,或者最好仍然使用 try-with-resources 语句

标签: java mysql exception try-catch


【解决方案1】:

要找出调用你的 catch 子句的数据库访问失败,试试这样的代码。

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;
}

您需要生成异常的行号,printStackTrace() 会显示给您。或者,在 Eclipse 的调试器中,在异常处理程序处设置断点并检查异常。

您的代码显示了这一点:

            PreparedStatement pstmt = null;
            pstmt.close();

第二行会产生异常,因为没有对象就不能调用方法。

专业提示:在现实世界的许多场景中,无法完成连接与无法完成INSERT 查询或其他查询有很大不同。连接问题通常意味着数据库服务器已关闭或您的网络工作不正常。查询问题通常意味着您的程序或数据库逻辑中存在某种问题。您可以选择使用两个 try/catch 块,一个用于连接,另一个用于查询。

专业提示:始终缩进代码以正确显示嵌套。 Eclipse(和大多数 IDE)会为您执行此操作。

【讨论】:

  • 我按照您所说的步骤进行操作,现在我可以看到问题出在哪里了。它显示 ParseException。用户输入的日期是无法解析的。我无法在 doPost 方法中抛出 ParseException 。我现在该怎么办?
  • 这应该是另一个问题的主题了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多