一下 技术 来自我跟我的朋友 大神25号 超牛的 大神
下面创建三个工具类 一个实体类 来调用他 。。也可以自己创建数据库 通过查询 可以的
package org.lange.page.base;
import java.util.ArrayList;import java.util.List;import org.apache.catalina.security.SecurityUtil;
public class BasePageSO {
/** * 当前页码 */ private int pageNumber = 1; /** * 每页记录数 page size */ private int objectsPerPage = 50; public BasePageSO() { } public int getPageNumber() { return pageNumber; } public void setPageNumber(int pageNumber) { if(pageNumber <= 0){ pageNumber = 1; } this.pageNumber = pageNumber; } public int getObjectsPerPage() { return objectsPerPage; } public void setObjectsPerPage(int objectsPerPage) { this.objectsPerPage = objectsPerPage; } public Integer getOffset(){ return (getPageNumber() - 1) * getObjectsPerPage(); } public Integer getLimit(){ return getObjectsPerPage();
}
}以上 是 penbean 的 类
package org.lange.page.entity;
import org.lange.page.base.BasePageSO;
public class Dome1 extends BasePageSO{
private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } private int age;}
以上的是 dome1 类
package org.lange.page.javabean;
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import javax.management.RuntimeErrorException;
import org.lange.page.entity.Dome1;
public class Dome2
{
private String url ="jdbc:mysql://localhost:3306/day15"; private String user="root"; private String password="root";private int pageNumber;
private int numPerPage; private String name; private Long age;public Dome2(int pageNumber, int numPerPage, String name, Long age) {
super(); this.pageNumber = pageNumber; this.numPerPage = numPerPage; this.name = name; this.age = age; }public void queryByPage()
{ //驱动注册 程序 java.sql.Statement stmt = null; Connection conn = null; try { //1:驱动注册程序 Class.forName("com.mysql.jdbc.Driver"); //获取链接对象 conn=DriverManager.getConnection(url, user, password); //3:创建一个statement stmt= conn.createStatement(); //4:创建sql语句// String sql ="CREATE TABLE along(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20),gender VARCHAR(2))"; //5:发送sql语句 得到sql语句 返回结果 返回int值// int count = stmt.executeUpdate(sql); //6:输出 sql 语句// System.out.println("结果"+count+"行"); //7:不能让他一直 链接呀 那服务器内存不崩溃了么:所以要关闭它i Dome1 d = new Dome1(); d.setPageNumber(pageNumber); d.setObjectsPerPage(numPerPage); int offset = d.getOffset(); int limit = d.getLimit(); String sql = "select * from chaxun where 1=1"; if(name !=null && name.length() != 0) sql += " and name like '%" + name + "%'"; if(age != null) sql += "and age = " + age; sql += " limit " + offset + "," + limit; String sqlCount = "select count(*) from chaxun where 1 = 1"; if(name !=null && name.length() != 0) sqlCount += " and name like '%" + name + "%'"; if(age != null) sqlCount += "and age = " + age; System.out.println(sql); ResultSet rs = stmt.executeQuery(sql);// while (rs.next()) { System.out .println(rs.getString(1) + "\t" + rs.getString(2));// }} catch (Error e) { e.printStackTrace(); throw new RuntimeErrorException(e); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally{ if(stmt!=null) try { stmt.close(); } catch (Error e) { e.printStackTrace(); throw new RuntimeErrorException(e); } catch (SQLException e) { e.printStackTrace(); } if(conn !=null) try { conn.close(); } catch (Error e) { e.printStackTrace(); throw new RuntimeErrorException(e); } catch (SQLException e) { e.printStackTrace(); } } }
以上的dome2 类 常用的工具类 用来 连 数据库的
package org.lange.page.test;
import org.junit.Test;import org.lange.page.javabean.Dome2; public class TestPage {@Test
public void pageTest() { Dome2 d = new Dome2(0,0,"李",null); d.queryByPage(); }以上 是 实体类 调用上面三个方法的
大哥 说jsp 有点难搞 就没搞 回头不上 但是 这个 直接 复制 粘贴 用就行了