【方法一】利用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();
		}


相关文章:

  • 2022-02-17
  • 2022-12-23
  • 2021-07-06
  • 2022-12-23
猜你喜欢
  • 2022-01-30
  • 2022-01-19
  • 2021-07-21
  • 2021-12-29
  • 2021-07-13
  • 2022-02-23
相关资源
相似解决方案