I want to execute a select query and be able to get the count of the results, without using “COUNT(*)”, like TOAD does. The concept is to give the option to the user to input any ‘select query’ he wants and to count the number of the results for him.
try {
selectStatement = globalConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet selectResultSet = selectStatement.executeQuery(selectStatementString);
selectResultSet.last();
int countOfResults = selectResultSet.getRow();
writeInLabel(kllStatusLabel, "Count Finished: Found " + countOfResults + " Inserts");
} catch (SQLException ex) {
writeInLabel(kllStatusLabel, "Error executing select query.");
}
This code delays much in line: “selectResultSet.last();”.
TOAD does it with much less delay. (Right-Click -> Record Count).
Even when I scroll the results table all the way down in TOAD it does it faster.
How does TOAD get the record count?
OK I got it, I will use count(*) and have a second ResultSet to show my results.
ResultSet selectResultSet = selectStatement.executeQuery("select count(*) from (" + selectStatementString + ")");
int countOfResults = 0;
if (selectResultSet.next()){
countOfResults = selectResultSet.getInt(1);
}
TOAD is probably selecting the
@@rowcountor some other DB specific result metadata.If you want the size of the result, ask the DB for it explicitly (with
COUNT())If you’re using mysql then you can use
ROW_COUNT().