天天看點

利用servlet+jsp+javabean進行分頁查詢分頁項目JDBCjavaBean的CompanyinfoDao層pageBeanservlet的page運作截圖eclipse常用的快捷鍵

利用servlet+jsp+javabean進行分頁查詢

  • 分頁項目
    • 檔案結構
    • 檔案說明
    • mysql的limit查詢
    • 源代碼下載下傳位址
  • JDBC
  • javaBean的Companyinfo
  • Dao層
  • pageBean
  • servlet的page
  • 運作截圖
  • eclipse常用的快捷鍵

分頁項目

檔案結構

利用servlet+jsp+javabean進行分頁查詢分頁項目JDBCjavaBean的CompanyinfoDao層pageBeanservlet的page運作截圖eclipse常用的快捷鍵

檔案說明

Companyinfo:是一個JavaBean類,需要顯示的内容
pageBean:描述分頁資訊的内容
CompanyinfoDao:Companyinfo的Dao層用來資料庫通路的一個接口
DB:連接配接資料庫,擷取資料庫連接配接對象
page:servlet與前台進行交換,在運作該項目的時候在浏覽器的的位址欄通路
http://localhost:8080/page/page
show.jsp:用來顯示從資料庫擷取的記錄,并可以進行上下頁進行檢視
lib:導入該項目所需要的jar
           

mysql的limit查詢

SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset  

說明:
LIMIT 子句可以被用于強制 SELECT 語句傳回指定的記錄數。LIMIT 接受一個
或兩個數字參數。參數必須是一個整數常量。如果給定兩個參數,第一個參數指
定第一個傳回記錄行的偏移量,第二個參數指定傳回記錄行的最大數目。初始記
錄行的偏移量是 0(而不是 1): 為了與 PostgreSQL 相容,MySQL 也支援句
法: LIMIT # OFFSET #。
           

源代碼下載下傳位址

利用servlet+jsp+javabean進行分頁查詢

JDBC

package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
	private final static String driver="com.mysql.jdbc.Driver";
	private final static String url = "jdbc:mysql://
    127.0.0.1:3306/job?useSSL=false";
	private final static String user = "root";
	private final static String password  ="";
	public static Connection getConn() {
		Connection connection = null;
		try {
			Class.forName(driver);
			connection=DriverManager.getConnection(url, user, 
            password);
		} catch (ClassNotFoundException e) {
			System.out.println("資料庫驅動加載失敗");
			e.printStackTrace();
		}catch (SQLException e) {
			System.out.println("資料庫連接配接失敗");
			e.printStackTrace();
		}
		return connection;
	}
	
	public static void close (Connection connection ,Statement 
    statement , ResultSet resultSet) {
		try {
			if(connection!=null)connection.close();
			if(statement!=null)statement.close();
			if(resultSet!=null)resultSet.close();
		} catch (SQLException e) {
			System.out.println("資料庫關閉異常");
			e.printStackTrace();
		}
	}
	
	//測試資料庫連接配接是否成功
	public static void main(String []args) {
		Connection connection=null;
		connection = getConn();
		System.out.println(connection);
		close(connection, null, null);
		
	}
}
           

javaBean的Companyinfo

package Bean;

public class Companyinfo {
	private String c_name;
	private String c_email;
	private String c_serve;

	@Override
	public String toString() {
		return "Companyinfo [c_name=" + c_name + ", c_email=" + c_email + ", c_serve=" + c_serve + "]";
	}

	public Companyinfo(String c_name, String c_email, String c_serve) {
		super();
		this.c_name = c_name;
		this.c_email = c_email;
		this.c_serve = c_serve;
	}

	public Companyinfo() {
		super();
	}

	public String getC_name() {
		return c_name;
	}

	public void setC_name(String c_name) {
		this.c_name = c_name;
	}

	public String getC_email() {
		return c_email;
	}

	public void setC_email(String c_email) {
		this.c_email = c_email;
	}

	public String getC_serve() {
		return c_serve;
	}

	public void setC_serve(String c_serve) {
		this.c_serve = c_serve;
	}

}
           

Dao層

package dao;

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

import Bean.Companyinfo;
import jdbc.DB;

public class CompanyinfoDao {
	
	public int getCount(String sql) {
		int count=0;
		Connection connection = DB.getConn();
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			statement=connection.createStatement();
			resultSet=statement.executeQuery(sql);
			while(resultSet.next()) {
				count=resultSet.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DB.close(connection, statement, resultSet);
		}
		return count;
	}
	
