【easyui】之DataGrid数据增删改

【easyui】之DataGrid数据增删改,第1张

上期文章小阿飞带大家使用easyui制作了表格数据的分页展示和模糊查询

回顾:【easyui】之DataGrid数据显示_小阿飞_的博客-CSDN博客

本期文章就是在上期文章的基础上对表格中的数据进行了增加、删除、修改功能

先看效果图:

点击《凡人修仙传》的删除按钮后就会把这一行数据从页面和数据库中删除

已经没有《凡人修仙传》了:

 

点击增加按钮后填上你要增加的内容点击保存就可以了

点击修改按钮后:

这是修改先的行数据内容

这是要修改的内容 

点击保存后就修改成功了:

 这是本期文章所使用的jar包,比之前的jar包多了几个哦

实现代码:

前端界面代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title
    
    
    



    
    
    

增加
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title
	
    
    





实体类代码

package pojo;

public class Topic {
	private Integer id;
    private String name;
    
    public Topic() {
		// TODO Auto-generated constructor stub
	}

	public Topic(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	@Override
	public String toString() {
		return "Topic [id=" + id + ", name=" + name + "]";
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
    
}

dao层代码

package dao;

import pojo.Topic;
import util.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class TopicDao {

    private Connection con;
    private PreparedStatement ps;
    private ResultSet rs;

    public List list() {
        List list = new ArrayList();
        try {
            con = DBHelper.getCon();
            ps = con.prepareStatement("select * from bs_topic");
            rs = ps.executeQuery();
            while (rs.next()) {
                Topic topic = new Topic(rs.getInt(1), rs.getString(2));
                list.add(topic);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBHelper.close(con, ps, rs);
        }
        return list;
    }

}

处理层代码

package servlet;

import dao.BookDao;
import pojo.Book;
import pojo.PageBean;
import util.ResponseUtil;
import org.apache.commons.beanutils.BeanUtils;

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;
import java.lang.reflect.InvocationTargetException;

@SuppressWarnings("all")
@WebServlet("/book/del.do")
public class DelServlet extends HttpServlet {

    private BookDao bookDao = new BookDao();

    @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 {
        //使用填充帮助类帮我们直接封装对象的属性
        Book book=new Book();
        try {
            BeanUtils.populate(book,req.getParameterMap());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用dao做删除
        int i=bookDao.delete(book);
        //返回给前台结果
        if(i>0){
            ResponseUtil.send(resp,"yes");
        }else{
            ResponseUtil.send(resp,"no");
        }
    }

}
package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.BookDao;
import pojo.Book;
import util.ResponseUtil;
import org.apache.commons.beanutils.BeanUtils;

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("/book/edit.do")
public class EditServlet extends HttpServlet {

    private BookDao bookDao = new BookDao();
    private ObjectMapper mapper = new ObjectMapper();

    @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 {
        //使用填充帮助类帮我们直接封装对象的属性
        Book book=new Book();
        try {
            BeanUtils.populate(book,req.getParameterMap());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用dao做删除
        int i=bookDao.edit(book);
        //返回给前台结果
        if(i>0){
            ResponseUtil.send(resp,"yes");
        }else{
            ResponseUtil.send(resp,"no");
        }
    }

}
package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.BookDao;
import pojo.Book;
import util.ResponseUtil;
import org.apache.commons.beanutils.BeanUtils;

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;

@SuppressWarnings("all")
@WebServlet("/book/insert.do")
public class InsertServlet extends HttpServlet {

    private BookDao bookDao = new BookDao();
    private ObjectMapper mapper = new ObjectMapper();

    @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 {
        //使用填充帮助类帮我们直接封装对象的属性
        Book book=new Book();
        try {
            BeanUtils.populate(book,req.getParameterMap());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //调用dao做删除
        int i=bookDao.insert(book);
        //返回给前台结果
        if(i>0){
            ResponseUtil.send(resp,"yes");
        }else{
            ResponseUtil.send(resp,"no");
        }
    }

}
package servlet;

import dao.TopicDao;
import util.ResponseUtil;

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("/topic/list.do")
public class TopicServlet extends HttpServlet {

    private TopicDao topicDao = new TopicDao();

    @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 {
        ResponseUtil.send(resp, topicDao.list());
    }

}

util包下的代码

package util;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ResponseUtil {

    private static ObjectMapper mapper = new ObjectMapper();

    public static void send(HttpServletResponse resp,Object data){
        try {
            mapper.writeValue(resp.getOutputStream(),data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/940170.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-17
下一篇 2022-05-17

发表评论

登录后才能评论

评论列表(0条)

保存