java - JSP Delete Row from MySQL -
i generated list of people in jsp using mysql , problem how should delete them. code:
jsp:
<% applicantdao applicantdao = new applicantdao();%> <% (int = 0; < applicantdao.viewapplicant().size(); i++) {%> <div class="column"> <div class="col-sm-3 col-xs-4"> <div class="list-group"> <a class="list-group-item active"> <img src = "th_1x1.jpg" class = "img-responsive" alt = "responsive image" width = "100%" height ="100"> <h4 class="list-group-item-heading" id="guardname<%=+i%>" id="guardname<%=+i%>"><%=applicantdao.viewapplicant().get(i).getapplicantfirstname() + " "%> <%=applicantdao.viewapplicant().get(i).getapplicantlastname()%></h4> </a> <a class="list-group-item"> <p class="list-group-item-text" id="applyingfor<%=+i%>" id="applyingfor<%=+i%>"><%=applicantdao.viewapplicant().get(i).getapplyingfor()%></p> </a> <a class="list-group-item" data-toggle="modal" href="#moredetails<%=+i%>"> <button class="btn btn-primary btn-lg btn-block" id="moredetails">more details</button> </a> <a class="list-group-item"> <form action="deleteapplicant" action="post"> <button type="button" class="btn btn-default delete" aria-label="left align" id="accept<%=applicantdao.viewapplicant().get(i).getapplicantid()%>"> <span class="glyphicon glyphicon-ok" aria-hidden="true"></span> </button> </form> <form> <button type="button" class="btn btn-default delete" aria-label="left align" id="reject<%=applicantdao.viewapplicant().get(i).getapplicantid()%>"> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> </button> </form> </a> <div class="modal fade" id="moredetails<%=+i%>"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h3 class="modal-title">applicant information</h3> </div> <div class="modal-body"> <h2 class="text-center"><%=applicantdao.viewapplicant().get(i).getapplicantlastname() + ", "%><%=applicantdao.viewapplicant().get(i).getapplicantfirstname()%></h2> <h4 class="text-center" id="id"><%=applicantdao.viewapplicant().get(i).getapplicantid()%></h4>
the following code generates people using loop made. however, problem getting id of going delete. code of servlet:
try (printwriter out = response.getwriter()) { /* todo output page here. may use following sample code. */ try { applicant deletedapplicant = new applicant(); string value = request.getparameter("id"); int id = integer.parseint(value); applicantdao applicantdao = new applicantdao(); boolean successful = applicantdao.rejectapplicant(deletedapplicant.getapplicantid()); if (successful){ servletcontext context= getservletcontext(); requestdispatcher rd= context.getrequestdispatcher("/hiringpage.jsp"); httpsession session = request.getsession(); session.setattribute("deletedapplicant", deletedapplicant); rd.forward(request, response); system.out.println("successful"); }else{ servletcontext context= getservletcontext(); requestdispatcher rd= context.getrequestdispatcher("/dashboard.jsp"); rd.forward(request, response); } } { out.close(); } }
and lastly, code of controller:
public boolean rejectapplicant(int applicantid) { try { dbconnectionfactory myfactory = dbconnectionfactory.getinstance(); connection conn = myfactory.getconnection(); string query = "delete applicant applicantid = ?"; preparedstatement pstmt = conn.preparestatement(query); pstmt.setint(1, applicantid); int rows = pstmt.executeupdate(); conn.close(); pstmt.close(); return true; } catch (sqlexception ex) { logger.getlogger(applicantdao.class.getname()).log(level.severe, null, ex); } return false; }
can please guide me on how things? thank much!
your problem in part of code
<form action="deleteapplicant" action="post"> <button type="button" class="btn btn-default delete" aria-label="left align" id="accept<%=applicantdao.viewapplicant().get(i).getapplicantid()%>"> <span class="glyphicon glyphicon-ok" aria-hidden="true"></span> </button> </form> <form> <button type="button" class="btn btn-default delete" aria-label="left align" id="reject<%=applicantdao.viewapplicant().get(i).getapplicantid()%>"> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> </button> </form>
i don't understand why did put 2 forms put servlet in first suggest add hidden input in form put id
<form action="deleteapplicant" action="post"> <input type="hidden" name="id" value="<%=applicantdao.viewapplicant().get(i).getapplicantid()%>"/> <button type="submit" class="btn btn-default delete" aria-label="left align" id="accept<%=applicantdao.viewapplicant().get(i).getapplicantid()%>"> <span class="glyphicon glyphicon-ok" aria-hidden="true"></span> </button> </form>
you can id value using servlet
string value = request.getparameter("id");
you should know second form put
<form> <button type="button" class="btn btn-default delete" aria-label="left align" id="reject<%=applicantdao.viewapplicant().get(i).getapplicantid()%>"> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> </button> </form>
doesn't pass parameter servlet
so change
<form action="deleteapplicant" action="post"> <input type="hidden" name="id" value="<%=applicantdao.viewapplicant().get(i).getapplicantid()%>"/> <button type="submit" class="btn btn-default delete" aria-label="left align" id="reject<%=applicantdao.viewapplicant().get(i).getapplicantid()%>"> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> </button> </form>
after edit :
why did pass
boolean successful = applicantdao.rejectapplicant(deletedapplicant.getapplicantid());
simply pass
boolean successful = applicantdao.rejectapplicant(id);
Comments
Post a Comment