从数据库中删除名称

时间:2013-01-31 08:04:56

标签: jsp

我有一个jsp网页。当我按下网页上的删除按钮时,我需要从数据库中删除用户名。我尝试了下面的代码,但我不能删除名称,而是在用户名数据库中插入一个新行和一个空值。

这是我的代码

UnblockServlet.java:

protected void doPost(HttpServletRequest request,HttpServletResponse response)抛出ServletException,IOException {     // TODO自动生成的方法存根

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";

        }

1 个答案:

答案 0 :(得分:2)

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";

    }

并检查您是否正在调用上述正确的功能。

String status = unblockuserdb.blockUser(unblockUser);

应该是

String status = unblockuserdb.unblockUser(unblockUser);