整体思路:  在项目首页写一个超链接,连接地址为一个servlet(ServletDome2)-->(通过service层和dao层到数据库中查询并获取联系人表中的所有联系人的所有信息),将获取到的信息通过重定向,传给一个展示所有联系人信息的jsp(show.jsp)-->(展示所有联系人的信息,并且每个联系人后面都有两个操作超链接按钮(修改和删除),页面下面还有一个添加联系人的超链接按钮),如果用户点击了修改按钮(此按钮指向一个可以查询到该联系人所有供修改信息的servlet(ServletSelect1))-->先查询到该联系人的信息,并将信息传给供用户修改联系人信息的jsp(update.jsp)以供用户查看并修改联系人信息(该jsp展示页面中的联系人信息如果被修改则数据库中的信息也将随之修改否则就还是之前的信息,此页面有三个按钮可以分别提交,重置和返回).当用户在该页面修改完成点提交按钮后,会弹出确认提示框,再次点击确认后,会将修改内容传给可以修改信息的servlet(ServletUpdate),再接收传来的数据封装到对象中,并调用service层对象的修改方法将对象作为方法参数传入,然后service层再传给dao层,在dao层进行对数据库的修改操作并将结果返回.-->在ServletUpdate中获取修改后返回的结果进行判断,并在页面给出相应的提示信息.

删除的操作步骤和修改的几乎一样.添加的步骤相对比较简单了.

注意:联系人的id为主键且自增不可以被修改.联系人名字不可以被修改.在删除操作时页面显示的信息都为只读.

首页超链接的代码略...

jsp的相应参考代码为:

显示信息的:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%--jsp的指令tagLib引入--%>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head><!-- 指定字符集 --><meta charset="utf-8"><!-- 使用Edge最新的浏览器的渲染方式 --><meta http-equiv="X-UA-Compatible" content="IE=edge"><!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。width: 默认宽度与设备的宽度相同initial-scale: 初始的缩放比,为1:1 --><meta name="viewport" content="width=device-width, initial-scale=1"><!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --><title>Bootstrap模板</title><!-- 1. 导入CSS的全局样式 --><link href="../css/bootstrap.min.css" rel="stylesheet"><!-- 2. jQuery导入,建议使用1.9以上的版本 --><script src="../js/jquery-2.1.0.min.js"></script><!-- 3. 导入bootstrap的js文件 --><script src="js/bootstrap.min.js"></script><style type="text/css">td, th {text-align: center;}</style>
</head>
<body>
<div class="container">
<h3 style="text-align: center">显示所有联系人</h3>
<table border="1" class="table table-bordered table-hover">
<tr class="success"><th>编号</th><th>姓名</th><th>性别</th><th>年龄</th><th>籍贯</th><th>QQ</th><th>邮箱</th><th>操作</th>
</tr><c:if test="${not empty list}"><c:forEach items="${list}" var="contact"><tr><td>${contact.id}</td><td>${contact.name}</td><td>${contact.sex}</td><td>${contact.age}</td><td>${contact.address}</td><td>${contact.qq}</td><td>${contact.email}</td>                   <%--将id属性值传回给服务器--%><td><a class="btn btn-default btn-sm" href= "${pageContext.servletContext.contextPath}/ss1?id=${contact.id}">修改</a>&nbsp;<a class="btn btn-default btn-sm"href="${pageContext.servletContext.contextPath}/ss2?id=${contact.id}">删除</a></td></tr></c:forEach></c:if><tr><td colspan="8" align="center"><a class="btn btn-primary" href="${pageContext.servletContext.contextPath}/jsps/add.jsp">添加联系人</a></td></tr></table></div></body></html>

