zoukankan      html  css  js  c++  java
  • ResultSet获得总行数的方法

    【方法一】利用ResultSet的getRow方法来获得ResultSet的总行数

    Connection conn = null;
    		Statement sta = null;
    		ResultSet rs = null;
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123");
    			sta = conn.createStatement();
    			rs = sta.executeQuery("select * from test");
    			rs.last();
    			int row = rs.getRow();
    			System.out.println("行数为:"+row);
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}

    【方法二】利用循环ResultSet的元素来获得ResultSet的总行数
    Connection conn = null;
    		Statement sta = null;
    		ResultSet rs = null;
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123");
    			sta = conn.createStatement();
    			rs = sta.executeQuery("select * from test");
    			int row = 0;
    			while(rs.next()){
    				row++;
    			}
    			System.out.println("行数为:"+row);
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}

    【方法三】利用sql语句中的count函数获得ResultSet的总行数

    Connection conn = null;
    		Statement sta = null;
    		ResultSet rs = null;
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123");
    			sta = conn.createStatement();
    			rs = sta.executeQuery("select count(*) totleRows from test");
    			int row = 0;
    			while(rs.next()){
    				row = rs.getInt("totleRows");
    			}
    			System.out.println("行数为:"+row);
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}


  • 相关阅读:
    因子个数筛
    原根
    Pollard Rho (大数分解算法)
    Miller-Rabin(素数测试算法)
    离不开的微服务架构,脱不开的RPC细节(值得收藏)!!!
    微服务架构,多“微”才合适?
    互联网架构,究竟为啥要做服务化?
    markdown
    docker安装、启动(挂载外部配置和数据)
    程序员代码面试指南上(1-3)
  • 原文地址:https://www.cnblogs.com/itmyhome/p/4131591.html
Copyright © 2011-2022 走看看