Reputation: 3
I have a jsp web page. I need to delete the username from database when i press the delete button from the web page. I tried the codes below but I cant delete the name, instead it insert a new row and an empty value in the username database.
Here's my codes
UnblockServlet.java :
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub
String username = request.getParameter("username");
System.out.println("username="+username);
CustomerInfor unblockUser = new CustomerInfor();
unblockUser.setUsername(username);
CustomerInforDBAO unblockuserdb = new CustomerInforDBAO();
String status = unblockuserdb.blockUser(unblockUser);
System.out.println("Deleted user from blocked:" + status);
if (status== "success"){
dispatch(request,response,"/success.jsp");
}
else{
dispatch(request,response,"/error.jsp");
}
}
CustomerInforDBAO.java :
public String unblockUser(CustomerInfor unblockUser){
Connection con = getConnection(true);
PreparedStatement stmt = null;
int status=1;
String select = "DELETE * FROM paybuddy.blocked where username=?";
try{
stmt = con.prepareStatement(select);
stmt.setInt(1,unblockUser.getId());
stmt.setString(2,unblockUser.getUsername());
status = stmt.executeUpdate();
} catch (Exception e){
e.printStackTrace();
unblockUser = null;
} finally {
try{
if (stmt != null) stmt.close();
if (con != null) con.close();
}catch (SQLException e) {}
}
if(status!=1)
return "error";
else
return "success";
}
Upvotes: 0
Views: 187
Reputation: 66
public String unblockUser(CustomerInfor unblockUser){
Connection con = getConnection(true);
PreparedStatement stmt = null;
int status=1;
String select = "DELETE FROM paybuddy.blocked where username=?"; // no *
try{
stmt = con.prepareStatement(select);
//stmt.setInt(1,unblockUser.getId()); is this needed?
stmt.setString(1,unblockUser.getUsername());
status = stmt.executeUpdate();
} catch (Exception e){
e.printStackTrace();
unblockUser = null;
} finally {
try{
if (stmt != null) stmt.close();
if (con != null) con.close();
}catch (SQLException e) {}
}
if(status!=1)
return "error";
else
return "success";
}
and check if ur calling the proper function above.
String status = unblockuserdb.blockUser(unblockUser);
should it be
String status = unblockuserdb.unblockUser(unblockUser);
Upvotes: 2