页面设计
修改页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改页面</title>
</head>
<body>
<form action="updategoods" method="post">
修改商品编号:<input type="text" name="gid" value="${goods.gid}" readonly="readonly"><br/>
修改商品名称:<input type="text" name="gname" value="${goods.gname}"><br/>
修改商品价格:<input type="number" step="0.01" name="price" value="${goods.price}"><br/>
修改商品信息:<input type="text" name="mark" value="${goods.mark}"><br/>
<input type="submit" value="修改商品">
</form>
</body>
</html>
主页搜索查询
<body>
<!-- 在页面导入jstl的核心 -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h2>欢迎来自${user.address}的${user.username}到主页</h2>
<table>
<a href="addGoods.jsp">添加商品</a>
<form action="seach" method="post">
<input type="text" name="key" value=""/>
<input type="submit" value="搜索"/>
</form>
<thead>
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品说明</th>
</tr>
</thead>
<tbody>
<c:forEach items="${goodsList}" var="goods">
<tr>
<td>${goods.gid}</td>
<td>${goods.gname}</td>
<td>${goods.price}</td>
<td>${goods.mark}</td>
<td>
<a href="del?gid=${goods.gid}">删除</a>
</td>
<td>
<a href="udate?gid=${goods.gid}">修改</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
编写JDBC
修改
public Boolean upateGoods(Goods goods) throws SQLException {
Boolean bool = null;
con = JDBCUtil.getCon();
//编写sql语句
String sql = "update t_goods set gname=?,price=?,mark=? where gid=?";
//获取sql语句
ps = con.prepareStatement(sql);
//传参
ps.setObject(1, goods.getGname());
ps.setObject(2, goods.getPrice());
ps.setObject(3, goods.getMark());
ps.setObject(4, goods.getGid());
int i = ps.executeUpdate();
if (i > 0) {
bool = true;
} else {
bool = false;
}
//关闭数据
JDBCUtil.close(ps,con);
return bool;
}
调用方法
修改
@WebServlet("/udate")
public class UpdateGoods extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//修改代码的格式
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//2.获取添加商品的信息
int gid = Integer.parseInt(req.getParameter("gid"));
//将商品信息添加到后端
Goodsdao goodsdao=new Goodsdao();
Goods goods=null;
try {
goods = goodsdao.Selectgoods(gid);
} catch (Exception e) {
e.printStackTrace();
}
req.setAttribute("goods",goods);
req.getRequestDispatcher("xiugai.jsp").forward(req,resp);
}
}
@WebServlet("/updategoods")
public class UpdateGoods1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//修改代码的格式
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//获取修改后的数据
Goods goods = null;
goods = new Goods();
goods.setGid(Integer.parseInt(req.getParameter("gid")));
goods.setGname(req.getParameter("gname"));
goods.setPrice(Double.parseDouble(req.getParameter("price")));
goods.setMark(req.getParameter("mark"));
System.out.println(goods);
Boolean bool = null;
//调用修改的方法
Updatedao updatedao = new Updatedao();
try {
bool = updatedao.upateGoods(goods);
} catch (Exception e) {
e.printStackTrace();
}
if (bool != false) {
req.getRequestDispatcher("GoodsSelectAll").forward(req, resp);
} else {
}
}
}
模糊查询
@WebServlet("/seach")
public class GoodsSelectlike extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//重编代码格式
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//获取页面的数值
String key = req.getParameter("key");
List<Goods> goodsList =null;
Goodsdao goodsdao=new Goodsdao();
try {
goodsList = goodsdao.Seach(key);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
if (goodsList!=null){
HttpSession session = req.getSession();
session.setAttribute("goodsList",goodsList);
resp.sendRedirect("zhuye.jsp");
}else{
}
}
}
修改的运行图
模糊查询的运行图