Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Rowcount of a query

  Asked By: Erica    Date: Jun 10    Category: Java    Views: 825
  

I am trying to write a java program that gives the number of rows
fetched by a query. It does not return any row. See below.

import java.util.*;
import java.io.*;
import java.lang.*;
import java.sql.*;

public class test
{
public static void main(String[] args)
{
try
{
int vCnt = 5;
String vConnect = "sun.jdbc.odbc.JdbcOdbcDriver";
String vURL = "jdbc:odbc:archerarjun";
String vQuery = " Select * from wp_carriers ";
boolean vTmp;

Class.forName(vConnect);
java.sql.Connection vConnObj = DriverManager.getConnection(vURL);

Statement vRetStmtObj =
vConnObj.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE );
ResultSet rs = vRetStmtObj.executeQuery(vQuery);

while (rs.next())
{
System.out.println(rs.getString(1)+" , "+rs.getString(2));
}

vTmp = rs.last();
vCnt = rs.getRow();

System.out.print("Total "+vCnt+" rows fetched.");
vTmp = rs.absolute(1);
}
catch(ClassNotFoundException cnfe)
{
System.err.println(cnfe);
}
catch(SQLException sqle)
{
System.err.println(sqle);
}
}
}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Vonda Ramirez     Answered On: Jun 10

Just use the rs.size() method. It will return  the number  of rows
present. Else use the following logic
int rowCount= 0;
while(rs.next())
{
//do whatever you want with the data
rowCount++;
}
System.out.println("Number of rows  = " + rowCount);

 
Didn't find what you were looking for? Find more on Rowcount of a query Or get search suggestion and latest updates.




Tagged: