ajax实现异步刷新删除table中的一行数据

<!-- ajax异步刷新删除用户所在行的前台 -->

     function deleteAccount(aid)//传入要删除的ID
     {
         var xhr=createXHR();  
            xhr.onreadystatechange=function()  
            {  
                if(xhr.readyState==4)  
                {  
                    if(xhr.status>=200&&xhr.status<300||xhr.status==304)  
                    {  
                        //alert(xhr.responseText);  
                        doDeleteSuccess(aid);  
                    }  
                }  
            } 
            xhr.open("get","DeleteAccountServlet?aid="+aid,true);  
            xhr.send(null); 
     }


//
     function createXHR()  
        {  
            if(window.XMLHttpRequest)  
            {  
                return new XMLHttpRequest();  
            }  
            else if(window.ActiveXObject)  
            {  
                return new ActiveXObject("Microsoft.XMLHTTP");  
            }  
        }
        //js删除已经删除的行
        function doDeleteSuccess(aid)  
        {  
             var btn=document.getElementById(aid);  
             //获取要删除的节点,此处为button所在的<tr></tr>标签  
             var whoToDel=btn.parentNode;  
             whoToDel.parentNode.removeChild(whoToDel);  
        }  


//后台Java删除代码servlet

public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  request.setCharacterEncoding("UTF-8");
  response.setCharacterEncoding("UTF-8");
  String aid_temp = request.getParameter("aid");
  int aid = Integer.parseInt(aid_temp.trim());
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  String otime = sdf.format(new Date());
  String otype="删除";
  String otext = "删除用户";
  String aname = "admin";
  AccountDao accountdao = new AccountDaoImpl();
  accountdao.deleteAccountByAid(aid, aname, otype, otext, otime);
  request.getRequestDispatcher("/index.jsp").forward(request, response);
 }

//dao层

@Override
 public void deleteAccountByAid(int aid,String aname,String otype,String otext,String otime)
 {
  try
  {
   QueryRunner queryrunner = new QueryRunner(JDBCUtil.getDataSource());
   String sql = " delete from account where aid = ?; ";
   queryrunner.update(sql,aid);
   
   String sql2 = "insert into operatelog(aname,otype,otext,otime) values(?,?,?,?); ";
   queryrunner.update(sql2, aname,otype,otext,otime);
  }
  catch(Exception e)
  {
   e.printStackTrace();
   throw new RuntimeException("数据库连接失败-->deleteAccountByAid()");
  }
 }

pojo层

public class Account {
 
 private int aid;
 private String aname;
 private String apass;
 private String aphoto;
 private int asex;
 private int aage;
 private String aphone;
 private String addr;
 private String aemail;
 private int did;
 private int rid;
 public int getAid() {
  return aid;
 }
 public void setAid(int aid) {
  this.aid = aid;
 }
 public String getAname() {
  return aname;
 }
 public void setAname(String aname) {
  this.aname = aname;
 }
 public String getApass() {
  return apass;
 }
 public void setApass(String apass) {
  this.apass = apass;
 }
 public String getAphoto() {
  return aphoto;
 }
 public void setAphoto(String aphoto) {
  this.aphoto = aphoto;
 }
 public int getAsex() {
  return asex;
 }
 public void setAsex(int asex) {
  this.asex = asex;
 }
 public int getAage() {
  return aage;
 }
 public void setAage(int aage) {
  this.aage = aage;
 }
 public String getAphone() {
  return aphone;
 }
 public void setAphone(String aphone) {
  this.aphone = aphone;
 }
 public String getAddr() {
  return addr;
 }
 public void setAddr(String addr) {
  this.addr = addr;
 }
 public String getAemail() {
  return aemail;
 }
 public void setAemail(String aemail) {
  this.aemail = aemail;
 }
 public int getDid() {
  return did;
 }
 public void setDid(int did) {
  this.did = did;
 }
 public int getRid() {
  return rid;
 }
 public void setRid(int rid) {
  this.rid = rid;
 }
 
 
}

//数据库连接层

public class JDBCUtil {
 
 private static ComboPooledDataSource cpds = new ComboPooledDataSource("root");
 
 public static Connection getConnection() throws Exception
 {
  return cpds.getConnection();
 }
 
 public static DataSource getDataSource()
 {
  return cpds;
 }
}

猜你喜欢

转载自blog.csdn.net/qq_29777207/article/details/70473587
今日推荐