使用Servlet完成商品信息的添加、修改、查询及删除功能。本程序使用的product表结构如下:‌‌ 编号 列名 数据类型 描述1 pid int 产品编号,自动增长2 name va

1.使用Servlet完成商品信息的添加、修改、查询及删除功能。本程序使用的product表结构如下:

 编号列名数据类型描述
1pidint产品编号,自动增长
2namevarchar(100)产品名称
3notetext产品简介
4pricedecimal(10,2)产品单价
5amountint 产品数量

‌ 2.数据库连接包,放在WEB-INF的lib目录下哦,官网下载。

2.1数据我放在test命名数据库,product是表名。

3.这时本人现大二,发表第二篇原创文章,有不足自处,可以指正,好的话点个赞,关注一下博,你的鼓励将是我创作的最大动力。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="ProductDeleteServlet" method="post" name="queryform">
	请输入需要删除的用户:<input type="text" name="name"/>
	<input type="submit" value="删除"/>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>新增产品</h1>
<form action="ProductInsertServlet" method="post" name="queryform">
	产品编号:<input type="text" name="pid"/><br/>
	产品名称:<input type="text" name="name"/><br/>
	产品简介:<textarea rows="10" cols="50" name="note"></textarea><br/>
	产品单价:<input type="text" name="price"/><br/>
	产品数量:<input type="text" name="amount"/><br/>
	<input type="submit" value="确定">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="ProductQueryServlet" method="post" name="queryform">
	请输入需要查询的产品名称:<input type="text" name="name"/>
	<input type="submit" value="查询"/>
</form>

</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>价格修改</h1>
<form action="PriceUpdateServlet" method="post" name="queryform">
	产品明:<input type="text" name="name"/><br/>
	价格:<input type="text" name="price"/><br/>
	
	<input type="submit" value="确定"/>
</form>
</body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

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

/**
 * Servlet implementation class PriceUpdateServlet
 */
@WebServlet("/PriceUpdateServlet")
public class PriceUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PriceUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		
		try {
			//第一步:装载驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
			
			//第二步:建立连接 
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC","root","123456");
		
			//第三步:构建查询对象
			String sql = "update Product set price = ? where name = ?";
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setString(1, price);
			pst.setString(2, name);
			
			int i = pst.executeUpdate();
			
			PrintWriter pw = response.getWriter();
			pw.write("价格修改成功");
			//第五步:关闭连接
			con.close();
		
		}catch(Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ProductDeleteServlet
 */
@WebServlet("/ProductDeleteServlet")
public class ProductDeleteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductDeleteServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String name = request.getParameter("name");
		
		try {
			//第一步:装载驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
			
			//第二步:建立连接 
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC","root","123456");
		
			//第三步:构建查询对象
			String sql = "delete from product where name = ?";
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setString(1, name);
			
			int i = pst.executeUpdate();
			
			PrintWriter pw = response.getWriter();
			pw.write("信息删除成功,请点击链接查看<a href='SimpleQueryServlet'>查看所有产品</a>");
			//第五步:关闭连接
			con.close();
		
		}catch(Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ProductInsertServlet
 */
@WebServlet("/ProductInsertServlet")
public class ProductInsertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductInsertServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String pid = request.getParameter("pid");
		String name = request.getParameter("name");
		String note = request.getParameter("note");
		String price = request.getParameter("price");
		String amount = request.getParameter("amount");
		
		try {
			//第一步:装载驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
			
			//第二步:建立连接 
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC","root","123456");
		
			//第三步:构建查询对象
			String sql = "insert into product(pid,name,note,price,amount) values(?,?,?,?,?)";
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setString(1, pid);
			pst.setString(2, name);
			pst.setString(3, note);
			pst.setString(4, price);
			pst.setString(5, amount);
			int i = pst.executeUpdate();
			
			PrintWriter pw = response.getWriter();
			pw.write("信息保存成功,请点击链接查看<a href='SimpleQueryServlet'>查看所有产品</a>");
			//第五步:关闭连接
			con.close();
		
		}catch(Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ProductQueryServlet
 */
@WebServlet("/ProductQueryServlet")
public class ProductQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductQueryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		
		String name = request.getParameter("name");
		try {
			//第一步:装载驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
			
			//第二步:建立连接 
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC","root","123456");
		
			//第三步:构建查询对象
			PreparedStatement pst = con.prepareStatement("select * from product where name=?");
			pst.setString(1, name);
			
			ResultSet rs = pst.executeQuery();
			
			//第四步:将结果集中的数据提取
			PrintWriter pw = response.getWriter();
			pw.write("<table>");
			pw.write("<tr>");
			pw.write("<th>产品编号</th>");
			pw.write("<th>产品名称</th>");
			pw.write("</tr>");
			while(rs.next()) {
				pw.write("<tr>");
				pw.write("<td>"+rs.getString("pid")+"</td>");
				pw.write("<td>"+rs.getString("name")+"</td>");
				pw.write("</tr>");
			}
			pw.write("</table>");
			//第五步:关闭连接
			con.close();
		
		}catch(Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

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

/**
 * Servlet implementation class SimpleQueryServlet
 */
@WebServlet("/SimpleQueryServlet")
public class SimpleQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SimpleQueryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		
		try {
			//第一步:装载驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
			
			//第二步:建立连接 
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC","root","123456");
		
			//第三步:执行sql语句 
			String sql = "select * from product";
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery(sql);
			
			//第四步:将结果集中的数据提取
			PrintWriter pw = response.getWriter();
			pw.write("<table>");
			pw.write("<tr>");
			pw.write("<th>产品编号</th>");
			pw.write("<th>产品名称</th>");
			
			pw.write("</tr>");
			while(rs.next()) {
				pw.write("<tr>");
				pw.write("<td>"+rs.getString("pid")+"</td>");
				pw.write("<td>"+rs.getString("name")+"</td>");
				pw.write("</tr>");
			}
			pw.write("</table>");
			//第五步:关闭连接
			con.close();
		}catch(Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值