admin管理员组

文章数量:1033075

JavaWeb后端入门10—增删改查

其实就是SQL+Servlet的应用,这里以查询为例,增删改直接放文件

采用原始的三层架构+MVC模式

查询功能就是利用select * from 表 where 列名 =?

查询功能

1、web层

1.1 jsp文件

代码语言:javascript代码运行次数:0运行复制
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib uri="; prefix="c" %>
<HTML>
<HEAD>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="${pageContext.request.contextPath}/css/Style1.css"
	rel="stylesheet" type="text/css" />
<script language="javascript"
	src="${pageContext.request.contextPath}/js/public.js"></script>
<script type="text/javascript">
	function addProduct() {
		window.location.href = "${pageContext.request.contextPath}/ShowSelectCategoryServlet";
	}
	function delProduct(pid,pname){
		let idDel = confirm("删除后无法恢复,只能手动重新添加!是否确认删除"+pname+"?");
		if (idDel) {
			//要删除
			window.location.href = "${pageContext.request.contextPath}/DelProductServlet?pid="+pid;
		}
	}
</script>
</HEAD>
<body>
	<br>
	<form id="Form1" name="Form1"
		action="${pageContext.request.contextPath}/user/list.jsp"
		method="post">
		<table cellSpacing="1" cellPadding="0" width="100%" align="center"
			bgColor="#f5fafe" border="0">
			<TBODY>
				<tr>
					<td class="ta_01" align="center" bgColor="#afd1f3"><strong>商品列表</strong>
					</TD>
				</tr>
				<tr>
					<td class="ta_01" align="right">
						<button type="button" id="add" name="add" value="添加" class="button_add" onclick="addProduct()">添加</button>

					</td>
				</tr>
				<tr>
					<td class="ta_01" align="center" bgColor="#f5fafe">
						<table cellspacing="0" cellpadding="1" rules="all"
							bordercolor="gray" border="1" id="DataGrid1"
							style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; BORDER-LEFT: gray 1px solid; WIDTH: 100%; WORD-BREAK: break-all; BORDER-BOTTOM: gray 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #f5fafe; WORD-WRAP: break-word">
							<tr
								style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; HEIGHT: 25px; BACKGROUND-COLOR: #afd1f3">

								<td align="center" width="18%">序号</td>
								<td align="center" width="17%">商品图片</td>
								<td align="center" width="17%">商品名称</td>
								<td align="center" width="17%">商品价格</td>
								<td align="center" width="17%">是否热门</td>
								<td width="7%" align="center">编辑</td>
								<td width="7%" align="center">删除</td>
							</tr>
							<c:forEach items="${productList }" var="product" varStatus="vs">
								<tr onmouseover="this.style.backgroundColor='white'" onmouseout="this.style.backgroundColor='#F5FAFE';">
									<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="18%">${vs.count }</td>
									<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%">
										<img width="40" height="45" src="${pageContext.request.contextPath }/${product.pimage }">
									</td>
									<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%">${product.pname }</td>
									<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%">${product.shop_price }</td>
									<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%">
										${product.is_hot==1?"是":"否" }
									</td>
									<%--编辑按钮 --%>
									<td align="center" style="HEIGHT: 22px">
										<a href="${pageContext.request.contextPath}/EditProductUIServlet?pid=${product.pid}">
											<img src="${pageContext.request.contextPath}/images/i_edit.gif" border="0" style="CURSOR: hand">
										</a>
									</td>
									<%--删除按钮 --%>
									<td align="center" style="HEIGHT: 22px">
										<a href="javascript:;" onclick="delProduct('${product.pid}','${product.pname}')">
											<img src="${pageContext.request.contextPath}/images/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand">
										</a>
									</td>
								</tr>
							</c:forEach>
						</table>
					</td>
				</tr>

			</TBODY>
		</table>
	</form>
</body>
</HTML>

1.2 实体domain层(JavaBean)

代码语言:javascript代码运行次数:0运行复制
package cn.wuter.domain;

public class Product {
	
