可滚动的结果集,向前滚动,绝对定位

结果集中的数据不仅可以向后滚动,还可以向前滚动,定位等等操作。

对于一些不支持分页技术的数据库,可是利用绝对定位等api实现分页,(mysql,sqlserver,oracle等都支持分页)

rs.next()是向后滚动;
rs.previous()向前滚动;
rs.absolute(5) 绝对定位。
还有其他的各种api

package cn.itcast.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ScrollTest {

    public static void main(String[] args) throws SQLException {
        Scroll();
    }

    static void Scroll() throws SQLException {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;

        try {
            conn = JdbcUtils.getConnection();
            st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); //设置成可滚动的结果集
            rs = st.executeQuery("select id,name,birthday,money from user where id<10");
            while(rs.next()) {
                System.out.println("id:" + rs.getObject("id") + "\tname:"
                        + rs.getObject("name") + "\tbirthday:"
                        + rs.getObject("birthday") + "\tmoney:"
                        + rs.getObject("money"));
            }
            System.out.println("-------------------");

            rs.absolute(5); //绝对定位,指定到第n行
            if(rs.previous())   //next()是向后,previous()向前
                System.out.println("id:" + rs.getObject("id") + "\tname:"
                        + rs.getObject("name") + "\tbirthday:"
                        + rs.getObject("birthday") + "\tmoney:"
                        + rs.getObject("money"));

        } finally {
            JdbcUtils.free(rs, st, conn);
        }
    }
}

你可能感兴趣的:(可滚动的结果集,向前滚动,绝对定位)