1.index.jsp聯繫人頁面html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ 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-2.1.4.min.js"></script> <% contactsDal md = new contactsDal(); ArrayList<contacts> al = md.getList(0); %> <script> function check() { var name = document.getElementById("name"); if(name.value == null || name.value.trim().length ==0) { alert("姓名不能爲空") return false; } var el = /\d{11}/; var tel = document.getElementById("tel"); if(tel.value != null && tel.value.trim().length == 0 && !el.test(tel.value)) { alert("手機號碼必須是11位數,電話爲區號+號碼11位數."); return false; } return true; } function confirmDelete() { return(confirm("確認要刪除該記錄麼?")); } </script> </head> <body> <table border=1> <tr> <td></td> <td>姓名</td> <td>電話</td> <td>分組</td> </tr> <% if(al != null) { for(contacts c : al) { %> <tr> <td><a href='Update.jsp?id=<%=c.getId() %>'>編輯</a> <a onclick="return confirmDelete()" href='DeleteContacts?id=<%=c.getId()%>'>刪除</a></td> <td><%=c.getName()%></td> <td><%=c.getTel() %></td> <td> <% int gid = c.getGroupid(); //實例化數據操做類 groupDal gd1 = new groupDal(); group g1 = gd1.getGroup(gid); out.print(g1.getName()); %> </td> </tr> <% } } %> </table> <input id="qwe" type="button" value="添加新號碼" onclick="$('#zxc,#qwe').toggle(500);" /> <form id="zxc" style="display:none" method='POST' action='InsertContacts' onsubmit="return check()"> 姓名:<input id='name' name='name' type="text" width=30/><br> 電話:<input id='tel' name='tel' type="text" width=30 maxlength=11/><br> 分組: <select id='groupid' name='groupid'> <% groupDal gd = new groupDal(); ArrayList<group> al2 = gd.getGrouplist(0); if(al2 != null) { for(group g : al2) { %> <option value=<%=g.getId() %>><%=g.getName() %></option> <% } } %> </select> <br> <input type="submit" value="添加" /> </form> </body> </html>
2.Update.jsp編輯頁面java
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ 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-2.1.4.min.js"></script> <script> function check() { var name = document.getElementById("name"); if(name.value == null || name.value.trim().length ==0) { alert("姓名不能爲空") return false; } var el = /\d{11}/; var tel = document.getElementById("tel"); if(tel.value != null && tel.value.trim().length == 0 && !el.test(tel.value)) { alert("手機號碼必須是11位數,電話爲區號+號碼11位數."); return false; } return true; } </script> </head> <body> <% //接收參數id String id = request.getParameter("id"); if(id == null || id.trim().length() == 0) { out.print("id爲空"); response.setHeader("refresh","5;url=index.jsp"); } else{ //查詢聯繫人信息 //實例化Contacts contacts c = new contacts(); //實例化數據庫操做類 contactsDal cd = new contactsDal(); c = cd.getContacts(Integer.parseInt(id)); try{ %> <form action="EditContacts?id=<%=c.getId() %>" method='POST' onsubmit='return check()'> 姓名:<input id='name' name='name' type="text" width=30 value='<%=c.getName() %>' /><br> 電話:<input id='tel' name='tel' type="text" width=30 maxlength=11 value='<%=c.getTel() %>' /><br> 分組: <select id='groupid' name='groupid'> <%}catch(Exception e) { response.getWriter().append(e.getMessage()); } groupDal gd = new groupDal(); ArrayList<group> al2 = gd.getGrouplist(0); if(al2 != null) { for(group g : al2) { %> <option value=<%=g.getId() %> <%= ((c.getGroupid()==g.getId())? "selected" : "" ) %>><%=g.getName() %></option> <% } } } %> </select> <br> <input type='submit' value='修改'/> </form> </body> </html>
3.DBHelper.java數據庫鏈接工具類jquery
package com.hanqi.dao; import java.sql.*; public class DBHelper { public static Connection getConnection() throws Exception { //加載驅動 Class.forName("oracle.jdbc.driver.OracleDriver"); //建立URL String url = "jdbc:oracle:thin:@localhost:1521:ORCL"; //驅動管理 Connection conn = DriverManager.getConnection(url,"test1","test1"); return conn; } }
4.contacts.java封裝聯繫人信息的實體類sql
package com.hanqi.dao; public class contacts { //id private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } //name private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } //tel private String tel; public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } //groupid private int groupid; public int getGroupid() { return groupid; } public void setGroupid(int groupid) { this.groupid = groupid; } }
5.group.java封裝分組信息的實體類數據庫
package com.hanqi.dao; public class group { //id private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } //name private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
6.contactsDal.java聯繫人的數據庫操做類oracle
package com.hanqi.dao; import java.sql.*; import java.util.*; public class contactsDal { //增 public int insert(contacts c) throws Exception { int rtn = -1; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if(conn != null) { try { String sql = "insert into contacts (id,name,tel,groupid) values (sq_members_id.nextval,?,?,?)"; pst = conn.prepareStatement(sql); pst.setString(1, c.getName()); pst.setString(2, c.getTel()); pst.setInt(3, c.getGroupid()); rtn = pst.executeUpdate(); } catch(Exception ex) { throw ex; } finally{ try{ pst.close();} catch(Exception ex){} conn.close(); } } return rtn; } //查 public ArrayList<contacts> getList(int id) throws Exception { ArrayList<contacts> rtn = new ArrayList<contacts>(); Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; ResultSet rs = null; if(conn != null) { try { String sql = "select * from contacts"; pst = conn.prepareStatement(sql); rs = pst.executeQuery(); if(rs != null) { //遍歷 while(rs.next()) { //實例化實體類 contacts m = new contacts(); m.setId(rs.getInt("ID"));//取值 m.setName(rs.getString("name"));//取值 m.setTel(rs.getString("tel")); m.setGroupid(rs.getInt("groupid")); rtn.add(m); } } } catch(Exception ex) { throw ex; } finally{ try{ pst.close();} catch(Exception ex){} conn.close(); } } return rtn; } //刪 public int delete(contacts c) throws Exception { int rtn = -1; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if(conn != null) { try{ String sql = "delete from contacts where id = ?"; pst = conn.prepareStatement(sql); pst.setInt(1,c.getId()); rtn = pst.executeUpdate(); } catch(Exception ex) { throw ex; } finally { try{ pst.close();} catch(Exception ex){} conn.close(); } } return rtn; } //改 public int update(contacts c) throws Exception { int rtn = -1; Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; if(conn != null) { try { String sql = "update contacts set name = ?, tel = ? ,groupid = ? where id = ?"; pst = conn.prepareStatement(sql); pst.setString(1, c.getName()); pst.setString(2, c.getTel()); pst.setInt(3, c.getGroupid()); pst.setInt(4,c.getId()); rtn = pst.executeUpdate(); } catch(Exception ex) { throw ex; } finally{ try{ pst.close();} catch(Exception ex){} conn.close(); } } return rtn; } //單條查詢 public contacts getContacts(int id) throws Exception { contacts rtn = null; //獲取 Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; ResultSet rs = null; if(conn != null) { try { String sql = "select * from contacts where id=?"; pst = conn.prepareStatement(sql); pst.setInt(1,id); rs = pst.executeQuery(); if(rs != null && rs.next()) { //初始化 rtn = new contacts(); //實例化實體類 rtn.setId(rs.getInt("ID"));//取值 rtn.setName(rs.getString("name"));//取值 rtn.setTel(rs.getString("tel")); rtn.setGroupid(rs.getInt("groupid")); } } catch(Exception ex) { throw ex; } finally{ try{ pst.close();} catch(Exception ex){} conn.close(); } } return rtn; } }
7.groupDal.java分組信息的數據庫操做類app
package com.hanqi.dao; import java.sql.*; import java.util.*; public class groupDal { //查 public ArrayList<group> getGrouplist(int id) throws Exception { ArrayList<group> rtn = new ArrayList<group>(); Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; ResultSet rs = null; if(conn != null) { try { String sql = "select * from groups"; pst = conn.prepareStatement(sql); rs = pst.executeQuery(); if(rs != null) { //遍歷 while(rs.next()) { //實例化實體類 group g = new group(); g.setId(rs.getInt("ID"));//取值 g.setName(rs.getString("name"));//取值 rtn.add(g); } } } catch(Exception ex) { throw ex; } finally{ try{ pst.close();} catch(Exception ex){} conn.close(); } } return rtn; } //單條查詢 public group getGroup(int id) throws Exception { group rtn = null; //獲取 Connection conn = DBHelper.getConnection(); PreparedStatement pst = null; ResultSet rs = null; if(conn != null) { try { String sql = "select * from groups where id=?"; pst = conn.prepareStatement(sql); pst.setInt(1,id); rs = pst.executeQuery(); if(rs != null && rs.next()) { //初始化 rtn = new group(); //實例化實體類 rtn.setId(rs.getInt("ID"));//取值 rtn.setName(rs.getString("name"));//取值 } } catch(Exception ex) { throw ex; } finally{ try{ pst.close();} catch(Exception ex){} conn.close(); } } return rtn; } }
8.InsertContacts.java添加聯繫人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.*; /** * Servlet implementation class InsertContacts */ @WebServlet("/InsertContacts") public class InsertContacts extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public InsertContacts() { 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 tel = request.getParameter("tel"); String gi = request.getParameter("groupid"); if(name == null || name.trim().length() == 0) { response.getWriter().append("姓名不能爲空"); } else { //get方式 //name = new String(name.getBytes("ISO-8859-1"),"UTF-8"); if(tel != null && tel.trim().length() > 0) { if(gi != null && gi.trim().length() > 0) { contacts c = new contacts(); c.setGroupid(Integer.parseInt(gi)); c.setName(name); c.setTel(tel); //調用模型層 contactsDal cd = new contactsDal(); try{ cd.insert(c); }catch(Exception e){ response.getWriter().append("保存數據失敗"); e.printStackTrace(); } } } else { response.getWriter().append("電話號碼不能爲空"); } } response.sendRedirect("index.jsp"); } /** * @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); } }
9.DeleteContacts.java刪除聯繫人ide
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 java.sql.*; import com.hanqi.dao.*; /** * Servlet implementation class DeleteContacts */ @WebServlet("/DeleteContacts") public class DeleteContacts extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DeleteContacts() { 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"); if(id != null && id.trim().length() > 0) { contacts c = new contacts(); contactsDal cd = new contactsDal(); c.setId(Integer.parseInt(id)); try{ cd.delete(c); response.sendRedirect("index.jsp"); }catch(Exception e){ response.getWriter().append("刪除數據失敗"); e.printStackTrace(); } } else { response.getWriter().append("ID不能爲空"); } } /** * @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); } }
10.EditContacts.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.*; import java.sql.*; /** * Servlet implementation class EditContacts */ @WebServlet("/EditContacts") public class EditContacts extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public EditContacts() { 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 tel = request.getParameter("tel"); String gi = request.getParameter("groupid"); String id = request.getParameter("id"); if(name == null || name.trim().length() == 0) { response.getWriter().append("姓名不能爲空"); } else if(tel == null || tel.trim().length() == 0) { response.getWriter().append("電話號碼不能爲空"); } else { contacts c = new contacts(); c.setName(name); c.setTel(tel); c.setGroupid(Integer.parseInt(gi)); c.setId(Integer.parseInt(id)); //調用模型層 contactsDal cd = new contactsDal(); try{ cd.update(c); }catch(Exception e){ response.getWriter().append("更新數據失敗"); e.printStackTrace(); } } response.sendRedirect("index.jsp"); } /** * @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); } }