标签:
数据库表:
1.联系人表 contacts
1 -- Create table 2 create table CONTACTS 3 ( 4 id NUMBER not null, 5 name VARCHAR2(20) not null, 6 tel NVARCHAR2(20) not null, 7 groupid NUMBER not null 8 ) 9 tablespace TEST 10 pctfree 10 11 initrans 1 12 maxtrans 255 13 storage 14 ( 15 initial 64K 16 next 1M 17 minextents 1 18 maxextents unlimited 19 ); 20 -- Create/Recreate primary, unique and foreign key constraints 21 alter table CONTACTS 22 add constraint PK_CONTACTS primary key (ID) 23 using index 24 tablespace TEST 25 pctfree 10 26 initrans 2 27 maxtrans 255 28 storage 29 ( 30 initial 64K 31 next 1M 32 minextents 1 33 maxextents unlimited 34 );
2.分组信息表 groups
1 -- Create table 2 create table GROUPS 3 ( 4 id NUMBER not null, 5 name VARCHAR2(20) not null 6 ) 7 tablespace TEST 8 pctfree 10 9 initrans 1 10 maxtrans 255 11 storage 12 ( 13 initial 64K 14 next 1M 15 minextents 1 16 maxextents unlimited 17 ); 18 -- Create/Recreate primary, unique and foreign key constraints 19 alter table GROUPS 20 add constraint PK_GROUPS primary key (ID) 21 using index 22 tablespace TEST 23 pctfree 10 24 initrans 2 25 maxtrans 255 26 storage 27 ( 28 initial 64K 29 next 1M 30 minextents 1 31 maxextents unlimited 32 );
3,联系人id列自增长序列 SQ_CONTACTS_ID
1 -- Create sequence 2 create sequence SQ_CONTACTS_ID 3 minvalue 1 4 maxvalue 9999999999999999999999999999 5 start with 21 6 increment by 1 7 cache 20;
javaee工程:lxr
工具类放在com.haq.comm包中,包括:
1,数据库连接工具类 DBHelper.java
1 package com.haq.comm; 2 3 import java.sql.*; 4 5 //数据库连接工具类 6 public class DBHelper { 7 8 public static Connection getConnection() throws Exception 9 { 10 Connection conn = null; 11 12 Class.forName("oracle.jdbc.driver.OracleDriver"); 13 14 String url = "jdbc:oracle:thin:@localhost:1521:orcl"; 15 16 conn = DriverManager.getConnection(url,"test","test"); 17 18 return conn; 19 } 20 21 }
2,中文编码过滤器 ZhongWen.java
1 package com.haq.comm; 2 3 import java.io.IOException; 4 import javax.servlet.Filter; 5 import javax.servlet.FilterChain; 6 import javax.servlet.FilterConfig; 7 import javax.servlet.ServletException; 8 import javax.servlet.ServletRequest; 9 import javax.servlet.ServletResponse; 10 import javax.servlet.annotation.WebFilter; 11 12 /** 13 * Servlet Filter implementation class ZhongWen 14 */ 15 @WebFilter("/*") 16 public class ZhongWen implements Filter { 17 18 /** 19 * Default constructor. 20 */ 21 public ZhongWen() { 22 // TODO Auto-generated constructor stub 23 } 24 25 /** 26 * @see Filter#destroy() 27 */ 28 public void destroy() { 29 // TODO Auto-generated method stub 30 } 31 32 /** 33 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) 34 */ 35 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 36 // TODO Auto-generated method stub 37 // place your code here 38 39 //处理中文乱码的过滤器 40 41 request.setCharacterEncoding("UTF-8"); 42 response.setCharacterEncoding("UTF-8"); 43 response.setContentType("text/html; charset=UTF-8"); 44 45 // pass the request along the filter chain 46 chain.doFilter(request, response); 47 } 48 49 /** 50 * @see Filter#init(FilterConfig) 51 */ 52 public void init(FilterConfig fConfig) throws ServletException { 53 // TODO Auto-generated method stub 54 } 55 56 }
模型层类:放在com.hanq.dao包中,包括:
1,封装联系人信息的实体类 Contacts.java
1 package com.haq.dao; 2 3 //实体类封装联系人信息 4 public class Contacts { 5 6 private int id; 7 private String name; 8 private String tel; 9 private int groupid; 10 11 public int getId() { 12 return id; 13 } 14 public void setId(int id) { 15 this.id = id; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public String getTel() { 24 return tel; 25 } 26 public void setTel(String tel) { 27 this.tel = tel; 28 } 29 public int getGroupid() { 30 return groupid; 31 } 32 public void setGroupid(int groupid) { 33 this.groupid = groupid; 34 } 35 36 37 38 }
2,封装联系人表数据库操作的类 ContDal.java
1 package com.haq.dao; 2 3 import java.util.*; 4 import java.sql.*; 5 6 import com.haq.comm.DBHelper; 7 8 //数据库操作类 9 public class ContDal { 10 11 //插入数据 12 public int insertCont(Contacts c ) throws Exception 13 { 14 //初始化返回值行数 15 int row = -1; 16 //获取数据库连接 17 Connection conn = DBHelper.getConnection(); 18 //如果获取成功 19 if(conn != null) 20 { 21 //声明PreparedStatement对象 22 PreparedStatement ps = null; 23 24 try 25 { 26 //组织sql插入语句 27 String sql = "insert into contacts(id,name,tel,groupid) values(sq_contacts_id.nextval,?,?,?)"; 28 //获取PreparedStatement对象; 29 ps = conn.prepareStatement(sql); 30 //对sql语句中的参数赋值 31 ps.setString(1, c.getName()); 32 ps.setString(2, c.getTel()); 33 ps.setInt(3, c.getGroupid()); 34 //执行插入操作,返回影响行数 35 row = ps.executeUpdate(); 36 } 37 catch(Exception e) 38 { 39 throw e; 40 } 41 finally 42 { 43 try 44 { 45 //释放资源 46 ps.close(); 47 conn.close(); 48 } 49 catch(Exception e) 50 { 51 //确保ps关闭失败时,数据库连接可正常关闭 52 conn.close(); 53 } 54 } 55 56 } 57 //返回插入行数 58 return row; 59 60 } 61 62 //查询方法 63 public ArrayList<Contacts> selectContList() throws Exception 64 { 65 ArrayList<Contacts> arr = null; 66 67 //获取数据库连接 68 Connection conn = DBHelper.getConnection(); 69 70 if(conn != null) 71 { 72 Statement st = null; 73 ResultSet rs = null; 74 75 try 76 { 77 //组织sql查询语句 78 String sql ="select * from contacts"; 79 //获取Statement 80 st = conn.createStatement(); 81 //执行sql语句,获取ResultSet 82 rs = st.executeQuery(sql); 83 //实例化ArrayList对象 84 arr = new ArrayList<Contacts>(); 85 if(rs != null) 86 { 87 //遍历rs 88 while(rs.next()) 89 { 90 //实例化实体类Contacts 91 Contacts c = new Contacts(); 92 93 c.setId(rs.getInt("id")); 94 c.setName(rs.getString("name")); 95 c.setTel(rs.getString("tel")); 96 c.setGroupid(rs.getInt("groupid")); 97 98 arr.add(c); 99 } 100 } 101 } 102 catch(Exception e) 103 { 104 throw e; 105 } 106 finally 107 { 108 try 109 { 110 st.close(); 111 rs.close(); 112 conn.close(); 113 } 114 catch(Exception e) 115 { 116 conn.close(); 117 } 118 } 119 } 120 121 122 return arr; 123 } 124 125 //删除方法 126 public int deleteCont(int id ) throws Exception 127 { 128 //初始化返回值行数 129 int row = -1; 130 //获取数据库连接 131 Connection conn = DBHelper.getConnection(); 132 //如果获取成功 133 if(conn != null) 134 { 135 //声明PreparedStatement对象 136 PreparedStatement ps = null; 137 138 try 139 { 140 //组织sql插入语句 141 String sql = "delete from contacts where id = ?"; 142 //获取PreparedStatement对象; 143 ps = conn.prepareStatement(sql); 144 //对sql语句中的参数赋值 145 ps.setInt(1, id); 146 //执行插入操作,返回影响行数 147 row = ps.executeUpdate(); 148 } 149 catch(Exception e) 150 { 151 throw e; 152 } 153 finally 154 { 155 try 156 { 157 //释放资源 158 ps.close(); 159 conn.close(); 160 } 161 catch(Exception e) 162 { 163 //确保ps关闭失败时,数据库连接可正常关闭 164 conn.close(); 165 } 166 } 167 168 } 169 //返回插入行数 170 return row; 171 172 } 173 174 //修改方法 175 public int updateCont(Contacts c, int id ) throws Exception 176 { 177 //初始化返回值行数 178 int row = -1; 179 //获取数据库连接 180 Connection conn = DBHelper.getConnection(); 181 //如果获取成功 182 if(conn != null) 183 { 184 //声明PreparedStatement对象 185 PreparedStatement ps = null; 186 187 try 188 { 189 //组织sql插入语句 190 String sql = "update contacts set name=?,tel=?,groupid=? where id=?"; 191 //获取PreparedStatement对象; 192 ps = conn.prepareStatement(sql); 193 //对sql语句中的参数赋值 194 ps.setString(1, c.getName()); 195 ps.setString(2, c.getTel()); 196 ps.setInt(3, c.getGroupid()); 197 ps.setInt(4, id); 198 //执行插入操作,返回影响行数 199 row = ps.executeUpdate(); 200 } 201 catch(Exception e) 202 { 203 throw e; 204 } 205 finally 206 { 207 try 208 { 209 //释放资源 210 ps.close(); 211 conn.close(); 212 } 213 catch(Exception e) 214 { 215 //确保ps关闭失败时,数据库连接可正常关闭 216 conn.close(); 217 } 218 } 219 220 } 221 //返回插入行数 222 return row; 223 224 } 225 226 //单条查询 227 public Contacts seleteCont(int id) throws Exception 228 { 229 //声明返回值 230 Contacts c = null; 231 232 //获得连接 233 Connection conn = DBHelper.getConnection(); 234 235 //如果获取到连接 236 if(conn != null) 237 { 238 PreparedStatement ps = null; 239 240 ResultSet rs = null; 241 242 try 243 { 244 //组织数据库查询的sql语句 245 String sql ="select * from contacts where id = ?"; 246 //获取PreparedStatement对象 247 ps = conn.prepareStatement(sql); 248 //添加sql语句参数 249 ps.setInt(1, id); 250 //执行sql语句,获取ResultSet对象 251 rs = ps.executeQuery(); 252 //如果ResultSet获取到 253 if(rs != null && rs.next()) 254 { 255 c = new Contacts(); 256 257 c.setId(rs.getInt("id")); 258 c.setName(rs.getString("name")); 259 c.setTel(rs.getString("tel")); 260 c.setGroupid(rs.getInt("groupid")); 261 } 262 263 } 264 catch(Exception e) 265 { 266 267 throw e; 268 269 } 270 finally 271 { 272 //释放资源 273 try 274 { 275 276 ps.close(); 277 rs.close(); 278 conn.close(); 279 } 280 catch(Exception e) 281 { 282 conn.close(); 283 } 284 } 285 286 } 287 288 //返回Groups对象 289 return c; 290 } 291 292 }
3,封装分组信息数据的实体类 Groups.java
1 package com.haq.dao; 2 3 //分组实体类 4 public class Groups { 5 6 //列作为属性,设为私有 7 private int id; 8 private String name; 9 //对外开放get,set方法 10 public int getId() { 11 return id; 12 } 13 public void setId(int id) { 14 this.id = id; 15 } 16 public String getName() { 17 return name; 18 } 19 public void setName(String name) { 20 this.name = name; 21 } 22 }
4,封装分组表数据库操作的类 GroupDal.java
1 package com.haq.dao; 2 3 import java.sql.*; 4 import java.util.ArrayList; 5 6 import com.haq.comm.DBHelper; 7 8 //分组表数据库操作类 9 public class GroupDal { 10 11 //查询全表信息 12 public ArrayList<Groups> seleteGroupsList() throws Exception 13 { 14 //声明返回值 15 ArrayList<Groups> arr = null; 16 17 //获得连接 18 Connection conn = DBHelper.getConnection(); 19 20 //如果获取到连接 21 if(conn != null) 22 { 23 Statement st = null; 24 25 ResultSet rs = null; 26 27 try 28 { 29 //组织数据库查询的sql语句 30 String sql ="select * from groups"; 31 //获取Statement对象 32 st = conn.createStatement(); 33 //执行sql语句,获取ResultSet对象 34 rs = st.executeQuery(sql); 35 //如果ResultSet获取到 36 if(rs != null) 37 { 38 //实例化ArrayList<Groups> 39 arr = new ArrayList<Groups>(); 40 41 //遍历rs,将数据添加到Groups实体类的对象中 42 while(rs.next()) 43 { 44 //实例化Groups 45 Groups g = new Groups(); 46 g.setId(rs.getInt("id")); 47 g.setName(rs.getString("name")); 48 //向集合中添加数据 49 arr.add(g); 50 } 51 52 } 53 } 54 catch(Exception e) 55 { 56 57 throw e; 58 59 } 60 finally 61 { 62 //释放资源 63 try 64 { 65 66 st.close(); 67 rs.close(); 68 conn.close(); 69 } 70 catch(Exception e) 71 { 72 conn.close(); 73 } 74 } 75 76 } 77 78 //返回Groups对象 79 return arr; 80 } 81 82 //查询单条记录方法 83 public Groups seleteGroups(int id) throws Exception 84 { 85 //声明返回值 86 Groups g = null; 87 88 //获得连接 89 Connection conn = DBHelper.getConnection(); 90 91 //如果获取到连接 92 if(conn != null) 93 { 94 PreparedStatement ps = null; 95 96 ResultSet rs = null; 97 98 try 99 { 100 //组织数据库查询的sql语句 101 String sql ="select * from groups where id = ?"; 102 //获取PreparedStatement对象 103 ps = conn.prepareStatement(sql); 104 //添加sql语句参数 105 ps.setInt(1, id); 106 //执行sql语句,获取ResultSet对象 107 rs = ps.executeQuery(); 108 //如果ResultSet获取到 109 if(rs != null && rs.next()) 110 { 111 g = new Groups(); 112 113 g.setId(rs.getInt("id")); 114 g.setName(rs.getString("name")); 115 } 116 117 } 118 catch(Exception e) 119 { 120 121 throw e; 122 123 } 124 finally 125 { 126 //释放资源 127 try 128 { 129 130 ps.close(); 131 rs.close(); 132 conn.close(); 133 } 134 catch(Exception e) 135 { 136 conn.close(); 137 } 138 } 139 140 } 141 142 //返回Groups对象 143 return g; 144 } 145 146 }
控制层Servlet:放于com.hanq包中,包括:
1,插入联系人 InsertCont.java
1 package com.haq; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import com.haq.dao.ContDal; 11 import com.haq.dao.Contacts; 12 13 /** 14 * Servlet implementation class InsertCont 15 */ 16 @WebServlet("/InsertCont") 17 public class InsertCont extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 /** 21 * @see HttpServlet#HttpServlet() 22 */ 23 public InsertCont() { 24 super(); 25 // TODO Auto-generated constructor stub 26 } 27 28 /** 29 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 30 */ 31 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 32 // TODO Auto-generated method stub 33 //接收表单数据 34 String name = request.getParameter("name"); 35 String tel = request.getParameter("tel"); 36 String groupid = request.getParameter("groupid"); 37 //验证数据是否正确传递 38 if(name == null || name.trim().length() ==0) 39 { 40 response.getWriter().append("姓名不能为空"); 41 } 42 else if(tel == null || tel.trim().length() ==0) 43 { 44 response.getWriter().append("电话不能为空"); 45 } 46 else if(groupid == null || groupid.trim().length() ==0) 47 { 48 response.getWriter().append("分组不能为空"); 49 } 50 else 51 { 52 //实例化联系人实体类 53 Contacts c = new Contacts(); 54 //将接收到的数据添加到对象中 55 c.setName(name); 56 c.setTel(tel); 57 c.setGroupid(Integer.parseInt(groupid)); 58 59 //实例化数据库操作类 60 ContDal cd = new ContDal(); 61 //调用添加方法添加 62 try { 63 64 int row = cd.insertCont(c); 65 response.getWriter().append("成功添加" + row + "条数据"); 66 67 } catch (Exception e) { 68 69 response.getWriter().append("添加失败;错误信息:" + e.getMessage()); 70 } 71 72 73 } 74 75 //跳转回主页面 76 response.setHeader("refresh", "1;URL=ContMana.jsp"); 77 //response.getWriter().append("Served at: ").append(request.getContextPath()); 78 } 79 80 /** 81 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 82 */ 83 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 84 // TODO Auto-generated method stub 85 doGet(request, response); 86 } 87 88 }
2,删除联系人 DeleteCont.java
1 package com.haq; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import com.haq.dao.*; 11 12 /** 13 * Servlet implementation class DeleteCont 14 */ 15 @WebServlet("/DeleteCont") 16 public class DeleteCont extends HttpServlet { 17 private static final long serialVersionUID = 1L; 18 19 /** 20 * @see HttpServlet#HttpServlet() 21 */ 22 public DeleteCont() { 23 super(); 24 // TODO Auto-generated constructor stub 25 } 26 27 /** 28 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 29 */ 30 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 31 // TODO Auto-generated method stub 32 33 //接收参数 34 String id = request.getParameter("id"); 35 36 if(id == null || id.trim().length() == 0) 37 { 38 response.getWriter().append("id不能为空"); 39 } 40 else 41 { 42 //实例化数据库操作类 43 ContDal cd = new ContDal(); 44 45 try { 46 int row = cd.deleteCont(Integer.parseInt(id)); 47 response.getWriter().append("成功删除" + row + "条数据"); 48 } catch (Exception e) { 49 // TODO Auto-generated catch block 50 response.getWriter().append(e.getMessage()); 51 } 52 53 } 54 55 //跳转回主页面 56 response.setHeader("refresh", "1;URL=ContMana.jsp"); 57 //response.getWriter().append("Served at: ").append(request.getContextPath()); 58 } 59 60 /** 61 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 62 */ 63 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 64 // TODO Auto-generated method stub 65 doGet(request, response); 66 } 67 68 }
3,修改联系人 UpdateCont.java
1 package com.haq; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.annotation.WebServlet; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import com.haq.dao.ContDal; 11 import com.haq.dao.Contacts; 12 13 /** 14 * Servlet implementation class UpdateCont 15 */ 16 @WebServlet("/UpdateCont") 17 public class UpdateCont extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 /** 21 * @see HttpServlet#HttpServlet() 22 */ 23 public UpdateCont() { 24 super(); 25 // TODO Auto-generated constructor stub 26 } 27 28 /** 29 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 30 */ 31 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 32 // TODO Auto-generated method stub 33 34 //接收表单数据 35 String name = request.getParameter("name"); 36 String tel = request.getParameter("tel"); 37 String groupid = request.getParameter("groupid"); 38 String id = request.getParameter("id"); 39 //验证数据是否正确传递 40 if(name == null || name.trim().length() ==0) 41 { 42 response.getWriter().append("姓名不能为空"); 43 } 44 else if(tel == null || tel.trim().length() ==0) 45 { 46 response.getWriter().append("电话不能为空"); 47 } 48 else if(groupid == null || groupid.trim().length() ==0) 49 { 50 response.getWriter().append("分组不能为空"); 51 } 52 else if(id == null || id.trim().length() ==0) 53 { 54 response.getWriter().append("id不能为空"); 55 } 56 else 57 { 58 //实例化联系人实体类 59 Contacts c = new Contacts(); 60 //将接收到的数据添加到对象中 61 c.setName(name); 62 c.setTel(tel); 63 c.setGroupid(Integer.parseInt(groupid)); 64 65 //实例化数据库操作类 66 ContDal cd = new ContDal(); 67 //调用添加方法添加 68 try { 69 70 int row = cd.updateCont(c,Integer.parseInt(id)); 71 response.getWriter().append("成功修改" + row + "条数据"); 72 73 } catch (Exception e) { 74 75 response.getWriter().append("添加失败;错误信息:" + e.getMessage()); 76 } 77 78 79 } 80 81 //跳转回主页面 82 response.setHeader("refresh", "2;URL=ContMana.jsp"); 83 84 //response.getWriter().append("Served at: ").append(request.getContextPath()); 85 } 86 87 /** 88 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 89 */ 90 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 91 // TODO Auto-generated method stub 92 doGet(request, response); 93 } 94 95 }
表现层jsp页面,包括:
1,显示联系人列表及添加 ContMana.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="com.haq.*" %> 4 <%@ page import="com.haq.dao.*" %> 5 <%@ page import="com.haq.comm.*" %> 6 <%@ page import="java.util.*" %> 7 8 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 9 <html> 10 <head> 11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 12 <title>联系人管理</title> 13 14 <script type="text/javascript" src="jquery-1.5.2.min.js"></script> 15 16 <script type="text/javascript"> 17 18 //验证表单数据函数 19 function checkform() 20 { 21 var name = form1.name.value; 22 var tel = form1.tel.value; 23 var group = form1.groupid.value; 24 25 var zhz = /\d{11}/; 26 27 if(name == "" || $.trim(name) == "") 28 { 29 alert("姓名不能为空"); 30 return false; 31 } 32 else if(tel == "" || $.trim(tel) == "") 33 { 34 alert("电话不能为空"); 35 return false; 36 } 37 else if(!zhz.test(tel)) 38 { 39 alert("电话格式错误"); 40 return false; 41 } 42 else if(group == "" || $.trim(group) == "") 43 { 44 alert("分组不能为空"); 45 return false; 46 } 47 else 48 { 49 return true; 50 } 51 52 } 53 54 //验证删除 55 function deletecheck() 56 { 57 return(confirm("确定删除联系人吗?")) 58 } 59 60 //修改联系人 61 62 //显示添加表单 63 64 $(document).ready(function(e) { 65 $("#show").click(function() 66 { 67 $("#add").toggle(); 68 } 69 ); 70 }); 71 72 73 74 </script> 75 76 </head> 77 78 79 <body> 80 <!--显示联系人表格--> 81 <table border="1" width="300"> 82 <tr> 83 <td></td> 84 <td>姓名</td> 85 <td>电话</td> 86 <td>分组</td> 87 </tr> 88 <% 89 //查询联系人信息 90 //实例化集合 91 ArrayList<Contacts> arr1 = new ArrayList<Contacts>(); 92 //实例化数据库操作类 93 ContDal cd = new ContDal(); 94 95 arr1 = cd.selectContList(); 96 97 if(arr1 != null) 98 { 99 for(Contacts c : arr1) 100 { 101 %> 102 <tr> 103 <td> 104 <a href="updateCont.jsp?id=<%=c.getId() %>" >编辑</a> 105 <a href="DeleteCont?id=<%=c.getId() %>" onclick="return deletecheck()">删除</a> 106 </td> 107 <td><%=c.getName() %></td> 108 <td><%=c.getTel() %></td> 109 110 <td> 111 <% 112 int gid = c.getGroupid(); 113 //实例化数据操作类 114 GroupDal gd1 = new GroupDal(); 115 116 Groups g1 = gd1.seleteGroups(gid); 117 118 out.print(g1.getName()); 119 %> 120 </td> 121 122 </tr> 123 124 <% 125 126 } 127 } 128 129 %> 130 131 </table> 132 <br> 133 <input type="button" id="show" name="show" value="添加联系人"> 134 135 <!--添加联系人表单--> 136 <div id="add" style="display:none"> 137 138 <form action="InsertCont" id="form1" name="form1" onsubmit="return checkform()" method="post"> 139 140 姓名:<input type="text" id="name" name="name"/><br> 141 电话:<input type="text" name="tel" id="tel" maxlength="11" /><br> 142 分组:<select id="groupid" name="groupid"> 143 <% 144 145 //查询分组信息 146 147 GroupDal gd = new GroupDal(); 148 149 ArrayList<Groups> arr = gd.seleteGroupsList(); 150 151 //遍历输出 152 if(arr != null) 153 { 154 for(Groups g : arr ) 155 { 156 %> 157 <option value="<%=g.getId()%>"><%=g.getName() %></option> 158 159 <% 160 } 161 } 162 163 %> 164 </select><br> 165 166 <input type="submit" value="添加"/> 167 168 </form> 169 170 </div> 171 172 </body> 173 </html>
2,修改联系人 updateCont.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 <%@ page import="com.haq.*" %> 5 <%@ page import="com.haq.dao.*" %> 6 <%@ page import="com.haq.comm.*" %> 7 <%@ page import="java.util.*" %> 8 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 9 <html> 10 <head> 11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 12 <title>修改联系人</title> 13 14 <script type="text/javascript" src="jquery-1.5.2.min.js"></script> 15 16 <script type="text/javascript"> 17 18 //验证表单数据函数 19 function checkform() 20 { 21 var name = form1.name.value; 22 var tel = form1.tel.value; 23 var group = form1.groupid.value; 24 25 var zhz = /\d{11}/; 26 27 if(name == "" || $.trim(name) == "") 28 { 29 alert("姓名不能为空"); 30 return false; 31 } 32 else if(tel == "" || $.trim(tel) == "") 33 { 34 alert("电话不能为空"); 35 return false; 36 } 37 else if(!zhz.test(tel)) 38 { 39 alert("电话格式错误"); 40 return false; 41 } 42 else if(group == "" || $.trim(group) == "") 43 { 44 alert("分组不能为空"); 45 return false; 46 } 47 else 48 { 49 return true; 50 } 51 52 } 53 54 55 </script> 56 </head> 57 <body> 58 59 <!--修改联系人表单--> 60 61 62 63 64 <% 65 //接收参数id 66 String id = request.getParameter("id"); 67 68 if(id == null || id.trim().length() == 0) 69 { 70 out.print("参数错误"); 71 response.setHeader("refresh","1;url=ContMana.jsp"); 72 } 73 else 74 { 75 //查询联系人信息 76 //实例化Contacts 77 Contacts c = new Contacts(); 78 //实例化数据库操作类 79 ContDal cd = new ContDal(); 80 81 c = cd.seleteCont(Integer.parseInt(id)); 82 83 %> 84 85 <form action="UpdateCont?id=<%=c.getId() %>" id="form1" name="form1" onsubmit="return checkform()" method="post"> 86 87 姓名:<input type="text" id="name" name="name" value="<%=c.getName() %>"><br> 88 电话:<input type="text" name="tel" id="tel" maxlength="11" value="<%=c.getTel() %>"><br> 89 分组:<select id="groupid" name="groupid"> 90 91 <% 92 //查询分组信息 93 GroupDal gd = new GroupDal(); 94 95 ArrayList<Groups> arr = gd.seleteGroupsList(); 96 97 //遍历输出 98 if(arr != null) 99 { 100 for(Groups g : arr ) 101 { 102 %> 103 <option value="<%=g.getId()%>" <%= ((c.getGroupid()==g.getId())? "selected" : "" ) %>><%=g.getName() %></option> 104 105 <% 106 } 107 } 108 109 %> 110 </select><br> 111 112 <input type="submit" value="修改"/> 113 <% 114 } 115 %> 116 </form> 117 </body> 118 119 </html>
标签:
原文地址:http://www.cnblogs.com/dirgo/p/5071542.html