	private Integer pid;
	private String pname;
	private double market_price;
	private double shop_price;
	private String pimage;
	private String pdate;
	private int is_hot;
	private String pdesc;
	private int pflag;
	private String cid;
	public Integer getPid() {
		return pid;
	}
	public void setPid(Integer pid) {
		this.pid = pid;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public double getMarket_price() {
		return market_price;
	}
	public void setMarket_price(double market_price) {
		this.market_price = market_price;
	}
	public double getShop_price() {
		return shop_price;
	}
	public void setShop_price(double shop_price) {
		this.shop_price = shop_price;
	}
	public String getPimage() {
		return pimage;
	}
	public void setPimage(String pimage) {
		this.pimage = pimage;
	}
	public String getPdate() {
		return pdate;
	}
	public void setPdate(String pdate) {
		this.pdate = pdate;
	}
	public int getIs_hot() {
		return is_hot;
	}
	public void setIs_hot(int is_hot) {
		this.is_hot = is_hot;
	}
	public String getPdesc() {
		return pdesc;
	}
	public void setPdesc(String pdesc) {
		this.pdesc = pdesc;
	}
	public int getPflag() {
		return pflag;
	}
	public void setPflag(int pflag) {
		this.pflag = pflag;
	}
	public String getCid() {
		return cid;
	}
	public void setCid(String cid) {
		this.cid = cid;
	}
	
	
	
}

1.3 控制Servlet层

代码语言:javascript代码运行次数:0运行复制
package cn.wuter.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.wuter.domain.Product;
import cn.wuter.service.AdminProductService;

/**
 * 后台页面显示商品信息的Servlet
 */
public class AdminProductListServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.传递请求给Service,查询所有商品,返回List<Product>类型
		AdminProductService service = new AdminProductService();
		List<Product> productList = null;
		try {
			productList = service.findAllProduct();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//2.保存信息
		request.setAttribute("productList", productList);
		
		//3.转发请求
		request.getRequestDispatcher("admin/product/list.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

2 Service层

代码语言:javascript代码运行次数:0运行复制
package cn.wuter.service;

import java.sql.SQLException;
import java.util.List;

import cn.wuter.dao.AdminProductListDao;
import cn.wuter.domain.Product;

public class AdminProductService {

	public List<Product> findAllProduct() throws SQLException {
		//传递查询所有商品的请求给Dao
		AdminProductListDao dao = new AdminProductListDao();
		List<Product> productList = dao.findAllProductList();
		return productList;
	}

}

3 Dao层

代码语言:javascript代码运行次数:0运行复制
package cn.wuter.dao;

import java.sql.SQLException;
import java.util.List;

import javax.management.Query;

import org.apachemons.dbutils.QueryRunner;
import org.apachemons.dbutils.handlers.BeanListHandler;

import cn.wuter.domain.Product;
import cn.wuter.utils.JDBCUtils;

public class AdminProductListDao {

	public List<Product> findAllProductList() throws SQLException {
		//查询后台所有商品
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from product";
		List<Product> productList = queryRunner.query(sql, new BeanListHandler<Product>(Product.class));
		return productList;
	}

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2021-04-28 ,如有侵权请联系 cloudcommunity@tencent 删除入门centerimportpublic后端

JavaWeb后端入门10—增删改查

其实就是SQL+Servlet的应用,这里以查询为例,增删改直接放文件

采用原始的三层架构+MVC模式

查询功能就是利用select * from 表 where 列名 =?

查询功能

1、web层

1.1 jsp文件

代码语言:javascript代码运行次数:0运行复制
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib uri="; prefix="c" %>
<HTML>
<HEAD>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="${pageContext.request.contextPath}/css/Style1.css"
	rel="stylesheet" type="text/css" />
<script language="javascript"
	src="${pageContext.request.contextPath}/js/public.js"></script>
<script type="text/javascript">
	function addProduct() {
		window.location.href = "${pageContext.request.contextPath}/ShowSelectCategoryServlet";
	}
	function delProduct(pid,pname){
		let idDel = confirm("删除后无法恢复,只能手动重新添加!是否确认删除"+pname+"?");
		if (idDel) {
			//要删除
			window.location.href = "${pageContext.request.contextPath}/DelProductServlet?pid="+pid;
		}
	}
</script>
</HEAD>
<body>
	<br>
	<form id="Form1" name="Form1"
		action="${pageContext.request.contextPath}/user/list.jsp"
		method="post">
		<table cellSpacing="1" cellPadding="0" width="100%" align="center"
			bgColor="#f5fafe" border="0">
			<TBODY>
				<tr>
					<td class="ta_01" align="center" bgColor="#afd1f3"><strong>商品列表</strong>
					</TD>
				</tr>
				<tr>
					<td class="ta_01" align="right">
						<button type="button" id="add" name="add" value="添加" class="button_add" onclick="addProduct()">添加</button>

					</td>
				</tr>
				<tr>
					<td class="ta_01" align="center" bgColor="#f5fafe">
						<table cellspacing="0" cellpadding="1" rules="all"
							bordercolor="gray" border="1" id="DataGrid1"
							style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; BORDER-LEFT: gray 1px solid; WIDTH: 100%; WORD-BREAK: break-all; BORDER-BOTTOM: gray 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #f5fafe; WORD-WRAP: break-word">
							<tr
								style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; HEIGHT: 25px; BACKGROUND-COLOR: #afd1f3">

								<td align="center" width="18%">序号</td>
								<td align="center" width="17%">商品图片</td>
								<td align="center" width="17%">商品名称</td>
								<td align="center" width="17%">商品价格</td>
								<td align="center" width="17%">是否热门</td>
								<td width="7%" align="center">编辑</td>
								<td width="7%" align="center">删除</td>
							</tr>
							<c:forEach items="${productList }" var="product" varStatus="vs">
								<tr onmouseover="this.style.backgroundColor='white'" onmouseout="this.style.backgroundColor='#F5FAFE';">
									<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="18%">${vs.count }</td>
									<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%">
										<img width="40" height="45" src="${pageContext.request.contextPath }/${product.pimage }">
									</td>
									<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%">${product.pname }</td>
									<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%">${product.shop_price }</td>
									<td style="CURSOR: hand; HEIGHT: 22px" align="center" width="17%">
										${product.is_hot==1?"是":"否" }
									</td>
									<%--编辑按钮 --%>
									<td align="center" style="HEIGHT: 22px">
										<a href="${pageContext.request.contextPath}/EditProductUIServlet?pid=${product.pid}">
											<img src="${pageContext.request.contextPath}/images/i_edit.gif" border="0" style="CURSOR: hand">
										</a>
									</td>
									<%--删除按钮 --%>
									<td align="center" style="HEIGHT: 22px">
										<a href="javascript:;" onclick="delProduct('${product.pid}','${product.pname}')">
											<img src="${pageContext.request.contextPath}/images/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand">
										</a>
									</td>
								</tr>
							</c:forEach>
						</table>
					</td>
				</tr>

			</TBODY>
		</table>
	</form>
</body>
</HTML>

1.2 实体domain层(JavaBean)

代码语言:javascript代码运行次数:0运行复制
package cn.wuter.domain;

public class Product {
	
	private Integer pid;
	private String pname;
	private double market_price;
	private double shop_price;
	private String pimage;
	private String pdate;
	private int is_hot;
	private String pdesc;
	private int pflag;
	private String cid;
	public Integer getPid() {
		return pid;
	}
	public void setPid(Integer pid) {
		this.pid = pid;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public double getMarket_price() {
		return market_price;
	}
	public void setMarket_price(double market_price) {
		this.market_price = market_price;
	}
	public double getShop_price() {
		return shop_price;
	}
	public void setShop_price(double shop_price) {
		this.shop_price = shop_price;
	}
	public String getPimage() {
		return pimage;
	}
	public void setPimage(String pimage) {
		this.pimage = pimage;
	}
	public String getPdate() {
		return pdate;
	}
	public void setPdate(String pdate) {
		this.pdate = pdate;
	}
	public int getIs_hot() {
		return is_hot;
	}
	public void setIs_hot(int is_hot) {
		this.is_hot = is_hot;
	}
	public String getPdesc() {
		return pdesc;
	}
	public void setPdesc(String pdesc) {
		this.pdesc = pdesc;
	}
	public int getPflag() {
		return pflag;
	}
	public void setPflag(int pflag) {
		this.pflag = pflag;
	}
	public String getCid() {
		return cid;
	}
	public void setCid(String cid) {
		this.cid = cid;
	}
	
	
	
}

1.3 控制Servlet层

代码语言:javascript代码运行次数:0运行复制
package cn.wuter.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.wuter.domain.Product;
import cn.wuter.service.AdminProductService;

/**
 * 后台页面显示商品信息的Servlet
 */
public class AdminProductListServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.传递请求给Service,查询所有商品,返回List<Product>类型
		AdminProductService service = new AdminProductService();
		List<Product> productList = null;
		try {
			productList = service.findAllProduct();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//2.保存信息
		request.setAttribute("productList", productList);
		
		//3.转发请求
		request.getRequestDispatcher("admin/product/list.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

2 Service层

代码语言:javascript代码运行次数:0运行复制
package cn.wuter.service;

import java.sql.SQLException;
import java.util.List;

import cn.wuter.dao.AdminProductListDao;
import cn.wuter.domain.Product;

public class AdminProductService {

	public List<Product> findAllProduct() throws SQLException {
		//传递查询所有商品的请求给Dao
		AdminProductListDao dao = new AdminProductListDao();
		List<Product> productList = dao.findAllProductList();
		return productList;
	}

}

3 Dao层

代码语言:javascript代码运行次数:0运行复制
package cn.wuter.dao;

import java.sql.SQLException;
import java.util.List;

import javax.management.Query;

import org.apachemons.dbutils.QueryRunner;
import org.apachemons.dbutils.handlers.BeanListHandler;

import cn.wuter.domain.Product;
import cn.wuter.utils.JDBCUtils;

public class AdminProductListDao {

	public List<Product> findAllProductList() throws SQLException {
		//查询后台所有商品
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from product";
		List<Product> productList = queryRunner.query(sql, new BeanListHandler<Product>(Product.class));
		return productList;
	}

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2021-04-28 ,如有侵权请联系 cloudcommunity@tencent 删除入门centerimportpublic后端

本文标签: JavaWeb后端入门10增删改查