【easyui】之DataGrid数据显示

【easyui】之DataGrid数据显示,第1张

🗨上期文章中我使用了easyui中的树结构来实现了前端的菜单和子菜单展示,大家感兴趣的可以去看一下哦♥♥♥♥♥

【easyui】之Tree后端工作_小阿飞_的博客-CSDN博

📕本期文章:继续使用前端框架easyui实现表格数据的分页展示 

😜话不多说,先看效果图:

这是展示5行数据的表格👇

这是展示10行数据的表格👇

这是展示20行数据的表格👇

 还可以进行模糊查询👇

🐒🐒使用easyui时你会发现,虽然排出来的前端界面色彩不太好看,但是代码是比较好实现的,接下来让我们看一看实现这些功能的代码🐒🐒

🐒 Oracle数据库代码

CREATE TABLE BS_BOOK
(
    ID    NUMBER PRIMARY KEY,
    NAME  VARCHAR(50) NOT NULL,
    PRICE NUMBER      NOT NULL
);

INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (20, '双q会给你答案', 62.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (21, '凡人修仙传', 90.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (22, '吞噬星空', 52.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (26, '重生之贼行天下', 84.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (27, '择天记', 6.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (28, '我的美女总裁老婆', 77.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (29, '爵迹·风津道', 70.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (30, '神墓', 88.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (31, '寻花问柳', 89.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (32, '仙逆', 86.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (33, '天珠变', 49.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (34, '重生之官道', 45.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (35, '春莺啭', 44.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (36, '永生', 26.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (37, '覆雨翻云', 59.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (38, '完美世界', 5.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (39, '活色生香', 60.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (40, '武动乾坤', 0.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (41, '眸倾天下', 63.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (42, '临界·爵迹2', 34.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (43, '恐怖高校', 37.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (44, '大罗金仙异界销魂', 92.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (45, '我把爱情煲成汤', 24.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (46, '罂粟的情人', 36.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (47, '魔兽剑圣异界纵横', 63.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (48, '香初上舞', 78.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (49, '莽荒纪', 5.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (50, '重生1998', 69.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (51, '晚明', 42.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (52, '空战极限', 56.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (53, '风临异世', 83.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (54, '傲世九重天', 20.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (55, '造化之门', 6.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (56, '武神', 26.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (57, '婚宠之枭妻霸爱(陈紊涛)', 78.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (58, '斗破苍穹', 96.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (59, '主神崛起', 11.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (60, '异世邪君', 71.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (61, '富贵锦绣', 61.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (62, '绝品医仙', 68.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (63, '杀手之王', 27.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (64, '极乐篮球风暴', 42.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (65, '极度修真', 58.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (66, '慧眼问道', 42.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (67, '混沌劫', 71.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (68, '混沌修真诀', 47.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (69, '华夏立国传', 15.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (70, '鸿蒙玄天曲', 9.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (71, '洪荒之金口玉言', 80.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (72, '横行在异世', 19.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (73, '鬼王的毒妃', 15.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (74, '和亲娇妃', 46.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (75, '宠物小精灵之小天', 41.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (76, '总裁的俏保镖', 54.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (77, '毒仙', 78.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (78, '钻石女人极品男', 96.00);
INSERT INTO BS_BOOK (ID, NAME, PRICE)
VALUES (79, '亡命奴妃', 11.00);

commit

🐒 数据库连接类代码

package util;

import java.sql.*;

public class DBHelper {

    //定义链接字符串
    private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";

    //加载驱动
    static {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //获得链接
    public static Connection getCon() {
        try {
            return DriverManager.getConnection(URL, "scott", "123");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //关闭资源
    public static void close(Connection con, PreparedStatement ps, ResultSet rs) {
        try {
            if (con != null && !con.isClosed()) {
                con.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

🐒 首先是界面代码👇

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


    Title
    
    
    



    
    

🐒 实体类代码👇

package pojo;

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

		public Book(Integer id, String name, Double price) {
			super();
			this.id = id;
			this.name = name;
			this.price = price;
		}

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

		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;
		}

		public Double getPrice() {
			return price;
		}

		public void setPrice(Double price) {
			this.price = price;
		}
	    
}

🐒 BookDao.java代码👇

package dao;

import pojo.Book;
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 BookDao {

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

    public List list(int page, int rows, String name) {
        int begin = (page - 1) * rows + 1;
        int end = page * rows;
        List list = new ArrayList();
        try {
            con = DBHelper.getCon();
            ps = con.prepareStatement("select * from\n" +
                    "              (select b.*,ROWNUM myr from BS_BOOK b where name like ?)\n" +
                    "a where myr between ? and ?");
            ps.setString(1, "%" + name + "%");
            ps.setInt(2, begin);
            ps.setInt(3, end);
            rs = ps.executeQuery();
            while (rs.next()) {
                Book b = new Book(rs.getInt(1), rs.getString(2), rs.getDouble(3));
                list.add(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBHelper.close(con, ps, rs);
        }
        return list;
    }

    public Integer total(String name) {
        try {
            con = DBHelper.getCon();
            ps = con.prepareStatement("select count(1) from BS_BOOK b where name like ?");
            ps.setString(1, "%" + name + "%");
            rs = ps.executeQuery();
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBHelper.close(con, ps, rs);
        }
        return 0;
    }

}

🐒 BookServlet.java代码👇

package servlet;

import dao.BookDao;
import pojo.Book;
import pojo.PageBean;
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 com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;

@WebServlet("/book.do")
public class BookServlet 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 {
        String name = "";
        String p3 = req.getParameter("name");
        if (p3 != null) {
            name = p3;
        }
        PageBean pb=new PageBean();
        pb.setReq(req);
        resp.setCharacterEncoding("gb2312");
        pb.setTotal(bookDao.total(name));
        pb.setRows(bookDao.list(pb.getPage(), pb.getRow(), name));
        mapper.writeValue(resp.getOutputStream(), pb);
    }

}

🐒 实体类二:PageBean.java代码(bean在java中可以理解为对象,这里的PageBean中的属性是专门给前端使用的)👇

package pojo;

import java.util.List;
import javax.servlet.http.HttpServletRequest;

public class PageBean {

		private Integer total;
	    private List rows;
	    private Integer page = 1;
	    private Integer row = 10;

	    //拿到请求中的分页数据
	    public void setReq(HttpServletRequest req){
	        String p1 = req.getParameter("page");
	        if (p1 != null) {
	            page = Integer.parseInt(p1);
	        }
	        String p2 = req.getParameter("rows");
	        if (p2 != null) {
	            row = Integer.parseInt(p2);
	        }
	    }

		public Integer getTotal() {
			return total;
		}
		public void setTotal(Integer total) {
			this.total = total;
		}
		public List getRows() {
			return rows;
		}
		public void setRows(List rows) {
			this.rows = rows;
		}
		public Integer getPage() {
			return page;
		}
		public void setPage(Integer page) {
			this.page = page;
		}
		public Integer getRow() {
			return row;
		}
		public void setRow(Integer row) {
			this.row = row;
		}
}

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存