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

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

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

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

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

jsp的相应参考代码为:

显示信息的:

Bootstrap模板

td, th {

text-align: center;

}

显示所有联系人

编号姓名性别年龄籍贯QQ邮箱操作
${contact.id}${contact.name}${contact.sex}${contact.age}${contact.address}${contact.qq}${contact.email}修改 

href="${pageContext.servletContext.contextPath}/ss2?id=${contact.id}">删除

添加联系人

修改信息的参考代码为:

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

修改用户

修改联系人

姓名:

性别: 展示选中的单选框--%>

年龄:

籍贯:

广东

广西

湖南

QQ:

Email:

删除联系人的代码:

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

修改用户

修改联系人

姓名:

性别: 展示选中的单选框--%>

年龄:

籍贯:

广东

广西

湖南

QQ:

Email:

添加联系人的代码:

添加用户

添加联系人

姓名:

性别:

年龄:

籍贯:

广东

广西

湖南

QQ:

Email:

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 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 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 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 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 show() {

String sql = "select * from contact;";

//将查询到的所有联系人的信息保存到泛型为contact对象的集合中

List list = jdbcTemplate.query(sql, new BeanPropertyRowMapper(Contact.class));

return list;

}

public Contact select(int id) {

String sql = "select* from contact where id =?;";

Contact contact = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(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包内的相应代码和数据库数据以及页面展示的效果全部省略.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值