Java Learning Paging Query

Source: Internet
Author: User

Java Learning Paging Queryan entity class that first makes a database Userinfo.java
<span style= "FONT-SIZE:14PX;" >package com.zhu.books.jdbc;import java.sql.connection;import Java.sql.sqlexception;import java.util.ArrayList; Import Java.util.list;public class Pagation extends DBConnection {public int gettablerscount () {int icount=0;//record line number, Then return to try {getconnection (); String sql= "SELECT count (ID) as icount from table1";p stmt=conn.preparestatement (SQL); Rs=pstmt.executequery (); if ( Rs.next ()) {Icount=rs.getint ("ICount");}} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}  return iCount; }/** * Query Information */public list<userinfo> getpagelistuserinfo (int pagenum,int pageSize) {list<userinfo> list=new ArrayList <Userinfo> (); try {getconnection (); int begin= (pageNum-1) *pagesize; String sql= "SELECT * FROM table1 limit" +begin+ "," +pagesize;pstmt=conn.preparestatement (SQL); Rs=pstmt.executequery () ; while (Rs.next ()) {Userinfo user=new Userinfo (); User.setid (Rs.getint ("id")); User.setuname (rs.getstring ("uname")); User.setrealname (rs.getstring ("RealnaMe ")); List.add (user);}} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return list;} </span><span style= "FONT-SIZE:18PX;" ><strong>}</strong></span>
Two: Establish database connectionDbconnection.java
Package Com.zhu.books.jdbc;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.statement;public class DBConnection {private Static final String CLS = "Com.mysql.jdbc.Driver";p rivate static final String URL = "Jdbc:mysql://localhost:3306/test";p R Ivate static final String USER = "root";p rivate static final String PWD = "122198";p ublic static Connection conn = Null;pu Blic static Statement stmt = null;public static PreparedStatement pstmt = Null;public static ResultSet rs = null;/** * Build number According to the library connection */public static void Getconnection () {try{class.forname (CLS); conn = Drivermanager.getconnection (URL, USER, PWD);} catch (Exception ex) {ex.printstacktrace ();}} /** * Close connection */public static void CloseAll () {try{if (rs!=null) {rs.close (); rs=null;} if (stmt!=null) {stmt.close (); stmt=null;} if (pstmt!=null) {pstmt.close ();p stmt=null;} if (conn!=null) {conn.close (); conn=null;}} catch (Exception ex) {ex.printstacktrace ();}}}
three-page of implementation
Pagation.java
Package Com.zhu.books.jdbc;import Java.sql.connection;import Java.sql.sqlexception;import java.util.ArrayList; Import Java.util.list;public class Pagation extends DBConnection {public int gettablerscount () {int icount=0;//record line number, Then return to try {getconnection (); String sql= "SELECT count (ID) as icount from table1";p stmt=conn.preparestatement (SQL); Rs=pstmt.executequery (); if ( Rs.next ()) {Icount=rs.getint ("ICount");}} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}  return iCount; }/** * Query Information */public list<userinfo> getpagelistuserinfo (int pagenum,int pageSize) {list<userinfo> list=new ArrayList <Userinfo> (); try {getconnection (); int begin= (pageNum-1) *pagesize; String sql= "SELECT * FROM table1 limit" +begin+ "," +pagesize;pstmt=conn.preparestatement (SQL); Rs=pstmt.executequery () ; while (Rs.next ()) {Userinfo user=new Userinfo (); User.setid (Rs.getint ("id")); User.setuname (rs.getstring ("uname")); User.setrealname (rs.getstring ("Realname")); List.add (user);}} catch (SqlexCeption e) {//TODO auto-generated catch Blocke.printstacktrace ();} return list;}}
Create JSP query page
paging.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%><% @page import= " Com.zhu.books.jdbc.Userinfo "%><% @page import=" com.zhu.books.jdbc.Pagation "%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >page Show

Java Learning Paging Query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.