修改信息的参考代码为:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head><base href="<%=basePath%>"/><!-- 指定字符集 --><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>修改用户</title><link href="css/bootstrap.min.css" rel="stylesheet"><script src="js/jquery-2.1.0.min.js"></script><script src="js/bootstrap.min.js"></script></head>
<body>
<div class="container" style="width: 400px;"><h3 style="text-align: center;">修改联系人</h3><%--οnsubmit="javascript: if(confirm('是否提交')){return true}else{return false}" 弹出确认框询问--%><form action="${pageContext.servletContext.contextPath}/update?id=${contact.id}" method="post" οnsubmit="javascript: if(confirm('是否提交')){return true}else{return false}"><div class="form-group"><label for="name">姓名:</label><input type="text" class="form-control" id="name" name="name"  readonly="readonly" value="${contact.name}" placeholder="请输入姓名" /></div><div class="form-group"><label>性别:</label><%--<%=concat.getSex()=="man"?" checked":""%> 展示选中的单选框--%><input type="radio" name="sex" value="男" ${contact.sex == "男"?"checked":""}  />男<input type="radio" name="sex" value="女" ${contact.sex == "女"?"checked":""} />女</div><div class="form-group"><label for="age">年龄:</label><input type="text" class="form-control" id="age"  name="age" value="${requestScope.contact.age}" placeholder="请输入年龄" /></div><div class="form-group"><label >籍贯:</label><select name="address" class="form-control" ><option value="广东" ${contact.address == "广东"?"selected":""}>广东</option><option value="广西" ${contact.address == "广西"?"selected":""}>广西</option><option value="湖南" ${contact.address == "湖南"?"selected":""}>湖南</option></select></div><div class="form-group"><label >QQ:</label><input type="text" class="form-control" name="qq" value="${contact.qq}" placeholder="请输入QQ号码"/></div><div class="form-group"><label >Email:</label><input type="text" class="form-control" name="email" value="${contact.email}" placeholder="请输入邮箱地址"/></div><div class="form-group" style="text-align: center"><input class="btn btn-primary" type="submit" value="提交"  /><input class="btn btn-default" type="reset" value="重置" /><input class="btn btn-default" type="button" value="返回"  οnclick="window.history.go(-1)"/></div></form>
</div>
</body>
</html>

删除联系人的代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head><base href="<%=basePath%>"/><!-- 指定字符集 --><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><title>修改用户</title><link href="css/bootstrap.min.css" rel="stylesheet"><script src="js/jquery-2.1.0.min.js"></script><script src="js/bootstrap.min.js"></script></head>
<body>
<div class="container" style="width: 400px;"><h3 style="text-align: center;">修改联系人</h3><%--οnsubmit="javascript: if(confirm('是否提交')){return true}else{return false}" 弹出确认框询问--%><form action="${pageContext.servletContext.contextPath}/delete?id=${contact.id}" method="post" οnsubmit="javascript: if(confirm('真的删除该联系人吗???')){return true}else{return false}"><div class="form-group"><label for="name">姓名:</label><input type="text" class="form-control" id="name" name="name"  readonly="readonly" value="${contact.name}" placeholder="请输入姓名" /></div><div class="form-group"><label>性别:</label><%--<%=concat.getSex()=="man"?" checked":""%> 展示选中的单选框--%><input type="radio" name="sex" readonly="readonly" value="男" ${contact.sex == "男"?"checked":""}  />男<input type="radio" name="sex" readonly="readonly" value="女" ${contact.sex == "女"?"checked":""} />女</div><div class="form-group"><label for="age">年龄:</label><input type="text" class="form-control" id="age"  name="age" readonly="readonly" value="${requestScope.contact.age}" placeholder="请输入年龄" /></div><div class="form-group"><label >籍贯:</label><select name="address" readonly="readonly" class="form-control" ><option value="广东" ${contact.address == "广东"?"selected":""}>广东</option><option value="广西" ${contact.address == "广西"?"selected":""}>广西</option><option value="湖南" ${contact.address == "湖南"?"selected":""}>湖南</option></select></div><div class="form-group"><label >QQ:</label><input type="text" class="form-control" name="qq"  readonly="readonly"value="${contact.qq}" placeholder="请输入QQ号码"/></div><div class="form-group"><label >Email:</label><input type="text" class="form-control" name="email" readonly="readonly" value="${contact.email}" placeholder="请输入邮箱地址"/></div><div class="form-group" style="text-align: center"><input class="btn btn-primary" type="submit" value="确定删除"  /><input class="btn btn-default" type="button" value="放弃删除"  οnclick="window.history.go(-1)"/></div></form>
</div>
</body>
</html>

