Java刪除表
這里頁面我用了layui 框架做刪除的,這里需要引入layui 的css 與js 插件,這里寫出主要的代碼。
Jsp :
//監聽行工具事件table.on('tool(test)', function(obj){var data = obj.data; //獲得當前行數據var date1 = data.classifyColumnID;if(obj.event === 'del'){//刪除layer.confirm("確認刪除嗎?", {icon: 3,btn: ['確定', '取消']}, function (index) {layer.close(index); $.post("servlet/SiteServletfun=deleteclassification&ClassifyColumnID="+date1, function(jsonObject) {data = JSON.parse(jsonObject);if (data.state == true) {layer.alert(data.msg,{icon:1,title:'提示',offset:'100px',btn: ['確定','取消']},function(index){layer.close(index); location.reload();//頁面刷新}); }else{layer.alert(data.msg,{icon:2,title:'提示',offset:'100px',btn: ['確定','取消']},function(index){layer.close(index); location.reload();//頁面刷新});};}); });
}
Servlet :
public void deleteclassification(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {JsonReturn jsonReturn = new JsonReturn();int ClassifyColumnID = Integer.parseInt(request.getParameter("ClassifyColumnID"));String str = iSite.deleteclassification(ClassifyColumnID);if ("刪除成功".equals(str)) {jsonReturn.setState(true);jsonReturn.setMsg(str);} else {jsonReturn.setState(false);jsonReturn.setMsg(str);}JSONObject jsonObject = JSONObject.fromObject(jsonReturn);PrintWriter out = response.getWriter();out.write(jsonObject.toString());out.flush();out.close(); }
Dao 實現類:
@Overridepublic int deleteclassification(int ClassifyColumnID) {int flog=0;try {con = DbUtil.getConnection();ps = con.prepareStatement("DELETE FROM jc_bas_classifycolumn WHERE ClassifyColumnID = ?");ps.setInt(1, ClassifyColumnID);ff = ps.executeUpdate();} catch (SQLException e) {e.printStackTrace();}finally{DbUtil.close(con, ps, rs); }return flog;}
Service 實現類:
@Overridepublic String deleteclassification(int ClassifyColumnID) {String str = null;if (userSite.deleteclassification(ClassifyColumnID)>0) {str= "刪除成功";} else {str= "刪除失敗";}return str;}