<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <%@ page import="com.hanqi.*" %> <%@ page import="com.hanqi.dao.*" %> <!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> <script > function test() { var id = document.getElementById("name"); if(id.value == "") { alert("地区不能为空!"); return false; } //isNaN 不是数字就返回true var el = / \d{6} /; //正则表达式 var post = document.getElementById("post"); if(post.value != "" && !el.test(post.value) == 0) { alert("邮编必须为6位数字!"); return false; } return true; } function confirmDelete() { return confirm("确认删除该地区吗?删除该地区的同时,会将该地区下的所有地区删除"); /* var r = confirm("确认删除该地区吗?删除该地区的同时,会将该地区下的所有地区删除"); if (r == true) { response.sendRedirect("index.jsp?parentid="+parentid); return true; } else { response.sendRedirect("index.jsp?parentid="+parentid); return false; } */ } </script> </head> <body> <table border="1"> <tr> <th>ID</th> <th>父ID</th> <th>地名</th> <th>邮编</th> <th>管理</th> <th>删除</th> </tr> <% String pid = request.getParameter("parentid"); if(pid == null || pid.trim().length() == 0) { pid = "0"; } int iPid = Integer.parseInt(pid); //转换数据类型 //获取数据 MembersDAL md = new MembersDAL(); ArrayList<Members> al = md.getList(iPid); if(al != null) { for(Members u : al) { /* if(u.getPostCode() == null) { u.setPostCode(""); } */ out.print("<tr><td>"+u.getId() + "</td><td>"+u.getParentid()+ "</td><td>" + u.getName() + "</td><td>" + (u.getPostCode() == null ? "":u.getPostCode()) + "</td><td>" + "<a href=‘index.jsp?parentid="+u.getId() +"‘>下级地区</a></td><td><a onclick=‘return confirmDelete();‘ href=‘DeleteMember?id=" + u.getId() + "& parentid=" + u.getParentid() + "‘>删除</a></td></tr>" ); } } //遍历数据(循环输出) %> </table> <!-- 无序排序,版面设计 --> <form method="post" action="InsertMember" onSubmit="return test();"> <input name="parentid" type="hidden" value="<%=pid %>" /> <ul> <li>请输入地区:<input id="name" name="name" type="text" width=30 /></li> <li>请输入邮编:<input id="post" name="postcode" type="text" width=30 maxlength="6"/></li> <li><input type="submit" value="提交" /> <input type="reset" value="取消" /> </li> </ul> </form> </body> </html>
index.jsp
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 InsertMember */ @WebServlet("/InsertMember") public class InsertMember extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public InsertMember() { 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 name = request.getParameter("name"); String postcode = request.getParameter("postcode"); //接收父ID String parentid = request.getParameter("parentid"); if(name != null && name.trim().length() > 0) { //get方式时需要的转码 post方式不需要 // name = new String(name.getBytes("ISO-8859-1"),"UTF-8"); if(parentid != null && parentid.trim().length() > 0) { //调用数据访问层 //构建实体类 实例化 Members m = new Members(); m.setName(name); m.setParentid(Integer.parseInt(parentid)); m.setPostCode(postcode); MembersDAL md = new MembersDAL(); try { if(md.insert(m) >0) { response.sendRedirect("index.jsp?parentid="+parentid); //根据parentid来进行跳转 } else { response.getWriter().append("保存数据失败"); } } catch (Exception e) { response.getWriter().append("发生错误" + e.getMessage()); e.printStackTrace(); } } else { response.getWriter().append("parentid不能为空"); } } else { response.getWriter().append("请正确填写地区名称"); } response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @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); } }
InsertMember.java
package com.hanqi.dao; import java.sql.*; public class DBHelper { //获取connection对象 static 静态方法 不需要调用他的实例化 public static Connection getConnection() throws Exception //工具类中异常不要用try catch { //加载数据库驱动,并注册到驱动管理器 Class.forName("oracle.jdbc.driver.OracleDriver"); //数据库连接字符串 String url = "jdbc:oracle:thin:@localhost:1521:orcl"; //获取connection对象 (个人理解为登陆数据库 并将数据关联) Connection conn = DriverManager.getConnection(url,"MemberDB","123654789"); //url,名称,密码 return conn; } }
DBHelper.java
package com.hanqi.dao; public class Members { private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } private int parentid; public int getParentid() { return parentid; } public void setParentid(int parentid) { this.parentid = parentid; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } private String postCode; public String getPostCode() { return postCode; } public void setPostCode(String postCode) { this.postCode = postCode; } }
Members.java
package com.hanqi.dao; import java.sql.*; import java.util.*; public class MembersDAL { //增 //创建一个实体类 m 并将它放置进去 public int insert(Members m) throws Exception { int rtn = 0; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if (conn != null) { try { String sql = "insert into MEMBERS (id, parentid, name,PostCode) values (SQ_MEMBERDB_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 e) { conn.close(); } } } return rtn; } //查找 public ArrayList<Members> getList(int parentid) throws Exception //将父ID传进去,这样能够继承上级ID { ArrayList<Members> rtn = null; // 如果没有查到数据就会返回空 Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if (conn != null) { try { String sql = "select * from Members where parentid = ?"; pst = conn.prepareStatement(sql); pst.setInt(1, parentid); ResultSet 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 { pst.close(); } catch(Exception e) { 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 = ?"; //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 e) { 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.prepareStatement(sql); 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); /* String sql = "delete Members where id = ? or parentid=?"; pst = conn.prepareStatement(sql); pst.setInt(1, id); pst.setInt(2, id); rtn = pst.executeUpdate(); */ conn.commit(); // 手动提交修改 } catch(Exception ex) { conn.rollback(); //回滚 throw ex; } finally { try { pst.close(); } catch(Exception e) { conn.close(); } } } return rtn; } }
MembersDAL.java
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.MembersDAL; /** * Servlet implementation class DeleteMember */ @WebServlet("/DeleteMember") public class DeleteMember extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DeleteMember() { 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 id = request.getParameter("id"); String parentid = request.getParameter("parentid"); MembersDAL ud = new MembersDAL(); if(id != null && id.trim().length() > 0) { try { //转换 //int ud = Integer.parseInt(id); //ud.delete(id); ud.deleteDG(Integer.parseInt(id)); //将 id 强制转换为 int 型 response.sendRedirect("index.jsp?parentid=" + (parentid == null ? "0" : parentid)); } catch (Exception e) { response.getWriter().append("删除数据失败"); e.printStackTrace(); } //删除 } else { response.getWriter().append("ID不能为空"); } } private String parentid() { // TODO 自动生成的方法存根 return null; } /** * @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); } }
DeleteMember.java
时间: 2024-10-10 13:17:10