标签:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>数据表格</title> <% String pid = request.getParameter("parentid"); if (pid == null || pid.trim().length() == 0) { pid = "0"; } %> <script type="text/javascript" src="js/jquery-easyui-1.4.4/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.4.4/themes/default/easyui.css"> <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.4.4/themes/icon.css"> <script type="text/javascript" src="js/jquery-easyui-1.4.4/jquery.easyui.min.js"></script> <script type="text/javascript" src="js/jquery-easyui-1.4.4/locale/easyui-lang-zh_CN.js"></script> <script > $(function() { $("#mydg").datagrid({ title:‘地区列表‘, collapsible:true, width:500, url:‘MembersList?parentid=<%=pid%>‘, fitColumns:true, striped:true, idField:‘id‘, //跨页选取,不会受到 翻页的影响 pagination:true, rownumbers:true, pageList:[5,10,50], pageSize:5, sortName:‘id‘, //排序依据 id sortOrder:‘desc‘, //排序方式 倒序排列 remoteSort:false, //客户端排序方式 frozenColumns:[[ {field:‘‘,checkbox:true}, {field:‘id‘,width:80,title:‘ID‘}]], columns:[[ {field:‘parentId‘,width:80,title:‘父ID‘}, {field:‘name‘,width:80,title:‘地区名‘, formatter: function(value,row,index){ return ‘<a href=datagrid.jsp?parentid=‘ + row.id + ‘>‘+ value + ‘</a>‘; } }, {field:‘postCode‘,width:180,title:‘邮编‘} ]], toolbar: [{ text:‘增加地区‘, iconCls: ‘icon-add‘, handler: function(){ $("#addform").form(‘reset‘); $("#add").dialog({ title:‘增加地区‘ }); $("#add").dialog(‘open‘); } },‘-‘,{ text:‘编辑地区‘, iconCls: ‘icon-edit‘, handler: function(){ var s = $("#mydg").datagrid(‘getSelected‘); if(s != null) { //编辑数据 $("#addform").form(‘reset‘); //清理ID $("#id").val(null); $("#add").dialog({ title:‘编辑地区‘ }); //绑定数据 $("#addform").form(‘load‘, s); $("#add").dialog(‘open‘); } else { alert(‘请选择一条数据‘); } } },‘-‘,{ text:‘删除地区‘, iconCls: ‘icon-remove‘, handler: function(){ var s = $("#mydg").datagrid(‘getSelections‘); if(s.length > 0) { $.messager.confirm(‘确认‘,‘您确认要删除吗?‘,function(r){ if (r) { var ids = ‘‘; for(var i = 0;i < s.length;i++) { ids += s[i].id; if(i != (s.length - 1)) { ids += ‘,‘; } } // alert(‘ids = ‘ + ids); $.get("DeleteMembers?ids="+ids, function(data,status) { // alert("Data: " + data + "\nStatus: " + status); var mes = eval(‘(‘ + data + ‘)‘); $.messager.show({title:‘提交信息‘, msg:mes.message}); $("#mydg").datagrid(‘reload‘); //清除选择项 $("#mydg").datagrid(‘clearSelections‘); }) } }); } else { alert(‘请选择一条数据‘); } } }] }); $("#addform").form({ novalidate:false, url:‘TestSubmit‘, onSubmit:function() { var isValid = $(this).form(‘validate‘); if (!isValid) { $.messager.show({title:‘表单验证‘, msg:‘验证未通过‘}); } return isValid; },success:function(data){ //解析接收到的数据 var mes = eval(‘(‘ + data + ‘)‘); $.messager.show({title:‘提交信息‘, msg:mes.message}); if (mes.success) { $("#add").dialog(‘close‘); $("#mydg").datagrid(‘reload‘); } else { } } }) $("#subbutton").click(function(){ $("#addform").form(‘submit‘); }); }); </script> </head> <body> <!-- <table id="mydatagrid" title="地区列表" class="easyui-datagrid" style="width:500px;height:200px" data-options="collapsible:true,singleSelect:false,url:‘MembersList?parentid=0‘,method:‘post‘" > <thead> <tr> <th data-options="field:‘id‘,width:80" >ID</th> <th data-options="field:‘parentId‘,width:80" >父ID</th> <th data-options="field:‘name‘,width:80" >地区名</th> <th data-options="field:‘postCode‘,width:180" >邮编</th> </tr> </thead> </table> --> <table id="mydg"></table> <div id="add" class="easyui-dialog" data-options="closed:true" title="增加地区" style="width:300px;"> <form id="addform" method="post" > <table > <tr> <td>地区名称:</td> <td><input id="name" name="name" class="easyui-textbox" data-options="required:true, validType:‘length[2,10]‘,missingMessage:‘这是地区名称,为必填项‘" width="30" ></td> </tr> <tr> <td>邮政编码:</td> <td><input id="postCode" name="postCode" class="easyui-numberbox" data-options="required:false, validType:‘length[6,6]‘" width="30" ></td> </tr> <input name="parentid" type="hidden" value="<%=pid%>" /> <input name="id" id="id" type="hidden" value="" /> <tr> <td colspan=2 align="center"> <a id="subbutton" class="easyui-linkbutton" style="width:80px" >提交</a> </td> </tr> </table> </form> </div> </body> </html>
package com.hanqi; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hanqi.dao.Members; import com.hanqi.dao.MembersDAL; import com.alibaba.fastjson.*; /** * Servlet implementation class JsonMembers */ @WebServlet("/JsonMembers") public class JsonMembers extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public JsonMembers() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String rtn = ""; String pid = request.getParameter("parentid"); if (pid == null || pid.trim().length() == 0 ) { pid = "-1"; } int iPid = Integer.parseInt(pid); MembersDAL md = new MembersDAL(); ArrayList<Members> al; try { al = md.getList(iPid); Members m = new Members(); m.setName("未选择"); m.setId(-1); m.setParentId(0); m.setPostCode("12345"); if (al == null) { al = new ArrayList<Members>(); } al.add(0,m); rtn = JSON.toJSONString(al); } catch (Exception e) { e.printStackTrace(); } response.getWriter().print(rtn); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.hanqi; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSON; import com.hanqi.dao.Members; import com.hanqi.dao.MembersDAL; /** * Servlet implementation class MembersList */ @WebServlet("/MembersList") public class MembersList extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public MembersList() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String rtn = ""; String pid = request.getParameter("parentid"); String pagenum = request.getParameter("page"); String pagerows = request.getParameter("rows"); if (pid == null || pid.trim().length() == 0 ) { pid = "-1"; } int iPid = Integer.parseInt(pid); int iPagenum = Integer.parseInt(pagenum);//页码 int iPagerows = Integer.parseInt(pagerows);//每页记录条数 MembersDAL md = new MembersDAL(); ArrayList<Members> al; try { al = md.getList(iPid, iPagenum, iPagerows); if (al != null) { int count = md.getCount(iPid); rtn = "{\"total\":" + count + ",\"rows\":" + JSON.toJSONString(al) + "}"; } } catch (Exception e) { e.printStackTrace(); } response.getWriter().print(rtn); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.hanqi; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hanqi.dao.Members; import com.hanqi.dao.MembersDAL; /** * Servlet implementation class TestSubmit */ @WebServlet("/TestSubmit") public class TestSubmit extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public TestSubmit() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String json = "{\"success\":true,\"message\":\"数据提交成功\"}"; String name = request.getParameter("name"); String postcode = request.getParameter("postCode"); String parentid = request.getParameter("parentid"); String id = request.getParameter("id"); if (name != null && name.trim().length() > 0) { //get方式 //name = new String(name.getBytes("ISO-8859-1"), "UTF-8"); if (parentid != null && parentid.trim().length() > 0) { Members m = new Members(); if (id != null && id.trim().length() > 0) { m.setId(Integer.parseInt(id)); } m.setName(name); m.setParentId(Integer.parseInt(parentid)); m.setPostCode(postcode); MembersDAL md = new MembersDAL(); try { //执行修改 if (id != null && id.trim().length() > 0) { if (md.update(m) > 0) { json = "{\"success\":true,\"message\":\"数据修改成功\"}"; } else { json = "{\"success\":false,\"message\":\"数据修改失败\"}"; } } else { if (md.insert(m) > 0) { json = "{\"success\":true,\"message\":\"数据新增成功\"}"; } else { json = "{\"success\":false,\"message\":\"数据新增失败\"}"; } } } catch (Exception e) { json = "{\"success\":false,\"message\":\"发生错误:" + e.getMessage() + "\"}"; e.printStackTrace(); } } else { json = "{\"success\":false,\"message\":\"parentid不能为空\"}"; } } else { json = "{\"success\":false,\"message\":\"请正确填写地区名称\"}"; } response.getWriter().append(json); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.hanqi.dao; import java.sql.*; public class DBHelper { public static Connection getConnection() throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:orcl"; Connection conn = DriverManager.getConnection(url,"MemberDB", "123654789"); return conn; } }
package com.hanqi.dao; public class Members { private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getParentId() { return parentId; } public void setParentId(int parentId) { this.parentId = parentId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPostCode() { return postCode; } public void setPostCode(String postCode) { this.postCode = postCode; } private int parentId; private String name; private String postCode; }
package com.hanqi.dao; import java.sql.*; import java.util.*; import com.hanqi.dao.DBHelper; public class MembersDAL { public int insert(Members m) throws Exception { int rtn = -1; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if (conn != null) { try { String sql = "insert into Members (id, parentid, name, postcode) values (sq_members_id.nextval,?,?,?) "; pst = conn.prepareStatement(sql); pst.setInt(1, m.getParentId()); pst.setString(2, m.getName()); pst.setString(3, m.getPostCode()); rtn = pst.executeUpdate(); } catch(Exception ex) { throw ex; } finally { try { pst.close(); } catch(Exception ex){} conn.close(); } } return rtn; } public int update(Members m) throws Exception { int rtn = -1; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if (conn != null) { try { String sql = "update Members set name = ?, postcode = ? where id = ? "; pst = conn.prepareStatement(sql); pst.setString(1, m.getName()); pst.setString(2, m.getPostCode()); pst.setInt(3, m.getId()); rtn = pst.executeUpdate(); } catch(Exception ex) { throw ex; } finally { try { pst.close(); } catch(Exception ex){} conn.close(); } } return rtn; } //删 public int delete(int id) throws Exception { int rtn = -1; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if (conn != null) { try { String sql = "delete Members where id = ? or parentid = ? "; pst = conn.prepareStatement(sql); pst.setInt(1, id); pst.setInt(2, id); rtn = pst.executeUpdate(); } catch(Exception ex) { throw ex; } finally { try { pst.close(); } catch(Exception ex) { } conn.close(); } } return rtn; } private void dgDel(Connection conn, int id) throws SQLException { PreparedStatement pst = null; ResultSet rs = null; Statement st = null; try { //删除 String sql = "delete Members where id = ? "; pst = conn.prepareStatement(sql); pst.setInt(1, id); pst.executeUpdate(); sql = "select * from Members where parentid = " + id; //遍历子节点 st = conn.createStatement(); rs = st.executeQuery(sql); if (rs != null) { while(rs.next()) { dgDel(conn, rs.getInt("id"));//递归调用 } } } catch(Exception ex) { ex.printStackTrace(); } finally { try { rs.close(); } catch(Exception ex) { } try { pst.close(); } catch(Exception ex) { } try { st.close(); } catch(Exception ex) { } } } //删 public int deleteDG(int id) throws Exception { int rtn = -1; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if (conn != null) { try { conn.setAutoCommit(false);//手动提交 //递归级联删除 dgDel(conn, id); conn.commit(); } catch(Exception ex) { conn.rollback(); throw ex; } finally { try { pst.close(); } catch(Exception ex) { } conn.close(); } } return rtn; } public ArrayList<Members> getList(int parentid) throws Exception { ArrayList<Members> rtn = null; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; ResultSet rs = null; if (conn != null) { try { String sql = "select * from members "; pst = conn.prepareStatement(sql); //pst.setInt(1, parentid); rs = pst.executeQuery(); if (rs != null) { rtn = new ArrayList<Members>(); while(rs.next()) { Members u = new Members(); u.setId(rs.getInt("id")); u.setParentId(rs.getInt("parentid")); u.setName(rs.getString("name")); u.setPostCode(rs.getString("postcode")); rtn.add(u); } } } catch(Exception ex) { throw ex; } finally { try { rs.close(); } catch(Exception ex){} try { pst.close(); } catch(Exception ex){} conn.close(); } } return rtn; } public ArrayList<Members> getList(int parentid, int pagenum, int pagerows) throws Exception { ArrayList<Members> rtn = null; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; ResultSet rs = null; if (conn != null) { try { int maxrows = pagenum * pagerows; String sql = "select * from (select t.*, rownum as rnum from (select * from MEMBERS where parentid = ? order by id desc) t where rownum <= ?) where rnum > ?"; pst = conn.prepareStatement(sql); pst.setInt(1, parentid); pst.setInt(2, maxrows); pst.setInt(3, maxrows - pagerows); rs = pst.executeQuery(); if (rs != null) { rtn = new ArrayList<Members>(); while(rs.next()) { Members u = new Members(); u.setId(rs.getInt("id")); u.setParentId(rs.getInt("parentid")); u.setName(rs.getString("name")); u.setPostCode(rs.getString("postcode")); rtn.add(u); } } } catch(Exception ex) { throw ex; } finally { try { rs.close(); } catch(Exception ex){} try { pst.close(); } catch(Exception ex){} conn.close(); } } return rtn; } //单条查询 public Members getMembers(int id) throws Exception { Members rtn = null; //获取 Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; ResultSet rs = null; if (conn != null) { try { String sql = "select * from Members where id = ?"; pst = conn.prepareStatement(sql); pst.setInt(1, id); rs = pst.executeQuery(); if (rs != null && rs.next()) { rtn = new Members(); rtn.setId(rs.getInt("id")); rtn.setParentId(rs.getInt("parentid")); rtn.setName(rs.getString("name")); rtn.setPostCode(rs.getString("postcode")); } } catch(Exception ex) { throw ex; } finally { try { rs.close(); } catch(Exception ex){} try { pst.close(); } catch(Exception ex){} conn.close(); } } return rtn; } //记录条数查询 public int getCount(int parentid) throws Exception { int rtn = 0; //获取 Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; ResultSet rs = null; if (conn != null) { try { String sql = "select count(1) as c from Members where parentid = ?"; pst = conn.prepareStatement(sql); pst.setInt(1, parentid); rs = pst.executeQuery(); if (rs != null && rs.next()) { rtn = rs.getInt("c"); } } catch(Exception ex) { throw ex; } finally { try { rs.close(); } catch(Exception ex){} try { pst.close(); } catch(Exception ex){} conn.close(); } } return rtn; } }
package com.haoqi; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hanqi.dao.MembersDAL; /** * Servlet implementation class DeleteMembers */ @WebServlet("/DeleteMembers") public class DeleteMembers extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DeleteMembers() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String json = ""; String ids = request.getParameter("ids"); if (ids != null && ids.trim().length() > 0) { String[] sid = ids.split(","); MembersDAL md = new MembersDAL(); try { for(String id : sid) //遍历ID ,每遍历一个id就执行一次删除 { //执行删除 int iID = Integer.parseInt(id); //转换类型 md.deleteDG(iID); //执行删除 } json = "{\"success\":true,\"message\":\"删除数据成功\"}"; } catch (Exception e) { e.printStackTrace(); json = "{\"success\":false,\"message\":\"删除数据失败\"}"; } } else { json = "{\"success\":false,\"message\":\"请正确填写ID\"}"; } response.getWriter().append(json); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
标签:
原文地址:http://www.cnblogs.com/name-hanlin/p/5103866.html