关闭

第二十六章 可滚动结果集与分页技术

250人阅读 评论(0) 收藏 举报


在查询结果ResultSet中前滚或后滚来取数据

分页 mysql的语法limit 5,2  取第5条数据之后的两条



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 read(){
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			st = conn.createStatement();
			rs = st.executeQuery("select id,name,birthday,money from user");
			//下一条
			while(rs.next()){
				System.out.println(rs.getObject("id"));
				System.out.println(rs.getObject("name"));
				System.out.println(rs.getObject("birthday"));
				System.out.println(rs.getObject("money"));
			}
			//上一条
			System.out.println("------------");
			if(rs.previous()){
				System.out.println(rs.getObject("id"));
				System.out.println(rs.getObject("name"));
				System.out.println(rs.getObject("birthday"));
				System.out.println(rs.getObject("money"));
			}
			
			//分页 查出第6和第7
			System.out.println("------------");
			rs = st.executeQuery("select id,name,birthday,money from user limit 5,2");
			while(rs.next()){
				System.out.println(rs.getObject("id"));
				System.out.println(rs.getObject("name"));
				System.out.println(rs.getObject("birthday"));
				System.out.println(rs.getObject("money"));
			}
			
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtils.free(rs, st, conn);
		}
	}
	public static void main(String[] args){
		ScrollTest.read();
		
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    文章分类
    最新评论