1

I have an Employee management application that can perform operations on tables in the database. My problem is when i try to delete a parent record (department record) it doesn't delete it because the department has employees in it , but it doesn't also notify the user . i want a way to handle this , a popup message or print exception description on the screen , something like that.

it maybe an easy problem but am new to programming , can you please help .

Thanks in advance this is the deleteMethod from DepartmentDao.java :

public void deleteDepartment(int deptno) {
    try {
        PreparedStatement preparedStatement = connection.prepareStatement("delete from dept where deptno=?");
        preparedStatement.setInt(1, deptno);
        preparedStatement.executeUpdate();

    } catch (SQLException e) {

    }// catch
} // deleteDepartment

this is the DepartmentController.java :

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String forward = "";
    String action = request.getParameter("action");
    if (action.equalsIgnoreCase("insert")) {
        forward = ADD_DEPARTMENT;
    } else if (action.equalsIgnoreCase("delete")) {
        int deptNo = Integer.parseInt(request.getParameter("deptno").trim());
        deptDao.deleteDepartment(deptNo);
        forward = DEPARTMENTS;

here's the DepartmentUpdate.jsp

<!DOCTYPE html>
<%@ page contentType="text/html;charset=windows-1256"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1256"/>
            <link rel="stylesheet" type="text/css" href="resources/css/styles.css" /> 
    </head>
    <body>
     <form method="POST" action='departmentcontroller' name="frmAddUser">
       <input type="hidden" name="theAction" value="update"> 
    <table align="center">
    <tr><th colspan="2">Update Department</th></tr>
    <tr><td>Department Number : </td><td><input type="text" name ="deptno" value="<%=request.getAttribute("deptno")%>" disabled="disabled"></td></tr>
     <tr><td>Department Name</td><td><input type="text" name="dname" value="<%=request.getAttribute("dname")%>"></td></tr>
      <tr><td>Location</td><td><input type="text" name="loc"></td></tr>
        <tr><td colspan="2"> <input type="submit" name="updateDepartment" value="Update"></td></tr>
      </table>
      </form>
    </body>
</html>
Reemi
  • 21
  • 3

2 Answers2

0

You have to use try..catch block to handle SQLException as below.

} catch (SQLException e) {
    response.sendError(422,"Department cannot be deleted. Because the department has employees in it. ");
}

OR

Redirect to a error page you created

}
catch(SQLException e) {
    response.sendRedirect("error.jsp?e="+URLEncoder.encode("Department cannot be deleted. Because the department has employees in it.", "UTF-8"))
}

and in the error.jsp file, display it with

<%= request.getParameter("e") %>
Thanga
  • 7,811
  • 3
  • 19
  • 38
  • am using try and catch(SQLException) , the point is i don't know how to inform the client that there was an exception on the server side. – Reemi Feb 23 '16 at 09:16
0

As far I understood the problem you want to notify the user of the exception occurred at server side. *As other mentioned you should not swallow the exception after catching it. After catching the exception check for the message by **e.getMessage() if it is similar to parent record existing(or whatever you are getting that) then you can do one of the following***

  1. send a flag of exception occurred during processing and the error message also from your JDBC helper to Service class to presentation layer.

  2. create a custom exception and throw the exception from the catch block of the jdbc helper. Your custom exception should have the message and display that message on your jsp.

  3. You can also have generic error page and can redirect to there (But it will not be suitable in this situation).

Reference for creating your custom exception - How can I write custom Exceptions?

Community
  • 1
  • 1
Narendra Jaggi
  • 1,297
  • 11
  • 33