1, index页面(带直接显示数据)——解决点击"编辑"没反应的问题链接
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.hanqi.*" %> <%@ page import="com.hanqi.dao.*" %> <%@ page import="java.util.*"%> <!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 src="js/jquery-1.11.3.min.js"></script> <script> function show() { $("#fo").show(); $("#i1").hide(); } function show1() { $("#i1").show(); $("#fo").hide(); } function check() { if(fo.name.value=="") { alert("联系人姓名不能为空"); return false; } else if(fo.tel.value=="") { alert("电话号码不能为空"); return false; } else if(fo.groupid.value==0) { alert("请选择分组"); return false; } else { return true; } } function con() { return confirm("确定要删除吗? 此操作不可撤销! "); } </script> <script> $(document).ready($("form").hide()); </script> </head> <body> <table border="1" > <tr align="center"> <td>姓名</td> <td>电话</td> <td>分组</td> <td> </td> </tr> <% ContactDal cd = new ContactDal(); ArrayList<Contactclass> al = cd.getContacts(); ContactDal cdg = new ContactDal(); ArrayList<Contactclass> algp = cdg.getGroups(); String name = null; if(al!=null) { int id = 2; for(Contactclass ctc : al) { id = ctc.getGroupid(); out.print("<tr><td>"+ctc.getName()+"</td>"); out.print("<td>"+ctc.getTel()+"</td>"); ContactDal cdgroup = new ContactDal(); ArrayList<Contactclass> all = cdgroup.getGroupName(id); if(all!=null) { for(Contactclass ctc1:all) { name = ctc1.getName(); } } out.print("<td>"+name+"</td>"); out.print("<td><a href='ContactDetele?id="+ctc.getId()+"' onclick='return con()'>删除</a>"); out.print(" <input type='button' value='编辑' onclick=''/><br>"); out.print("<form id='fo' action='#'>姓名<input name='name1' type='text' value=''>"); out.print("电话<input name='tel1' type='text' value='' />分组<select name=groupid1>"); out.print("<option value='0' selected>未选择</option>"); for(Contactclass ccg : algp) { out.print("<option value='"+ccg.getId()+"'>"+ccg.getName()+"</option>"); } out.print("</select><input type='submit' value='修改'></form></td></tr>"); } } %> </table> <br> <input id="i1" type="button" value="添加联系人" onclick="show()" /> <form id="fo" action="ContactsInsert" method="post" onsubmit="return check()"> <input type="button" value="返回" onclick="show1()"><br><br> 姓名: <input name="name" type="text" value="" /><br><br> 电话: <input name="tel" type="text" value="" /><br><br> 分组: <select name="groupid"> <option value="0" seleceted>未选择</option> <% ContactDal cdgroup1 = new ContactDal(); ArrayList<Contactclass> all = cdgroup1.getGroups(); if(all!=null) { for(Contactclass ctc2:all) { out.print("<option value='"+ctc2.getId()+"'>"+ctc2.getName()+"</option>"); } } %> </select> <br><br> <input type="submit" value="添加"> <input type="reset" value="重写"> </form> </body> </html> <script> $("#fo").hide(); </script>
2, Servlet控制层
录入联系人
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=utf-8"); String name = request.getParameter("name"); String tel = request.getParameter("tel"); String groupid = request.getParameter("groupid"); //response.getWriter().append("能接收到数据"); if(name!=null&&name.trim().length()>0&&tel!=null&&tel.trim().length()>0&&groupid!=null&&groupid.trim().length()>0) { Contactclass c = new Contactclass(); c.setName(name); c.setTel(tel); c.setGroupid(Integer.parseInt(groupid)); //response.getWriter().append(c.getName()+c.getTel()+c.getGroupid()); ContactDal cd = new ContactDal(); try { if(cd.insert(c)>0) { response.sendRedirect("index.jsp"); } else { response.getWriter().append("录入联系人数据失败"); } } catch(Exception ex) { response.getWriter().append(ex.getMessage()); } } else { response.getWriter().append("姓名,电话,分组都不能为空"); } }
删除数据
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); if(id!=null&&id.trim().length()>0) { ContactDal cd = new ContactDal(); try { if(cd.delete(Integer.parseInt(id))>0) { response.sendRedirect("index.jsp"); } else { response.getWriter().append("删除数据失败"); } } catch (Exception e) { e.printStackTrace(); } } }
修改联系人信息
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=utf-8"); String name1 = request.getParameter("name1"); String tel1 = request.getParameter("tel1"); String groupid1 = request.getParameter("groupid1"); String id1 = request.getParameter("id"); if(name1!=null&&name1.trim().length()>0&&tel1!=null&&tel1.trim().length()>0&&groupid1!=null&&groupid1.trim().length()>0) { ContactDal cd = new ContactDal(); try { int a = cd.update(Integer.parseInt(id1), name1, tel1, Integer.parseInt(groupid1)); if(a>0) { response.sendRedirect("index.jsp"); } else { response.getWriter().append("修改联系人失败"); } } catch(Exception er) { response.getWriter().append(er.getMessage()); } } }
3, Dal工具类
链接数据库, 加载驱动
package com.hanqi.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DbHelper { public static Connection getConn() throws ClassNotFoundException, SQLException { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:orcl"; Connection conn = DriverManager.getConnection(url,"sa","sasa"); return conn; } }
对象类
package com.hanqi.dao; public class Contactclass { private int Id; private String Name; private String Tel; private int Groupid; public int getId() { return Id; } public void setId(int id) { Id = id; } public String getName() { return Name; } public void setName(String name) { Name = name; } public String getTel() { return Tel; } public void setTel(String tel) { Tel = tel; } public int getGroupid() { return Groupid; } public void setGroupid(int groupid) { Groupid = groupid; } }
增删查方法体类
package com.hanqi; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import com.hanqi.dao.Contactclass; import com.hanqi.dao.DbHelper; public class ContactDal { //获取联系人的方法 public ArrayList<Contactclass> getContacts() throws ClassNotFoundException, SQLException { ArrayList<Contactclass> al = null; Connection con = DbHelper.getConn(); PreparedStatement ps = null; ResultSet rs = null; if(con!=null) { try { String sql = "select * from contacts"; ps = con.prepareStatement(sql); rs = ps.executeQuery(); al = new ArrayList<Contactclass>(); if(rs!=null) { while(rs.next()) { Contactclass cc = new Contactclass(); cc.setId(rs.getInt("id")); cc.setName(rs.getString("Name")); cc.setGroupid(rs.getInt("groupid")); cc.setTel(rs.getString("tel")); al.add(cc); } } con.close(); } catch(Exception ex) { System.out.println(ex); } finally{ try { ps.close(); } catch(Exception ex1) { System.out.println(ex1); } finally{ rs.close(); } } } return al; } //获取分组的方法 public ArrayList<Contactclass> getGroupName(int id) throws ClassNotFoundException, SQLException { ArrayList<Contactclass> al = null; Connection con = DbHelper.getConn(); PreparedStatement ps = null; ResultSet rs = null; if(con!=null) { try { String sql = "select * from groups where id ="+id; ps = con.prepareStatement(sql); rs = ps.executeQuery(); if(rs!=null) { al = new ArrayList<Contactclass>(); while(rs.next()) { Contactclass cc = new Contactclass(); cc.setId(rs.getInt("id")); cc.setName(rs.getString("Name")); al.add(cc); } } con.close(); } catch(Exception ex) { System.out.println(ex); } finally{ try { ps.close(); } catch(Exception ex1) { System.out.println(ex1); } finally{ rs.close(); } } } return al; } //录入联系人数据 public int insert(Contactclass Obj) throws Exception{ int row = 0; Connection con = DbHelper.getConn(); PreparedStatement pps = null; if(con!=null) { try { String sql = "insert into contacts (id, name, tel, groupid) values (sq_contacts.nextval,?,?,?)"; pps = con.prepareStatement(sql); pps.setString(1, Obj.getName()); pps.setString(2, Obj.getTel()); pps.setInt(3, Obj.getGroupid()); row = pps.executeUpdate(); } catch(Exception e) { System.out.println(e); } finally { try { con.close(); } catch(Exception e1) { System.out.println(e1); } finally { pps.close(); } } } return row; } //删除联系人 public int delete(int id) throws Exception { int row = 0; Connection co = DbHelper.getConn(); String sql = "delete contacts where id ="+id; PreparedStatement p = null; if(co!=null) { try { p = co.prepareStatement(sql); row = p.executeUpdate(); } catch(Exception e) { System.out.println(e); } finally { try { p.close(); } catch(Exception question) { System.out.println(question); } co.close(); } } return row; } //无参数获取全部分组的方法 public ArrayList<Contactclass> getGroups() throws ClassNotFoundException, SQLException { ArrayList<Contactclass> al = null; Connection con = DbHelper.getConn(); PreparedStatement ps = null; ResultSet rs = null; if(con!=null) { try { String sql = "select * from groups"; ps = con.prepareStatement(sql); rs = ps.executeQuery(); if(rs!=null) { al = new ArrayList<Contactclass>(); while(rs.next()) { Contactclass cc = new Contactclass(); cc.setId(rs.getInt("id")); cc.setName(rs.getString("Name")); al.add(cc); } } con.close(); } catch(Exception ex) { System.out.println(ex); } finally{ try { ps.close(); } catch(Exception ex1) { System.out.println(ex1); } finally{ rs.close(); } } } return al; } //修改联系人方法 public int update(int id,String name,String tel,int groupid) throws Exception { int row = 0; Connection con = DbHelper.getConn(); String sql = "update contacts set name=?, tel=?, groupid=? where id="+id; PreparedStatement p = null; if(con!=null) { try { p = con.prepareStatement(sql); p.setString(1, name); p.setString(2, tel); p.setInt(3, groupid); row = p.executeUpdate(); } catch(Exception e) { System.out.println(e); } finally { try { con.close(); } catch(Exception e) { System.out.println(e); } finally { p.close(); } } } return row; } }