Number of rows and columns of result set in java

The number of rows and columns of the result set in java
Article classification: Java programming
In Java, there are several ways to obtain the total number of rows of the ResultSet.


The first: use the getRow method of the ResultSet to obtain the total number of rows in the ResultSet.

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rset = stmt.executeQuery("select * from yourTableName");
rset.last( );
int rowCount = rset.getRow(); //Get the total number of rows of the ResultSet The

second type: use the elements of the loop ResultSet to get the total number of rows of the

ResultSet ResultSet rset = stmt.executeQuery("select * from yourTableName");
int rowCount = 0;
while(rset.next()) {   rowCount++; } rowCount is the total number of rows in the ResultSet. The third type: use the count function in the sql statement to obtain the total number of rows in the ResultSet ResultSet rset = stmt.executeQuery("select count(*) totalCount from yourTableName");








int rowCount = 0;
if(rset.next()) {   rowCount=rset .getInt("totalCount "); } rowCount is the total number of rows in the ResultSet. The code for obtaining the total number of ResultSet columns by j ava is as follows: Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rset = stmt.executeQuery("select * from yourtable"); ResultSetMetaData rsmd = rset.getMetaData() ; int columnCount = rsmd.getColumnCount(); columnCount is the total number of columns in the ResultSet.














Guess you like

Origin blog.csdn.net/weixin_44182586/article/details/101317199