我是靠谱客的博主 迷路乐曲,最近开发中收集的这篇文章主要介绍Eclipse+Maven项目+分页显示MySQL中的数据,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

先看效果:


本人数据库:数据库名bookclass, 列名有:BookClassID,BookClassMC。请大家修改对应的数据库




下面开始教程:(项目名:Paging)

第一步:

1.在pom.xml中加入依赖:

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.22</version>
    </dependency>

2.写一个实体类entity 封装定义的要查询的字段:

package com.sve.dao;

public class entity {
	public static final int PAGE_SIZE = 5;//每页显示的条数 
	
	private int bookid;
	private String boookname;
	
	public int getBookid() {
		return bookid;
	}
	public void setBookid(int bookid) {
		this.bookid = bookid;
	}
	public String getBoookname() {
		return boookname;
	}
	public void setBoookname(String boookname) {
		this.boookname = boookname;
	}
}

第二步:

写一个dao类 PagDao:

package com.sve.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class PagDao {
	public static void main(String [] args) throws SQLException {
		List<entity> listentitys = new PagDao().find(1);
	}
	
	public Connection getConnection() throws SQLException {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306";
			String username = "root";
			String password = "******";
			conn = DriverManager.getConnection(url,username,password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	public List<entity> find(int page) throws SQLException{
		List<entity> list = new ArrayList<entity>();
		Connection conn = getConnection();
		String sql = "select * from library.bookclass order by BookClassID asc limit ?,?";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, (page-1)*entity.PAGE_SIZE);
			ps.setInt(2, entity.PAGE_SIZE);
			ResultSet rs = ps.executeQuery();
			while(rs.next()) {
				entity e = new entity();
				e.setBookid(rs.getInt("BookClassID"));
				e.setBoookname(rs.getString("BookClassMC"));
				list.add(e);
			}
			rs.close();
			ps.close();
			conn.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return list;
	}
	
	public int findCount() throws SQLException {
		int count = 0;
		Connection conn = getConnection();
		String sql = "select count(*) from library.bookclass";
		Statement sm = (Statement)conn.createStatement();
		ResultSet rs = sm.executeQuery(sql);
		if(rs.next()) {
			count = rs.getInt(1);
		}
		rs.close();
		conn.close();
		
		return count;
	}
	
}

第三步:

写一个servlet类:

public class PagServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public PagServlet() {
    	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
		//当前页
		int currPage = 1;
		//判断页面是否有效
		if(request.getParameter("page") != null) {
			currPage = Integer.parseInt(request.getParameter("page"));
		}
		PagDao dao = new PagDao();

		try {
			//查询所有书籍
			List<entity> list = dao.find(currPage);
			request.setAttribute("list", list);
			//总页数
			int pages;
			//总记录数
			int count = dao.findCount();
			//总页数
			if(count % entity.PAGE_SIZE == 0) {
				pages = count / entity.PAGE_SIZE;
			}else {
				pages = count / entity.PAGE_SIZE+1;
			}
			
			StringBuffer sb = new StringBuffer();
			//
			for(int i = 1;i <= pages;i++) {
				//是否当前页
				if(i==currPage) {
					//构建分页
					sb.append("【"+i+"】");
				}else {
					sb.append("<a href='PagServlet?page="+i+"'>"+i+"</a>");
				}
				sb.append(" ");
			}
			request.setAttribute("bar", sb.toString());
			request.getRequestDispatcher("entity_list.jsp").forward(request, response);
		} catch (SQLException e) {
		// TODO Auto-generated catch block
			e.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);
	}


第四步:


新建一个index.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>
	<a href="PagServlet">查看所有书籍</a>
</body>
</html>

第五步:

1.新建一个entity_list.jsp 显示数据页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.List,com.sve.dao.*" %>
<!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>所有书籍</title>
<style type="text/css">  
    td{font-size: 12px;}  
    h2{margin: 0px}  
</style>  
</head>
<body>
	<table align="center" width="300" border="1" height="180" bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">  
    <tr bgcolor="white">  
        <td align="center" colspan="5">  
            <h2>所有商品信息</h2>  
        </td>  
    </tr> 
    <tr align="center" bgcolor="#e1ffc1" >  
        <td><b>ID</b></td>  
        <td><b>书籍名称</b></td>  
    </tr>  
    <% 
    	List<entity> list = (List<entity>)request.getAttribute("list");
    	for(entity e : list){
    %>
    <tr align="center" bgcolor="white">  
        <td><%=e.getBookid()%></td>  
        <td><%=e.getBoookname()%></td>  
    </tr>  
    <%} %>
    <tr>  
        <td align="center" colspan="2" bgcolor="white">  
            <%=request.getAttribute("bar")%>  
        </td>  
    </tr>  
</table>  
</body>
</html>

2.web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
  	<servlet-name>PagServlet</servlet-name>
  	<display-name>PagServlet</display-name>
  	<description></description>
  	<servlet-class>com.sve.servlet.PagServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>PagServlet</servlet-name>
  	<url-pattern>/PagServlet</url-pattern>
  </servlet-mapping>
</web-app>



最后:http://localhost:8080/Paging/index.jsp


最后

以上就是迷路乐曲为你收集整理的Eclipse+Maven项目+分页显示MySQL中的数据的全部内容,希望文章能够帮你解决Eclipse+Maven项目+分页显示MySQL中的数据所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部