jsp+servlet手机管理(增加和修改二)

在以上篇我们写了实现列表功能

一 :新增功能

注意新增 不仅要增加手机 还要选择手机的品牌 , 我们可以将手机品牌做成下拉列表框

首先 在t_list.jsp列表页面新增一个a标签 跳转新增界面

<a href="<%=request.getContextPath()%>/tel?method=tosave">新增</a>

虽然样式有点丑 ,勉强还能看 。 我们跳到新增界面后

先实现添加时品牌动态从数据库读取

Dao

在dao层创建TbrandDao 接口

List<Tbrand> findAll();

接口实现类 TbrandDaoImpl

public List<Tbrand> findAll() {Connection connection=null;PreparedStatement preparedStatement=null;ResultSet resultSet=null;String sql="select * from t_brand";List<Tbrand> tbrandList=null;Tbrand tbrand=null;try {tbrandList=new ArrayList<>();connection = JDBCUtil.getConnection();preparedStatement = connection.prepareStatement(sql);resultSet = preparedStatement.executeQuery();while (resultSet.next()) {tbrand=new Tbrand();tbrand.setId(resultSet.getInt("id"));tbrand.setDname(resultSet.getString("dname"));tbrandList.add(tbrand);}}catch (Exception e){e.printStackTrace();}finally {JDBCUtil.close(resultSet,preparedStatement,connection);}return tbrandList;
}

与我们上一期的列表有些许相似

service

然后实现我们的service层

TbrandService 接口

List<Tbrand> findAll();

TbrandService 接口实现类 TbrandServiceImpl

public class TbrandServiceImpl implements TbrandService {private TbrandDao tbrandDao=new TbrandDaoImpl();@Overridepublic List<Tbrand> findAll() {return tbrandDao.findAll();}
}

Telservlet

在doPost方法中 还是老样子 判断请求地址

String method = request.getParameter("method");if ("findAll".equals(method)){findAll(request,response);}else if ("tosave".equals(method)){tosave(request,response);}
}private void tosave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {List<Tbrand> tbrandList = tbrandService.findAll();request.setAttribute("tbrandList",tbrandList);request.getRequestDispatcher("/t_save.jsp").forward(request,response);
}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);
}

创建 新增页面 t_save.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>新增</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/tel?method=saveTel" method="post">名称:<input type="text" name="name"><br>品牌:<select name="bid"><c:forEach items="${requestScope.tbrandList}" var="tbrandList"><option value="${tbrandList.id}">${tbrandList.dname}</option></c:forEach>
</select>
<input type="submit" value="提交">
</form>
</body>
</html>

接下来我们实现新增功能

在TelDao里 添加新方法

//新增
void save(Tel tel);

在TelDaoImpl接口实现类实现该方法

@Override
public void save(Tel tel) {Connection connection=null;PreparedStatement preparedStatement=null;String sql="insert into t_tel set name=?,bid=?";try {connection = JDBCUtil.getConnection();preparedStatement = connection.prepareStatement(sql);preparedStatement.setString(1,tel.getName());preparedStatement.setInt(2,tel.getBid());//更新操作preparedStatement.executeUpdate();}catch (Exception e){e.printStackTrace();}finally {JDBCUtil.close(null,preparedStatement,connection);}
}

下面就是TelService接口 添加新方法:

 void save(Tel tel);

TelServiceImpl 实现 新方法

public void save(Tel tel) {telDao.save(tel);
}

Telservlet

还是老样子 判断请求参数

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String method = request.getParameter("method");
if ("findAll".equals(method)){findAll(request,response);
}else if ("tosave".equals(method)){tosave(request,response);
}else if ("saveTel".equals(method)){saveTel(request,response);
}
}private void saveTel(HttpServletRequest request, HttpServletResponse response) throws IOException {String name = request.getParameter("name");String bid = request.getParameter("bid");Tel tel = new Tel();tel.setBid(Integer.parseInt(bid));tel.setName(name);telService.save(tel);response.sendRedirect(request.getContextPath()+"/tel?method=findAll");}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);  //千万不要忘记}

二: 更新功能

