Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2244354
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: Java

2011-09-13 20:45:03

  1. package cn.itcast.jdbc;

  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;

  6. public class ScrollTest {

  7.     /**
  8.      * @param args
  9.      * @throws SQLException
  10.      */
  11.     public static void main(String[] args) throws SQLException {
  12.         scroll();
  13.     }

  14.     static void scroll() throws SQLException {
  15.         Connection conn = null;
  16.         Statement stmt = null;
  17.         ResultSet rs = null;
  18.         try {
  19.             conn = JdbcUtils.getConnection();
  20.             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
  21.                     ResultSet.CONCUR_READ_ONLY);
  22.             rs = stmt
  23.                     .executeQuery("select id, name, money, birthday from user limit 1, 10");
  24.             while (rs.next()) {
  25.                 System.out.println(rs.getObject("id") + "\t"
  26.                         + rs.getObject("name") + "\t"
  27.                         + rs.getObject("birthday") + "\t"
  28.                         + rs.getObject("money"));
  29.             }

  30.             System.out.println("------------");
  31.             rs.absolute(11);
  32.             int i = 0;
  33.             while (rs.previous() && i < 10) {
  34.                 i++;
  35.                 System.out.println(rs.getObject("id") + "\t"
  36.                         + rs.getObject("name") + "\t"
  37.                         + rs.getObject("birthday") + "\t"
  38.                         + rs.getObject("money"));
  39.             }

  40.             // if (rs.previous())
  41.             // System.out.println(rs.getObject("id") + "\t"
  42.             // + rs.getObject("name") + "\t"
  43.             // + rs.getObject("birthday") + "\t"
  44.             // + rs.getObject("money"));

  45.         } finally {
  46.             JdbcUtils.free(rs, stmt, conn);
  47.         }
  48.     }
  49. }
运行结果:
1003 dao name2 2011-08-16 200.0
1004 daoname1 2011-08-16 200.0
1005 daoname1 2011-08-16 200.0
1006 daoname1 2011-08-16 200.0
1007 daoname1 2011-08-16 200.0
1008 daoname1 2011-08-16 8800.1
1009 daoname1 2011-08-16 200.0
1010 daoname1 2011-08-16 200.0
1012 daoname1 2011-08-16 200.0
1015 name4 2011-09-11 200.0
------------
1015 name4 2011-09-11 200.0
1012 daoname1 2011-08-16 200.0
1010 daoname1 2011-08-16 200.0
1009 daoname1 2011-08-16 200.0
1008 daoname1 2011-08-16 8800.1
1007 daoname1 2011-08-16 200.0
1006 daoname1 2011-08-16 200.0
1005 daoname1 2011-08-16 200.0
1004 daoname1 2011-08-16 200.0
1003 dao name2 2011-08-16 200.0
 
阅读(725) | 评论(0) | 转发(0) |
0

上一篇:limit的用法

下一篇:Java EE学习路线图

给主人留下些什么吧!~~