添加联系人的代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- HTML5文档-->
<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head><!-- 指定字符集 --><meta charset="utf-8"><!-- 使用Edge最新的浏览器的渲染方式 --><meta http-equiv="X-UA-Compatible" content="IE=edge"><!-- viewport视口:网页可以根据设置的宽度自动进行适配,在浏览器的内部虚拟一个容器,容器的宽度与设备的宽度相同。width: 默认宽度与设备的宽度相同initial-scale: 初始的缩放比,为1:1 --><meta name="viewport" content="width=device-width, initial-scale=1"><!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! --><title>添加用户</title><!-- 1. 导入CSS的全局样式 --><link href="../css/bootstrap.min.css" rel="stylesheet"><!-- 2. jQuery导入,建议使用1.9以上的版本 --><script src="../js/jquery-2.1.0.min.js"></script><!-- 3. 导入bootstrap的js文件 --><script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container"><center><h3>添加联系人</h3></center><form action="${pageContext.servletContext.contextPath}/add" method="post" οnsubmit="javascript: if(confirm('是否确认添加')){return true}else{return false}"><div class="form-group"><label for="name">姓名:</label><input type="text" class="form-control" id="name" name="name" placeholder="请输入姓名"></div><div class="form-group"><label>性别:</label><input type="radio" name="sex" value="男" checked="checked"/>男<input type="radio" name="sex" value="女"/>女</div><div class="form-group"><label for="age">年龄:</label><input type="text" class="form-control" id="age" name="age" placeholder="请输入年龄"></div><div class="form-group"><label >籍贯:</label><select name="address" class="form-control" id="jiguan"><option value="广东">广东</option><option value="广西">广西</option><option value="湖南">湖南</option></select></div><div class="form-group"><label >QQ:</label><input type="text" class="form-control" name="qq" placeholder="请输入QQ号码"/></div><div class="form-group"><label >Email:</label><input type="text" class="form-control" name="email" placeholder="请输入邮箱地址"/></div><div class="form-group" style="text-align: center"><input class="btn btn-primary" type="submit" value="提交" /><input class="btn btn-default" type="reset" value="重置" /><input class="btn btn-default" type="button" value="返回" οnclick="window.history.go(-1)" /></div></form>
</div>
</body>
</html>

Java代码:

web层:

-->1:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;public class ServletDemo2 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {ShowService showService = new ShowService();List<Contact> list=showService.show();request.getSession().setAttribute("list",list);response.sendRedirect(request.getContextPath()+"/jsps/show.jsp");//重定向}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);}
}

-->2:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;public class ServletSelect1 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//查询要修改的数据库的信息int id = Integer.parseInt(request.getParameter("id"));ShowService showService = new ShowService();Contact contact = showService.select(id);request.setAttribute("contact",contact);request.getRequestDispatcher("/jsps/update.jsp").forward(request,response);//请求转发}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);}
}

-->3:

import org.apache.commons.beanutils.BeanUtils;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;public class ServletUpdate extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8");//防止中文乱码response.setContentType("text/html;charset=utf-8");//防止中文乱码//获取用户传过来的修改用户的信息int id = Integer.parseInt(request.getParameter("id"));Map<String, String[]> map = request.getParameterMap();//获取页面提交过来的修改的数据Contact contact = new Contact();try {BeanUtils.populate(contact,map);//将修改后的数据封装到对象中ShowService showService = new ShowService();int count = showService.update(id, contact);if (count > 0) {response.getWriter().print("修改成功");} else {response.getWriter().print("修改失败");}} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);}
}

