JAVAWeb添加,查询,删除,修改

发布于:2022-12-13 ⋅ 阅读:(519) ⋅ 点赞:(0)

Customer实体类

目录

Customer实体类

CustomerDaoImpl DAO实现类

CustomerServlet 客户注册类

DeletionServlet 客户删除类

QueryServlet 客户查询类

 XiugaiServlet 客户修改类

xg.jsp



​​​​​​​​​​​​​​

CustomerDaoImpl DAO实现类

import java.awt.image.RasterFormatException;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.nanyang.database.C3pOPoolUtils;
import com.nanyang.entity.Customer;

public class CustomerDaoImpl implements CustomerDao{
	//获取数据库连接
	QueryRunner runner =new	QueryRunner(C3pOPoolUtils.getDataSource());
	
	@Override
	public boolean save(Customer c) {
		try {
			 //sql语句
			String sql = "insert into root values(null,?,?,?)";
			//为占位符设置参数
		int i =	runner.update(sql,c.getName(),c.getAccount(),c.getPassword());
		//判断是否添加成功
			return i >0 ? true : false;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RasterFormatException(e.getMessage());
		}
		
	}

	public List<Customer> QueryCustomer() {
		try {
			//sql语句
			String sql ="select * from root";
			return 	runner.query(sql, new BeanListHandler<Customer>(Customer.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RasterFormatException(e.getMessage());
		}
		
	}

	public boolean DeleteCustomer(String id) {	
		
		try {
			//sql语句
			String sql = "delete from root where id=?";
			int update = runner.update(sql , id);
			//判断是否删除成功
			String msg =update > 0 ? "删除成功" : "删除失败";
			System.out.println(msg);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RasterFormatException(e.getMessage());
		}
		return false;
		
	}

	public Integer XiuGaiCustomer(int id,Customer customer) {
		String sql= "update root set name=?,account=?,password=? where id=? ";
		// TODO Auto-generated method stub
		Integer up;
		try {
			up = runner.update(sql, customer.getName(),customer.getAccount(),customer.getPassword(),id);
			return up;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 5;
		}
		
	}

CustomerServlet 客户注册类

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.nanyang.entity.Customer;
import com.nanyang.service.CustomerServiceImpl;

public class CustomerServlet 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.setContentType("text/html;charset=utf-8");
		req.setCharacterEncoding("utf-8");
		// 获取客服注册信息
		String name = req.getParameter("name");
		String account = req.getParameter("account");
		String password = req.getParameter("password");
		//将获取的数据封装到一个客户对象在
		Customer customer = new Customer(name,account,password);
	CustomerServiceImpl customerServiceImpl = new	CustomerServiceImpl();
	boolean addCustomer = customerServiceImpl.addCustomer(customer);
	//响应客户端
	PrintWriter writer = resp.getWriter();
		if (addCustomer) {
			writer.write("注册成功 <br> <a href='query'>查询客户信息</a>");
		} else {
			writer.write("注册失败 <br><a href='register.html'>重新注册</a>");
		}
	
	}
}

DeletionServlet 客户删除类

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.nanyang.service.CustomerServiceImpl;
import com.nanyang.service.CustomerServlet;

public class DeletionServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		// 处理乱码问题
				req.setCharacterEncoding("utf-8");
				// 设置响应的字符集编码
				resp.setContentType("text/html;charset=utf-8");
				req.setCharacterEncoding("utf-8");
				//获取客户的编号【删除】
		String id = req.getParameter("id");
		//调用service层根据id删除客户信息
		CustomerServlet customerServlet = new CustomerServiceImpl(); 
		boolean deletion = customerServlet.DeleteCustomerByid(id);
		//响应客户端
		PrintWriter writer = resp.getWriter();
		if (deletion) {
			writer.write("删除失败");
		} else {
			writer.write("删除成功<br> <a href='query'>查询客户信息</a>");
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}
}

QueryServlet 客户查询类

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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

import com.nanyang.entity.Customer;
import com.nanyang.service.CustomerServiceImpl;

public class QueryServlet extends HttpServlet{
	
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException, IOException {
	// 处理乱码问题
	req.setCharacterEncoding("utf-8");
	// 设置响应的字符集编码
	resp.setContentType("text/html;charset=utf-8");
	req.setCharacterEncoding("utf-8");
	//查询service业务层实现查询所有客户数据
	CustomerServiceImpl customerServiceImpl = new CustomerServiceImpl();
	List<Customer> list = customerServiceImpl.QueryAllCustomer();
	//把集合数据显示到页面上
	PrintWriter writer = resp.getWriter();
	//表格
    String data ="<table align='center' width='80%'  border='1' cellspacing='0px' cellpadding='0px'>"
    		+ "<tr><th>编号</th><th>姓名</th><th>账号</th><th>密码</th><th colspan='2'>操作</th></tr>";
    //遍历表的内容
    for (int i = 0; i < list.size(); i++) {
		Customer c = list.get(i);
		data+="<tr><td>"+c.getId()+"</td><td>"+c.getName()+"</td>"
				+ "<td>"+c.getAccount()+"</td><td>"+c.getPassword()+"</td>"
				+"<td><a href='deletion?id="+c.getId()+"'>"+"删除"+"</a></td><td><a href='xiu?id="+c.getId()+"'>修改</a></td></tr>";
	}
    data+="</table>";
	writer.write(data);
	
}
@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
	doGet(req, resp);
	}
}

 XiugaiServlet 客户修改类

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nanyang.entity.Customer;
import com.nanyang.service.CustomerServiceImpl;
import com.nanyang.service.CustomerServlet;

public class XiugaiServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);	
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws 
	ServletException, IOException {
		// 处理乱码问题
				req.setCharacterEncoding("utf-8");
				// 设置响应的字符集编码
				resp.setContentType("text/html;charset=utf-8");
				req.setCharacterEncoding("utf-8");
				// 获取客服注册信息
				String id = req.getParameter("id");
				String name = req.getParameter("name");
				String account = req.getParameter("account");
				String password = req.getParameter("password");
				Customer cu=new Customer(name,account,password);
				//调用service层根据id修改客户信息
				 CustomerServlet customer = new CustomerServiceImpl();
				 customer.XiuGaiCustomerByid(Integer.parseInt(id),cu);
				 req.getRequestDispatcher("xg.jsp").forward(req, resp);
	}
}

xg.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body> 
<form action="xiu" method="post">
编号:<input type="text" name="id" placeholder="请输入编号"><br>
 姓名:<input type="text" name="name" placeholder="请输入用户名"><br>
 账号:<input type="text" name="account" placeholder="请输入账号"><br>
密码:<input type="password" name="password" placeholder="请输入密码"><br>
    <input type="submit" onclick="xiu()" value="确认修改">
  </form>
  <a href='query'>查询客户信息</a>
</body>
<script>
function xiu(){
	alert("修改成功");
}
</script>
</html>

注册

注册成功

 

 删除编号49

查看一下 49被删除了

修改编号44

修改成功 

 数据库

 

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到