无法在jsp中显示数据库中的数据

时间:2015-12-14 16:17:43

标签: controller crud

在jsp表单中添加数据后,我在jsp中检索列表中的数据。列表根据我的jsp文件更新。但是我无法更新数据,一旦我点击更新按钮,jsp表单就不会显示在之前的jsp文件列表中选择的数据。这是我的控制者。

 This is my list jsp form

<br />
<br />

									<a class="btn btn-success" href="addEmployee2.jsp" ><button><font color='red'>Add New Employee</font></button></a>
		                           <table border="3" class="table table-striped responsive-utilities jambo_table bulk_action">
           <thead>
            <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Phone Number</th>
            <th>Address</th>
            <th>Role</th>
            <th>Action</th>
             </tr>
           </thead>
             
 <%
Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con= DriverManager.getConnection("jdbc:Oracle:thin:@localhost:1521:XE", "hally", "hally");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from EMPLOYEE");
while (rs.next()) {
%>                   
             <tr>
             <td><%= rs.getString(1) %></td>
             <td><%= rs.getString(2) %></td>
             <td><%= rs.getString(3) %></td>
             <td><%= rs.getString(4) %></td>
             <td><%= rs.getString(6) %></td>
             <td><a href="updateEmployee.jsp">Update</a><br/>
                 <button id="del" type="submit" class="btn btn-success">Delete</button><br/>
               
             </td>
             </td>
            </tr>
<% }%> 
            </table>


<br />
<br />

this is my update jsp form


<br />
<br />
<span class="section">Personal Info</span>
                                        
                                        <form class="form-horizontal form-label-left" action="EmployeeController?action=edit" method="post">
											<div class="item form-group">
	                                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Identification Card <span class="required">*</span>
	                                            </label>
	                                            <div class="col-md-6 col-sm-6 col-xs-12">
	                                                <input type="number" class="form-control col-md-7 col-xs-12" name="empID" value="${empDetail.empID}">
	                                            </div>
	                                        </div>
	                                         <div class="item form-group">
                                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Name <span class="required">*</span>
                                            </label>
                                            <div class="col-md-6 col-sm-6 col-xs-12">
                                                <input type="text" class="form-control col-md-7 col-xs-12" name="empName" value="${empDetail.empName}">
                                            </div>
                                        </div>
                                         <div class="item form-group">
                                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="number">Phone Number <span class="required">*</span>
                                            </label>
                                            <div class="col-md-6 col-sm-6 col-xs-12">
                                                <input type="number" id="number" name="empPhoneNum" class="form-control col-md-7 col-xs-12" value="${empDetail.empPhoneNum}">
                                            </div>
                                        </div>
                                         <div class="item form-group">
                                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="textarea">Address <span class="required">*</span>
                                            </label>
                                            <div class="col-md-6 col-sm-6 col-xs-12">
                                                <input type="text"  name="empAddress" class="form-control col-md-7 col-xs-12" value="${empDetail.empAddress}"></textarea>
                                            </div>
                                        </div>
                                        <div class="item form-group">
                                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="textarea">Password <span class="required">*</span>
                                            </label>
                                            <div class="col-md-6 col-sm-6 col-xs-12">
                                                <input type="password"  name="password" class="form-control col-md-7 col-xs-12" value="${empDetail.empPassword}"></textarea>
                                            </div>
                                        </div>
                                        <div class="item form-group">
                                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="textarea">Role <span class="required">*</span>
                                            </label>
                                            <div class="col-md-6 col-sm-6 col-xs-12">
                                                <input type="number"  name="empRole" class="form-control col-md-7 col-xs-12" value="${empDetail.empRole}"></textarea>
                                            </div>
                                        </div>
                                        <div class="item form-group">
                                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="textarea">Level <span class="required">*</span>
                                            </label>
                                            <div class="col-md-6 col-sm-6 col-xs-12">
                                                <input type="text"  name="empLevel" class="form-control col-md-7 col-xs-12" placeholder="e.g. Employee" value="${empDetail.empLevel}"></textarea>
                                            </div>
                                        </div>
                                   			<button id="upd" type="submit" class="btn btn-success">Update</button>
                                   		</form> 
                                   		<form class="form-horizontal form-label-left" action="EmployeeController?action=delete" method="post">
                                   			<input type="hidden" class="form-control col-md-7 col-xs-12" name="empID" value="${empDetail.empID}">
                                   			<button id="del" type="submit" class="btn btn-success">Delete</button>
                                   		</form> 

        @WebServlet("/EmployeeController")
         public class EmployeeController extends HttpServlet {
         private static final long serialVersionUID = 1L;

          private EmployeeDA edao;
           private static String DELETE_DROP = "searchEmployee2.jsp";
          private static String INSERT_OR_EDIT = "searchEmployee2.jsp";

       /**
         * @see HttpServlet#HttpServlet()
         */
       public EmployeeController() {
         super();
        edao = new EmployeeDA();
          // TODO Auto-generated constructor stub
         }

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
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("listCust")){
        System.out.println("listCust triggered");
        forward = "searchCustomer.jsp";
        request.setAttribute("custsBean", cdao.getAllCusts());
    }
    else {
        System.out.println("xxx triggered");
        forward = INSERT_OR_EDIT;
    }
    RequestDispatcher view = request.getRequestDispatcher(forward);
    view.forward(request, response);*/
}

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

    if (action.equalsIgnoreCase("search")){
        try {
            //String forward="pages/ExistCust.jsp";
            String searchString = request.getParameter("searchbox");

            Employee emp = edao.searchEmp(searchString);

            if( emp.getEmpID() == null || emp.getEmpID() == "" ){
                request.setAttribute( "nonExists", "1" );
                RequestDispatcher display = request.getRequestDispatcher("searchEmployee2.jsp");
                display.forward(request, response);
            }
            else{
                request.setAttribute( "nonExists", "2" );
                request.setAttribute( "empDetail", emp );
                System.out.println(emp);
                RequestDispatcher display = request.getRequestDispatcher("searchEmployee2.jsp");
                display.forward(request, response);
            }

        }
        catch (Throwable theException) { 
            System.out.println(theException); 




             else if (action.equalsIgnoreCase("edit")){
            //System.out.println("cust");
            Employee emp = new Employee();
            emp.setEmpID(request.getParameter("empID"));
            emp.setEmpName(request.getParameter("empName"));
            emp.setEmpPhoneNum(request.getParameter("empPhoneNum"));
            emp.setEmpAddress(request.getParameter("empAddress"));
            emp.setPassword(request.getParameter("password"));
            emp.setEmpRole(Integer.parseInt(request.getParameter("empRole")));
            emp.setEmpLevel(request.getParameter("empLevel"));  
            String ic = request.getParameter("empID");
            edao.updateEmp(emp);

            RequestDispatcher view = request.getRequestDispatcher("searchEmployee2.jsp");
            request.setAttribute( "nonExists", "2" );
            request.setAttribute("empDetail", edao.searchEmp(ic));
            request.setAttribute("dispMsg", "Successfully Updated!");
            view.forward(request, response);
        }


        else if (action.equalsIgnoreCase("delete")){
            System.out.println("DELETE");
            String empIc = request.getParameter("empID");
            edao.deleteEmp(empIc);
            //forward = DELETE_DROP;
            //request.setAttribute("custs", cdao.getAllCusts());    
            RequestDispatcher view = request.getRequestDispatcher("searchEmployee2.jsp");
            request.setAttribute( "nonExists", "1" );
            request.setAttribute("dispMsg", "Successfully Deleted!");
            view.forward(request, response);
        }



    }

0 个答案:

没有答案