	public List<Companyinfo> getList(String sql){
		List<Companyinfo> list = new ArrayList<>();
		Connection connection = DB.getConn();
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			statement=connection.createStatement();
			resultSet=statement.executeQuery(sql);
			while(resultSet.next()) {
				Companyinfo companyinfo = new Companyinfo(resultSet.getString(1),resultSet.getString(2),resultSet.getString(3));
				list.add(companyinfo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DB.close(connection, statement, resultSet);
		}
		return list;
	}
	
}
           

pageBean

package Bean;
public class pageBean {
	private int everyPage; // 每頁顯示記錄數
	private int totalCount; // 總記錄數
	private int totalPage; // 總頁數
    private int currentPage; //目前頁
    private int beginIndex;			//查詢起始點
	public pageBean(int everyPage, int totalCount ,int currentPage) {
		super();
		this.everyPage = everyPage;
		this.totalCount = totalCount;
		this.currentPage = currentPage;
		if(totalCount != 0 &&totalCount % everyPage == 0) {
			totalPage = totalCount / everyPage;
		} else {
			totalPage = totalCount / everyPage + 1;
		}
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		if(currentPage<1)currentPage=1;
		this.currentPage = currentPage;
		
	}
	public int getEveryPage() {
		return everyPage;
	}
	public void setEveryPage(int everyPage) {
		this.everyPage = everyPage;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getBeginIndex() {
		setBeginIndex();
		return beginIndex;
	}
	public void setBeginIndex() {
		this.beginIndex =(currentPage - 1) * everyPage;
	}

}
           

servlet的page

package servlet;
import java.io.IOException;
import java.util.List;
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 javax.servlet.http.HttpSession;
import Bean.Companyinfo;
import Bean.pageBean;
import dao.CompanyinfoDao;
@WebServlet("/page")
public class page extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static CompanyinfoDao companyinfoDao =  new CompanyinfoDao();
	String sql;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		pageBean pBean = (pageBean)session.getAttribute("page");
		if(pBean==null) {
			sql = "select count(*) from companyinfo ";
			int count =companyinfoDao.getCount(sql);
			pBean  = new pageBean(3, count, 1);
		}else {
			String cString = request.getParameter("currentPage");
			if(cString==null)cString="1";
			int currentPage = Integer.parseInt(cString);
			pBean.setCurrentPage(currentPage);
		}
		String sql = "select c_name,c_email, c_serve from companyinfo limit "+pBean.getBeginIndex()+","+pBean.getEveryPage();
		List<Companyinfo> list = companyinfoDao.getList(sql);
		session.setAttribute("page", pBean);
		request.setAttribute("list", list);
		request.getRequestDispatcher("show.jsp").forward(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
           

#前台 show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="Bean.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<style type="text/css">
table {
	border-collapse: collapse;
	border:2px solid black;
}

th,td {
    padding: .5em .75em;
}

th {
	border:1px solid grey;
}

td {
   	border:1px dotted grey;
}
</style>
 <body>
        <table>
            <caption>彩排安排</caption>
            <thead>
                <!-- 表格頭部 -->
                <tr>
                    <th scope="rowgroup">公司名稱</th>
                    <th scope="col">公司email</th>
                    <th scope="col">公司服務</th>
                </tr>
            </thead>
            <tbody>
                <!-- 表格主體 -->
               <c:forEach items="${requestScope.list}" var="companyinfo">
               	<tr>
               		<td>${companyinfo.c_name}</td>
               		<td>${companyinfo.c_email}</td>
               		<td>${companyinfo.c_serve}</td>
               </tr>
               </c:forEach>
            </tbody>
        </table>
        <a href="page?currentPage=1">首頁</a>&nbsp;&nbsp;
         <c:if test="${sessionScope.page.currentPage !=1}">
        	 <a href="page?currentPage=${sessionScope.page.currentPage-1}">上一頁</a>&nbsp;&nbsp;
         </c:if>
         <c:if test="${sessionScope.page.currentPage !=sessionScope.page.totalPage}">
        	 <a href="page?currentPage=${sessionScope.page.currentPage+1}">下一頁</a>&nbsp;&nbsp;
         </c:if>       
          <a href="page?currentPage=${sessionScope.page.totalPage}">尾頁</a>
    </body>
</html>
           

運作截圖

利用servlet+jsp+javabean進行分頁查詢分頁項目JDBCjavaBean的CompanyinfoDao層pageBeanservlet的page運作截圖eclipse常用的快捷鍵
利用servlet+jsp+javabean進行分頁查詢分頁項目JDBCjavaBean的CompanyinfoDao層pageBeanservlet的page運作截圖eclipse常用的快捷鍵

eclipse常用的快捷鍵

描述 快捷鍵
生成set和get方法 Alt+shift+s r
生成toString方法 Alt+shift+s s
生成object方法 Alt+shift+s o
格式化代碼 Alt+shift+s f
儲存所有代碼 ctrl+shift+s
自動補全代碼或者提示代碼 alt+?或alt+/

注意:Alt+shift+s r 是指先按下組合按下Alt,shift,s 後按下r即可