There is a method getColumnCount in the JDBC API. Is there a similar method to find the number of rows in a result set?
Hi friends,
Create an instance of a JDBC driver or load JDBC drivers through jdbc.drivers Register a driver Specify a database Open a database connection Submit a query Receive results Process results
Thanks.
Hi friends, No, but it is easy to find the number of rows. If you are using a scrollable result set, rs, you can call the methods rs.last and then rs.getRow to find out how many rows rs has. If the result is not scrollable, you can either count the rows by iterating through the result set or get the number of rows by submitting a query with a COUNT column in the SELECT clause.
Hi friends, No, but it is esay to find the number of rows. If you are using a scrollable result set , you can call the method last() and then getRow() to find out how many rows in result set. It result set is not scrollable, you can either count the rows by iterating through the result set or get the number of rows by submitting a query with a COUNT column in the SELECT clause.
======Example=====
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
public class CountRows { public static void main(String[] args) { try { String mysqlDriver = "com.mysql.jdbc.Driver"; Class.forName(mysqlDriver).newInstance(); String connectionString = "jdbc:mysql://192.168.10.13:3306/studentinformation"; String username = "root"; String password = "root"; Connection con = null; Statement stmt = null; ResultSet rs = null; con = DriverManager.getConnection(connectionString, username, password); stmt = con.createStatement(ResultSet.TYPESCROLLSENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sqlString = "Select * from s_infor;"; rs = stmt.executeQuery(sqlString); System.out.println("---------Student Information----------"); while (rs.next()) { int roll = rs.getInt("s_Roll"); String Name = rs.getString("s_Name"); String Address = rs.getString("s_Address"); System.out.println(roll + " " + Name + " " + Address); } rs.last(); int numOfRows = rs.getRow(); System.out.println("Number of rows in result set : " + numOfRows); } catch (Exception e) { System.out.println(e.getMessage()); } }
}
======OutPut=====
---------Student Information---------- 1 Rohit Barabanki 3 Bharat Bareilly 4 Gyan Bareilly Number of rows in result set : 3
Thanks.
Hi friends,
Create an instance of a JDBC driver or load JDBC drivers through jdbc.drivers Register a driver Specify a database Open a database connection Submit a query Receive results Process results
Thanks.
Hi friends, No, but it is esay to find the number of rows. If you are using a scrollable result set , you can call the method last() and then getRow() to find out how many rows in result set. It result set is not scrollable, you can either count the rows by iterating through the result set or get the number of rows by submitting a query with a COUNT column in the SELECT clause.
======Example=====
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class CountRows { public static void main(String[] args) { try { String mysqlDriver = "com.mysql.jdbc.Driver"; Class.forName(mysqlDriver).newInstance(); String connectionString = "jdbc:mysql://192.168.10.13:3306/studentinformation"; String username = "root"; String password = "root"; Connection con = null; Statement stmt = null; ResultSet rs = null; con = DriverManager.getConnection(connectionString, username, password); stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); String sqlString = "Select * from s_infor;"; rs = stmt.executeQuery(sqlString); System.out.println("---------Student Information----------"); while (rs.next()) { int roll = rs.getInt("s_Roll"); String Name = rs.getString("s_Name"); String Address = rs.getString("s_Address"); System.out.println(roll + " " + Name + " " + Address); } rs.last(); int numOfRows = rs.getRow(); System.out.println("Number of rows in result set : " + numOfRows); } catch (Exception e) { System.out.println(e.getMessage()); } }
}
======OutPut===== ---------Student Information---------- 1 Rohit Barabanki 3 Bharat Bareilly 4 Gyan Bareilly Number of rows in result set : 3
Thanks.
Ads