修改思维逻辑:
两步:1.数据的回显;2.数据的更新
1.点击修改跳转到servlet,后端根据id查询到该商品的信息;
2.把商品信息添加到request作用域中请求转发到信息展示页面;
3.用户根据需求修改表单中的字段,重新提交表单;
4.编写后端servlet获取用户最新修订的商品信息,执行数据库的修改操作;
5.重新请求查询所有商品,跳转到商品信息主页;
zhuye.jsp页面:添加修改a标签
showGoods.jsp页面:编写商品修改页面
商品信息修改
商品信息修改
GoodsDao.java页面:1.添加selectById()方法执行JDBC查询操作
public Goods selectById(int gid) {
Goods goods = null;
try {
//加载驱动
Class.forName(driver);
//获取连接
con = DriverManager.getConnection(url, user, password);
//编写sql语句
String sql = "select * from t_goods where gid = ?";
ps = con.prepareStatement(sql);
ps.setObject(1,gid);
rs = ps.executeQuery();
if (rs.next()) {
goods = new Goods();
goods.setGid(rs.getInt("gid"));
goods.setGname(rs.getString("gname"));
goods.setPrice(rs.getDouble("price"));
goods.setMark(rs.getString("mark"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return goods;
}
2.编写JDBC根据id修改操作
public int update(Goods goods){
try {
//加载驱动
Class.forName(driver);
//获取连接
con = DriverManager.getConnection(url, user, password);
//编写sql语句
String sql = "update t_goods set gname=?,price=?,mark=? where gid =?";
ps = con.prepareStatement(sql);
ps.setObject(1,goods.getGname());
ps.setObject(2,goods.getPrice());
ps.setObject(3,goods.getMark());
ps.setObject(4,goods.getGid());
row = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return row;
}
FindById.java页面:地址请求;根据id获取商品信息
package com.su.servlet;
import com.su.Bean.Goods;
import com.su.dao.GoodDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/findById")//地址请求
public class FindById extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取gid
int gid = Integer.parseInt(request.getParameter("gid"));//强制转换
//根据gid查询商品信息
GoodDao goodsDao = new GoodDao();//实例化一个对象
Goods goods = goodsDao.selectById(gid);//调用方法
//
if(goods!=null){
//查询商品信息成功,存入request域中,然后请求转发到页面展示数据
request.setAttribute("goods",goods);
request.getRequestDispatcher("showGoods.jsp").forward(request,response);
}else{
request.setAttribute("error_msg","修改出了异常!");
request.getRequestDispatcher("error.jsp").forward(request,response);
}
}
}
UpdateGoods.java:地址请求;获取数据,封装
package com.su.servlet;
import com.su.Bean.Goods;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/updateGoods")
public class UpdateGoods extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");//设置响应的编码格式
//获取表单提交的数据,封装到goods对象中
Goods goods=new Goods();
goods.setGid(Integer.parseInt(request.getParameter("gid")));
goods.setGname(request.getParameter("gname"));
goods.setPrice(Double.parseDouble(request.getParameter("price")));
goods.setMark(request.getParameter("mark"));
//输出
System.out.println(goods);
//执行数据库JDBC修改操作
GoodDao goodsDao = new GoodDao();
int row = goodsDao.update(goods);
if (row>0){
request.getRequestDispatcher("selectAllGoods").forward(request,response);
}else{
request.setAttribute("error_msg","修改出现问题!");
request.getRequestDispatcher("error.jsp").forward(request,response);
}
}
}
登录后跳转到主页,点击修改跳转到商品修改页面
点击修改,跳到主页并刷新数据;
控制台日志
模糊查询思维逻辑:
1.在zhuye.jsp定义搜索的表单页面,请求地址;
2.在GoodsDao.java页面定义seach方法,编写JDBC模糊查询,便于调用;
public List seach(String keyword){
List goodsList = new ArrayList<>();
try {
//加载驱动
Class.forName(driver);
//获取连接
con = DriverManager.getConnection(url, user, password);
//编写sql语句
String sql = "select * from t_goods where gname like concat('%',?,'%')";
ps = con.prepareStatement(sql);
ps.setObject(1,keyword);
rs = ps.executeQuery();
while (rs.next()) {
Goods goods = new Goods();
goods.setGid(rs.getInt("gid"));
goods.setGname(rs.getString("gname"));
goods.setPrice(rs.getDouble("price"));
goods.setMark(rs.getString("mark"));
goodsList.add(goods);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return goodsList;
}
3.Seach.java中地址请求,获取用户搜索的关键词,执行模糊查询,把查询到的商品信息存到session中起别名,之后跳转到主页展示信息;
@WebServlet("/seach")
public class Seach extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//获取表单提交的数据,封装到goods对象中
String keyword = request.getParameter("keyword");
//执行数据库JDBC模糊查询
GoodDao goodsDao = new GoodDao();
List goodsList = goodsDao.seach("%" + keyword + "%");
System.out.println(goodsList);
//把模糊查询到的商品信息集合存储到session中
HttpSession session= request.getSession();
session.setAttribute("goodsList",goodsList);
//跳转到主页进行信息展示
response.sendRedirect("zhuye.jsp");
}
}
在文本框输入查询的关键词,点击搜索
点击搜索后,查询出包含关键词的商品信息
控制台日志信息