예제 #1
0
 public static void main(String[] args) throws Exception {
   CachedRowSetPage cp = new CachedRowSetPage();
   cp.initParam("mysql.ini");
   CachedRowSet rs = cp.query("select * from student_table", 3, 2); // ①
   // 向后滚动结果集
   while (rs.next()) {
     System.out.println(rs.getString(1) + "\t" + rs.getString(2) + "\t" + rs.getString(3));
   }
 }
예제 #2
0
 public CachedRowSet query(String sql, int pageSize, int page) throws Exception {
   // 加载驱动
   Class.forName(driver);
   try (
   // 获取数据库连接
   Connection conn = DriverManager.getConnection(url, user, pass);
       Statement stmt = conn.createStatement();
       ResultSet rs = stmt.executeQuery(sql)) {
     // 使用RowSetProvider创建RowSetFactory
     RowSetFactory factory = RowSetProvider.newFactory();
     // 创建默认的CachedRowSet实例
     CachedRowSet cachedRs = factory.createCachedRowSet();
     // 设置每页显示pageSize条记录
     cachedRs.setPageSize(pageSize);
     // 使用ResultSet装填RowSet,设置从第几条记录开始
     cachedRs.populate(rs, (page - 1) * pageSize + 1);
     return cachedRs;
   }
 }