-->4:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;public class ServletSelect2 extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//查询要删除的数据库的信息int id = Integer.parseInt(request.getParameter("id"));ShowService showService = new ShowService();Contact contact = showService.select(id);request.setAttribute("contact",contact);request.getRequestDispatcher("/jsps/delete.jsp").forward(request,response);//请求转发}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);}
}-->5:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;public class ServletDelete extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8");//防止中文乱码response.setContentType("text/html;charset=utf-8");//防止中文乱码//获取用户传过来的修改用户的信息int id = Integer.parseInt(request.getParameter("id"));//根据id给页面展示要删除的联系人信息询问是否要删除此联系人int count = new ShowService().delete(id);if (count > 0) {response.getWriter().print("联系人删除成功");} else {response.getWriter().print("联系人删除失败");}}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);}
}

-->6:

import org.apache.commons.beanutils.BeanUtils;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;public class ServletAdd extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8");//防止中文乱码response.setContentType("text/html;charset=utf-8");//防止中文乱码Map<String, String[]> map = request.getParameterMap();//获取页面提交过来的数据Contact contact = new Contact();try {BeanUtils.populate(contact,map);//将添加的数据封装到对象中ShowService showService = new ShowService();int count = showService.add(contact);if (count > 0) {response.getWriter().print("添加成功");} else {response.getWriter().print("添加失败");}} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);}
}
service层:
import java.util.List;public class ShowService {ShowDao showDao = new ShowDao();public List<Contact> show() {return showDao.show();}public Contact select(int id) {return showDao.select(id);}public int  update(int id, Contact contact) {return showDao.update(id,contact);}public int delete(int id) {return showDao.delete(id);}public int add(Contact contact) {return showDao.add(contact);}
}

dao层:

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;import java.util.List;public class ShowDao {JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcUtil.getDataSource());public List<Contact> show() {String sql = "select * from contact;";//将查询到的所有联系人的信息保存到泛型为contact对象的集合中List<Contact> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Contact>(Contact.class));return list;}public Contact select(int id) {String sql = "select* from contact where id =?;";Contact contact = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Contact>(Contact.class), id);return contact;}public int  update(int id, Contact contact) {// update 表名 set 字段名 = 值,字段名 = 值 where 字段名=值; 按条件改String sql = "update contact set name=?,sex=?, age=?, address=?,qq=?,  email=? where id =? ";int count = jdbcTemplate.update(sql, contact.getName(), contact.getSex(), contact.getAge(),contact.getAddress(), contact.getQq(), contact.getEmail(), id);return count;}public int  delete(int id) {String sql = "delete from contact where id =? ;";int count = jdbcTemplate.update(sql, id);return count;}public int add(Contact contact) {String sql = "insert into contact values(null,?,?,?,?,?,?)";int count = jdbcTemplate.update(sql, contact.getName(), contact.getSex(), contact.getAge(), contact.getAddress(), contact.getQq(), contact.getEmail());return count;}
}
utils和domain包内的相应代码和数据库数据以及页面展示的效果全部省略.