1. 我们先点击更新按钮实现一个数据的回显

<td><button οnclick="update(${vo.id})">更新</button>
</td>
<script>function update(id) {location.href="<%=request.getContextPath()%>/tel?method=toUpdate&id="+id}
</script>

根据Id来查站我们的数据

TelDao创建新方法

Tel findById(Integer id);

TelDaoImpl实现接口方法

public Tel findById(Integer id) {Connection connection=null;PreparedStatement preparedStatement=null;ResultSet resultSet=null;String sql="select id,name,bid from t_tel where id=?";Tel tel=null;try {connection = JDBCUtil.getConnection();preparedStatement = connection.prepareStatement(sql);//给id一个占位符preparedStatement.setInt(1,id);resultSet = preparedStatement.executeQuery();if (resultSet.next()) {tel=new Tel();tel.setId(resultSet.getInt("id"));tel.setName(resultSet.getString("name"));tel.setBid(resultSet.getInt("bid"));}}catch (Exception e){e.printStackTrace();}finally {JDBCUtil.close(resultSet,preparedStatement,connection);}return tel;
}

Telservice 添加新方法

Tel findById(Integer id);

TelServiceImpl 实现该方法

public Tel findById(Integer id) {return telDao.findById(id);
}

Telservlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String method = request.getParameter("method");
if ("findAll".equals(method)){findAll(request,response);
}else if ("tosave".equals(method)){tosave(request,response);
}else if ("saveTel".equals(method)){saveTel(request,response);
}else if ("toUpdate".equals(method)){toUpdate(request,response);
}}
private void toUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String id = request.getParameter("id");Tel byId = telService.findById(Integer.parseInt(id));//品牌的回显List<Tbrand> tbrands = tbrandService.findAll();//存入作用域request.setAttribute("byId",byId);request.setAttribute("tbrands",tbrands);request.getRequestDispatcher("/t_update.jsp").forward(request,response);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);  //千万不要忘记}

t_update.jsp 页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>更新</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/tel?method=updateTel" method="post"><input type="hidden" name="id" value="${requestScope.byId.id}"><br>名称:<input type="text" name="name" value="${requestScope.byId.name}"><br>品牌:<select name="bid"><c:forEach items="${requestScope.tbrands}" var="tbs"><option value="${tbs.id}"<!--根据id来判断--><c:if test="${tbs.id==requestScope.byId.bid}">selected</c:if>>${tbs.dname}</option></c:forEach></select><input type="submit" value="提交">
</form>
</body>
</html>

2. 更新功能

TelDao

void updateTel(Tel tel);

TelDaoImpl

public void updateTel(Tel tel) {Connection connection=null;PreparedStatement preparedStatement=null;String sql="update t_tel set name=?, bid=? where id=?";try {connection = JDBCUtil.getConnection();preparedStatement = connection.prepareStatement(sql);preparedStatement.setString(1,tel.getName());preparedStatement.setInt(2,tel.getBid());preparedStatement.setInt(3,tel.getId());preparedStatement.executeUpdate();}catch (Exception e){e.printStackTrace();}finally {JDBCUtil.close(null,preparedStatement,connection);}
}

TelService

void updateTel(Tel tel);

TelServiceImpl

public void updateTel(Tel tel) {telDao.updateTel(tel);
}

Telservlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String method = request.getParameter("method");
if ("toUpdate".equals(method)){toUpdate(request,response);
}else if ("updateTel".equals(method)){updateTel(request,response);
}}private void updateTel(HttpServletRequest request, HttpServletResponse response) throws IOException {String id = request.getParameter("id");String name = request.getParameter("name");String bid = request.getParameter("bid");Tel tel = new Tel();tel.setId(Integer.parseInt(id));tel.setName(name);tel.setBid(Integer.parseInt(bid));telService.updateTel(tel);response.sendRedirect(request.getContextPath()+"/tel?method=findAll");}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,response);  //千万不要忘记}

