網路文摘" 滾動特性:
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 對象的類型。
應用:
- public ConcurrentLinkedQueue getStockInfoQueue(String table, String stockCode ){ //從資料庫中取出股票資訊,儲存到隊列,並返回隊列
- ConcurrentLinkedQueue<RTmm> clq = new ConcurrentLinkedQueue<RTmm>();
- String sql = "select * from "+table +" where s_code = '" +stockCode+"'";
- String sql2 = "select count(*) from "+table +" where s_code = '" +stockCode+"'";
- try {
- Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); //可滾動結果集
- ResultSet rs2 = stmt.executeQuery(sql2);
- rs2.last();
- System.out.println("記錄數"+rs2.getInt(1));
- int records = rs2.getInt(1); //得到記錄個數
- ResultSet rs = stmt.executeQuery(sql);
- if(records < 60){
- while(rs.next()){
- String code = rs.getString(1);
- String time = rs.getString(3);
- double open = rs.getDouble(4);
- double close = rs.getDouble(5);
- double low = rs.getDouble(6);
- double high = rs.getDouble(7);
- // RTmm(String code, String dt, double low, double open, double close, double high)
- // System.out.println(code+time+open+" "+close+" "+high);
- RTmm rtmm = new RTmm(code.trim(), time.trim(), low, open, close, high);
- clq.add(rtmm);
- }
- }else{
- rs.absolute(records - 60);
- while(rs.next()){
- String code = rs.getString(1);
- String time = rs.getString(3);
- double open = rs.getDouble(4);
- double close = rs.getDouble(5);
- double low = rs.getDouble(6);
- double high = rs.getDouble(7);
- // RTmm(String code, String dt, double low, double open, double close, double high)
- // System.out.println(code+time+open+" "+close+" "+high);
- RTmm rtmm = new RTmm(code.trim(), time.trim(), low, open, close, high);
- clq.add(rtmm);
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return clq;
- }