Java的web项目练习,展示数据库中所有联系人的信息,并实现添加,删除和修改功能相关推荐

  1. jsp项目在idea需要导入什么依赖_idea中的java web项目(添加jar包介绍)和java maven web项目目录结构...

    java web项目 web项目下web根目录名称是可以更改的 idea中新建java web项目,默认src为Sources Root,当然也可以手动改,在Sources Root下右键只能新建Pa ...

  2. ASP.NET Core Web项目连接MySQL数据库

    作者在新建了一个ASP.NET Core Web项目的基础上,想连接本地的Mysql数据库,参考了很多博客,各种各样的说法都有,多少让人有感凌乱!自己最后捣鼓成功了!所以写一篇博客,以便后人查阅! 操 ...

  3. 通过loganalyzer展示数据库中的日志

    目的:通过loganalyzer展示数据库中的日志 准备环境:   CentOS7_1:用来生成日志   CentOS7_2:用来存放日志的数据库   CentOS7_3:LAP服务器 第一步:在Ce ...

  4. 查询数据库中的表结构信息

    查询数据库中的表结构信息 SELECT COLUMN_NAME 列名, COLUMN_TYPE 数据类型, DATA_TYPE 字段类型, CHARACTER_MAXIMUM_LENGTH 长度, I ...

  5. 数据库索引统计信息不一致_列存储索引增强功能–克隆数据库中的索引统计信息更新

    数据库索引统计信息不一致 SQL Server was launched in 1993 on WinNT and it completed its 25-year anniversary recen ...

  6. mysql 查询数据库中所有表的信息

    查询数据库中的所有表格信息: SELECT table_name, table_comment, create_time, update_time FROM information_schema.ta ...

  7. Java 项目JDBC 链接数据库中会出现的错误

    1.出现的地方 1 package com.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 impor ...

  8. 实现以最快速度搭建springboot后台web项目并连通数据库实现控制层与服务层与dao层的select查询...

    背景 Servlet的出现 上世纪90年代,随着Internet和浏览器的飞速发展,基于浏览器的B/S模式随之火爆发展起来.最初,用户使用浏览器向WEB服务器发送的请求都是请求静态的资源,比如html ...

  9. Java springmvc web项目,基于maven的hello world入门级项目使用IntelliJ IDEA 2017版本

    IntelliJ IDEA使用教程 (总目录篇) 使用这个IntelliJ IDEA创建一个简单的Java web maven项目,我在前面的文章里面已经示范过了. 地址:IntelliJ IDEA ...

最新文章

  1. 2022-2028年中国实心轮胎行业市场研究及前瞻分析报告
  2. C#精髓【月儿原创】第三讲 C#泛型有什么好处
  3. PAT甲级题目翻译+答案 AcWing(高精度)
  4. 用python做数据分析pdf_利用python进行数据分析pdf
  5. Java原来如此-随机数
  6. 前端学习(3345):设计模式之工厂模式2
  7. zlib数据格式及解压缩实现
  8. 公需科目必须学吗_要考电工证吗?电工技术必须学的33招,电工技术知识
  9. 支持python开发的环境有哪些特点_Python虚拟环境详细教程,一篇带你入坑
  10. 去掉 iOS 导航栏返回按钮文本三种方案
  11. 项目建设做好服务器,我院数字化建设项目(一期)服务器系统切换工作顺利完成...
  12. Charles(1) 请求转发
  13. 【珍藏版】震撼发布2017年Android百大框架排行榜
  14. 常见的数据库有哪些?
  15. cad相对坐标快捷键_cad角度快捷键(cad角度命令怎么输入)
  16. 如何正确使用螳螂教育CRM系统?
  17. Summery about show input info bar of MTK 分享
  18. 为什么都推崇测试先行?论软件测试先行的12个好处
  19. 转:Linux系统管理学习路线图
  20. 大炮打蚊子(c语言易懂版)

热门文章

  1. Java期末复习总结
  2. 微信小程序调试模式请求失败,真机模式、调试带控制台模式却都成功
  3. Unity Animator笔记
  4. 百度python小白逆袭大神系列课程day5——爱奇艺《青你2》评论爬取并分析
  5. 二阶锥潮流Distfolw(OPF)
  6. ViewPager+FragmentPagerAdapter实现微信5.2.1主界面架构
  7. windows bat脚本注册成服务
  8. 数字化会员留存怎么做?会员积分体系如何设计?
  9. 时光倒流童年_使用Microsoft Excel时光倒流
  10. VoiceOver经验