可滚动结果集

来源:互联网 发布:js给数组增加元素 编辑:程序博客网 时间:2024/05/16 08:43
     网络文摘"
     滚动特性:
  next(),此方法是使游标向下一条记录移动。
  previous() ,此方法可以使游标上一条记录移动,前提前面还有记录。
  absolute(int row),可以使用此方法跳到指定的记录位置。定位成功返回true,不成功返回false,返回值为false,则游标不会移动。
  afterLast() ,游标跳到最后一条记录之后。
  beforeFirst() ,游标跳到第一条记录之前。(跳到游标初始位)
  first(),游标指向第一条记录。
  last(),游标指向最后一条记录。
  relative(int rows) ,相对定位方法,参数值可正可负,参数为正,游标从当前位置向下移动指定值,参数为负,游标从当前位置向上移动指定值。
  TYPE_FORWARD_ONLY ,该常量指示指针只能向前移动的 ResultSet 对象的类型。 
  TYPE_SCROLL_INSENSITIVE ,该常量指示可滚动但通常不受其他的更改影响的 ResultSet 对象的类型。 
  TYPE_SCROLL_SENSITIVE ,该常量指示可滚动并且通常受其他的更改影响的 ResultSet 对象的类型。

应用:
  1. public ConcurrentLinkedQueue getStockInfoQueue(String table, String stockCode ){   //从数据库中取出股票信息,保存到队列,并返回队列
  2.         ConcurrentLinkedQueue<RTmm> clq = new ConcurrentLinkedQueue<RTmm>();
  3.         String sql = "select * from "+table +" where s_code = '" +stockCode+"'";
  4.         String sql2 = "select count(*) from "+table +" where s_code = '" +stockCode+"'";
  5.         try {
  6.             Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);  //可滚动结果集
  7.             ResultSet rs2 = stmt.executeQuery(sql2);
  8.             rs2.last();
  9.             System.out.println("记录数"+rs2.getInt(1));
  10.             int records = rs2.getInt(1);   //得到记录个数
  11.             ResultSet rs = stmt.executeQuery(sql);
  12.             if(records < 60){
  13.                 while(rs.next()){
  14.                     String code = rs.getString(1);
  15.                     String time = rs.getString(3);
  16.                     double open = rs.getDouble(4);
  17.                     double close = rs.getDouble(5);
  18.                     double low = rs.getDouble(6);
  19.                     double high = rs.getDouble(7);
  20.                     // RTmm(String code, String dt, double low, double open, double close, double high) 
  21. //                  System.out.println(code+time+open+"  "+close+"  "+high);
  22.                     RTmm rtmm = new  RTmm(code.trim(), time.trim(), low,  open, close, high);
  23.                     clq.add(rtmm);
  24.                 }
  25.             }else{
  26.                 rs.absolute(records - 60);
  27.                 while(rs.next()){
  28.                     String code = rs.getString(1);
  29.                     String time = rs.getString(3);
  30.                     double open = rs.getDouble(4);
  31.                     double close = rs.getDouble(5);
  32.                     double low = rs.getDouble(6);
  33.                     double high = rs.getDouble(7);
  34.                     // RTmm(String code, String dt, double low, double open, double close, double high) 
  35. //                  System.out.println(code+time+open+"  "+close+"  "+high);
  36.                     RTmm rtmm = new  RTmm(code.trim(), time.trim(), low,  open, close, high);
  37.                     clq.add(rtmm);
  38.                 }
  39.             }
  40.         } catch (SQLException  e) {
  41.             e.printStackTrace();
  42.         }
  43.         return clq;
  44.     }

原创粉丝点击