简体   繁体   中英

How to do edit in mysql using JSP page?

This is EmployeeDao.java file.

public Employee getEmployeeById(String employeeId){
    System.out.println("IN getEmployeeById");
    Employee employee = new Employee();
    try {
        PreparedStatement preparedStatement = conn.prepareStatement("select * from login where email=?");
        preparedStatement.setString(1, employeeId);
        ResultSet rs = preparedStatement.executeQuery();
        System.out.println(rs);
        if(rs.next()){
            employee.setFirstname(rs.getString("firstName"));
            System.out.println(""+employee.getFirstname());
            employee.setLastname(rs.getString("lastName"));
            System.out.println(""+employee.getLastname());
            employee.setEmail(rs.getString("email"));
            System.out.println(""+employee.getEmail());
            employee.setStatus(rs.getBoolean("status"));
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return employee;    
}

This is the EmployeeController.java file.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String forward = "";
    String action = request.getParameter("action");

    if(action.equalsIgnoreCase("delete")){
        String employeeId = request.getParameter("employeeId");
        dao.deleteEmployee(employeeId);
        forward = employee_listing ;
        request.setAttribute("employees", dao.getAllEmployees());
    }else if(action.equalsIgnoreCase("edit")){
        forward = employee_detail ;
        String employeeId = request.getParameter("employeeId");
        Employee employee = dao.getEmployeeById(employeeId);
        request.setAttribute("employee", employee);
    }else if(action.equalsIgnoreCase("listEmployee")){
        forward = employee_listing;
        request.setAttribute("employees", dao.getAllEmployees());
    }
    else{
        forward = dashboard;
    }

    RequestDispatcher view  = request.getRequestDispatcher(forward);
    view.forward(request, response);
}

This is the employee-detail page.

<form class="form-horizontal" method="post" action="EmployeeController" name="frmAddEmployee">  
                                <% 
                                    String action = request.getParameter("action");
                                    System.out.println(action);
                                %>
                                    <% while (rs.next()) { %>

                                <div class="form-group">
                                    <label for="" class="col-sm-4 control-label">First Name</label>
                                    <div class="col-sm-8">
                                      <input type="text" class="form-control" id="" value="<%= rs.getString("firstName") %>" >
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="" class="col-sm-4 control-label">Last Name</label>
                                    <div class="col-sm-8">
                                      <input type="text" class="form-control" id="" value="<%= rs.getString("lastName") %>"  >
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="" class="col-sm-4 control-label">Email</label>
                                    <div class="col-sm-8">
                                      <input type="email" class="form-control" id="" value="<%= rs.getString("email") %>" >
                                      <% } %>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">Employee Status</label>
                                    <div class="col-sm-8">
                                        <label class="switch">
                                            <input type="checkbox" checked>
                                            <div class="slider round"></div>
                                        </label>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-4"></div>
                                    <div class="col-sm-8">
                                        <input type="submit" name="submit" value="Update" class="btn btn-primary"> &nbsp; <input type="reset" name="cancel" value="Cancel" class="btn btn-primary">
                                    </div>
                                </div>
                            </form> 

So when I clicked on edit button it will be redirect to employee-detail page and I got the specific employee detail.

But after edit that particular employee detail and clicked on "update" button, I got the white screen and nothing else. And that page url is :: " http://localhost:8000/SampleLogin/EmployeeController "

So How can I solved this and move on.

All you need to do is replace

<input type="submit" name="submit" value="Update" class="btn btn-primary">

with : < input type="submit" name="action" value="edit" class="btn btn-primary">

First of all - do not use doGet for update data.

Your form method is POST :

<form class="form-horizontal" method="post" ...>

So you need to do all the update magick inside doPost method of your controller.

Here are some useful tips about CRUD with servlets - https://stackoverflow.com/tags/servlets/info

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