jsp+servlet手机管理(增删改查 二)相关推荐

  1. MVC、JSP实现mysql的增删改查功能的封装和简陋的界面交互

    1.眼见为实 (1)欢迎界面,总索引:带下划线的三个都是链接 : (2)搜索界面:--有颜色的为链接 (3).点击上面图片中的修改链接之后就会弹出下面的修改界面: (4).修改用户信息的界面 (5). ...

  2. OA项目实战学习(3)——实现岗位管理增删改查

    我们这里注重实现功能,对页面不进行布局. 一.分析增删改查的功能: 添加.修改.删除成功后 要重定向到列表功能,这样在刷新页面时才不会出现"又做一次增.删.改"的操作. 列表与删除 ...

  3. python+django+sqlite3,不成熟的学生管理增删改查

    因为是边学边做的,所以有一些地方有了很麻烦的方法,登录验证也没做完,前端也很难看,只是做了基本的增删改查 1.新建project django-admin.py startproject studen ...

  4. PHP----练习-----新闻管理----增删改查

    练习-----新闻管理 题目要求如下: 做法: [1]建数据库 [2]封装类文件--------DBDA.class.php 1 <?php 2 class DBDA 3 { 4 public ...

  5. 使用JDB操作数据库—增删改查(二)

    一.使用jdbc操作数据库步骤: 注意:这里操作的是MySQL数据库! 1.创建Java项目,导入jdbc的jar包[ 具体操作步骤 ] 2.创建包(package): 右击项目的src文件夹,选中N ...

  6. mybatis3单表增删改查(二)——注解方式

    2019独角兽企业重金招聘Python工程师标准>>> 代码如下: 1.mybatis配置文件 <mappers><mapper class="org.z ...

  7. kibana客户端工具操作ElasticSearch(增删改查二)

    #不指定id情况下 ElasticSearch自动生成id PUT /lib/user/ {"first_name":"Douglas","last_ ...

  8. xmind角色管理增删改查测试用例编写

  9. 基于JSP的数据库增删改查实现

    基于JSP的数据库增删改查实现 一.JAVA包的设计 包 类 方法 entity PM25 Set,get dao BaseDao getConnection,close PM25Dao findAl ...

最新文章

  1. oracle exec 和 call 区别
  2. 春天来了,我也发“芽”了!
  3. 如何查看dede版本信息
  4. Python 技术篇 - 使用unicode_escape对js的escape()方法编码后的字符串进行解码实例演示
  5. MySQL show binlog events命令查看binlog日志内容
  6. 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)
  7. java.io.StreamCorruptedException: invalid type code: AC解决办法
  8. SharePoint 2013 图文开发系列之自定义字段
  9. 结构pop_宏旺半导体总结嵌入式存储封装技术SiP、SOC、MCP、PoP的区别
  10. 【写作技巧】如何写开题报告?
  11. Bzoj4542--Hnoi2016大数
  12. smobiler php对比,Smobiler的开发讲解
  13. 7-4 人民币兑换 (10 分)C语言
  14. 线性方程组/矩阵方程求解(方法汇总)
  15. entity、bo、vo、po、dto、pojo如何理解和区分?
  16. Coad和Yourdon面向对象的分析过程
  17. 智能体仿真中的BDI(belief-desire-intention)架构
  18. 计算机学院家长座谈会,计算机信息技术学院召开新生家长座谈会
  19. goodnotas,notability电子手帐笔记打印纸的颜色改变
  20. 索尼乐播投屏服务器未响应,乐播投屏电视没有声音怎么办?三招帮你解决令人困扰的问题!...

热门文章

  1. G. Count the Trains(思维set + 二分)
  2. 关于flask-SocketIO 开发部署详解
  3. 嗨 Jina,帮我画一幅高山流水图
  4. JSP验证码系列(数字验证码、英文与数字混合验证码、中文验证码、表达式验证码)
  5. python工业控制系统机柜_智慧职教2020Python程序设计(常州工业职业技术学院)答案...
  6. 记一次模拟生成数据发到kafka的bug
  7. 信息系统管理师、高级项目经理与计算机系统集成资质之间的关系
  8. javascript 常用的时间函数
  9. @Delete通过批量删除的方法
  10. 第一范式1NF、第二范式2NF、第三范式3NF详解