ideas to add
Requirement:
Add a record from the jsp page to the database and display it on the interface
Analysis:
1. Create a jsp page
2. Create a Servlet>addGoods method
1. Set the request encoding
2. Get the values of all parameters
3. Encapsulate the object 4.
Call the addGoods method
3. Create a Service interface>addGoods method
1. Call addGoods(Goods) Method
4. Create GoodsDao interface>addGoods method
1. Create sql statement
2. Get the result set
Code
jsp page
<%@ page import="java.util.List" %>
<%@ page import="com.wang.bean.Goods" %>
<%@ page import="com.wang.bean.User" %><%--
Created by IntelliJ IDEA.
User: 小m
Date: 2023/2/16
Time: 12:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!--在页面导入jstl的核心类库--->
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>主页</title>
</head>
<body>
<h2>欢迎来自${user.address}的${user.username}来到主页!</h2>
<a href="addGoods.jsp"></a>
<table>
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品介绍</th>
</tr>
<!--items:要遍历的集合元素 var:临时变量-->
<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=""></a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
SelectAllGoods
package com.wang.servlet;
import com.wang.bean.Goods;
import com.wang.dao.GoodsDao;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
@WebServlet("/selectAllGoods")
public class SelectAllGoods extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("SelectAllGoods-doGet...");
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.执行查询业务
// 2.查询所有商品信息
GoodsDao goodsDao = new GoodsDao();
List<Goods> goodsList = goodsDao.selectAllGoods();
System.out.println(goodsList);
// 3.把数据传递到前端页面
// 3.1通过request获取session对象,该对象可以向前端传输数据的容器
HttpSession session = request.getSession();
// 4.向session中存入商品信息集合
session.setAttribute("goodsList",goodsList);
String id = session.getId();
System.out.println("sessionID:"+id);
// 4.登录成功,跳转到登录页
response.sendRedirect("zhuye.jsp");
}
}
addGoods
public int addgoods(Goods goods) {
try {
con=JDBCUtil.getCon();
String sql="insert into t_goods(gname,price,mark) values (?,?,?)";
//
pstm = con.prepareStatement(sql);
pstm.setObject(1,goods.getGname());
pstm.setObject(2,goods.getPrice());
pstm.setObject(3,goods.getMark());
//
row=pstm.executeUpdate();
System.out.println(row);
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
JDBCUtil.close(con, pstm);
}
return row;
}
Delete ideas
Idea:
First, use javaScript to obtain the name of the selected record in the check box, store each selected record name in a variable, and then transfer it to the background for batch deletion.
<!--items:要遍历的集合元素 var:临时变量-->
<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="#">修改</a>
<a href="del?gid=${goods.gid}">删除</a>
</td>
</tr>
</c:forEach>
Here we mainly use tags in jsp to display data.
Then look at the code in the servlet
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
Goods goods=new Goods();
goods.setGname(request.getParameter("gname"));
goods.setPrice(Double.parseDouble(request.getParameter("price")));
goods.setMark(request.getParameter("mark"));
GoodsDao goodsDao=new GoodsDao();
int row=goodsDao.add(goods);
if (row>0){
request.getRequestDispatcher("ServletAllGoods").forward(request,response);
}else {
request.setAttribute("error_msg","添加信息失败");
request.getRequestDispatcher("error_jsp").forward(request,response);
}
}
}
Through the configuration of the Servlet, obtain the path to connect the Servlet and Tianjia to facilitate processing of browser requests.
public int add(Goods goods) {
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, psw);
String sql = "insert into t_goods(gname,price,mark)values(?,?,?)";
psmt = connection.prepareStatement(sql);
psmt.setObject(1, goods.getGname());
psmt.setObject(2, goods.getPrice());
psmt.setObject(3, goods.getMark());
row=psmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
//8.关闭资源
try {
if (psmt != null) {
psmt.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return row;
}
}
According to the obtained data, use string segmentation to get the name of each item that needs to be deleted, and finally call the delete method in a loop to delete!