Get number of rows in ResultSet object
Can anybody guide me on how to get the total number of rows in a result set object without iterating through all rows? Thanks.
Hi Soon,
You mention that you want to get the row count without iterating through the "ResultSet". I don't know what database you are using, but I want to inform you that with my Oracle 8i (8.1.7.4) database, using JDK 1.3.1 and the matching JDBC (thin) driver, the implementation of the "last()" method (that others have recommended that you use), actually iterates through the whole "ResultSet".
What I do is first execute a "SELECT COUNT(1)" query (using a literal "1") to get the number of rows -- this is much faster than executing the "last()" method.
Then I execute my actual query.
Hope this helps you.
Good Luck,
Avi.
Similar Messages
-
How to get number of rows return in SELECT query
i'm very new in java, i have a question:
- How to get number of rows return in SELECT query?
(i use SQL Server 2000 Driver for JDBC and everything are done, i only want to know problems above)
Thanks.make the result set scroll insensitve, do rs.last(), get the row num, and call rs.beforeFirst(), then you can process the result set like you currently do.
String sql = "select * from testing";
PreparedStatement ps =
con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = ps.executeQuery();
rs.last();
System.out.println("Row count = " + rs.getRow());
rs.beforeFirst();~Tim
NOTE: Ugly, but does the trick. -
How to get number of rows and columns in a two dimensional array ?
Hello,
What would be the simplest way to get number of rows and columns in a two dimensional array represented as integers ?
I'm looking for another solution as For...Each loop in case of large arrays.
Regards,
PetriHi Petri,
See a attached txt file for obtaining two arrays with upper and lower index values
Regards
Ray
Regards
Ray Farmer
Attachments:
Get2DArrayIndex.txt 2 KB -
Getting number of rows in a resultset
I am moving my application from JDBC 1.0 to JDBC 2.0. In JDBC 1.0, I used to fire separate query for count(*) to get the no. of rows in results but with JDBC 2.0 is there way that I dont need to execute a separte query to get no. or rows in a resultset.
Thanks
SushilThere is Sushil if your driver supports it. It can be done with scrollable ResultSets.
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("sql");
if (rs != null) {
rs.last();
int numRows = rs.getRow();
// be sure to set rs.beforeFirst() if you want to process the ResultSet
} -
Hi!
Can someone tell me how to obtain the number of rows returned by a query?
I tried to use getFetchSize() but it seems to always return 0 for my query.
THANKS in advance!
ResultSet testRS = Stmt.executeQuery(testStmt);
int size = testRS.getFetchSize();
System.out.println("size = " + size);
Wo LayTo get the number of rows returned, you have to loop
through the result set, or do a "select count" (which
is more efficient since it doesn't have to pass the
rows across the network) ...Looping around the entire result set is not very performance efficient. Then agin if you use count (*) you actually execute an extra query.
The best thing you can do is:// assume res is your result set.
res.last();
int rows = res.getRow();This way you only have to execute one query and you don't really need to loop around the entire result set. Of course you will have to have a scrollable result set, if this is supported by your DB.
Hope that helped.
afotoglidis -
Result Set - How to retrieve the number of rows in resultset
harlo....everyone.
i m new in this language and would like to ask that how can i store the result into an array or u all hav others ideal to do it......? Hopefully can get answer asap....thanksssss.
Below is the source code that i did. Please comment it
ResultSet rs = stmt.executeQuery("Select CCourse_Code from RegisteredCourse, Student where Student.NStud_ID ='123'and Student.CStud_Pwd='123' and Student.NStud_ID = RegisteredCourse.NStud_ID ");
if(rs!=null){
// can i get the the number of records in resultset, rs to define my array size
String course[] = new String[2];
while(rs.next()){
for(int i =0; i<course.length; i++)
course[i] = rs.getString("ccourse_code");
return course;Or...
ResultSet rs = stmt.executeQuery("Select count(CCourse_Code) from RegisteredCourse, Student where Student.NStud_ID ='123'and Student.CStud_Pwd='123' and Student.NStud_ID = RegisteredCourse.NStud_ID ");
This should return a table with one row and one column. This will hold the number originally requested.
However I would still go with the ArrayList suggestion given above. -
Large number of rows in ResultSet
Hai,
I have huge number of rows retrive from database using ResultSet but it seems to be slow to get records. When it retrive data from database can we read data from ResultSet?.
Advance wishes,
DaieshNo, he can't, but who's to say that's what's slowing
him down? Could be bad JDBC code, old JDBC drivers,
or bad queries. Could be.
Honestly why does it matter to you? It matters a great deal.
If he's talking about a web page, and LOTS of people who ask this question are bringing that data down to the browser, my answer would be that no user wants to deal with 500K records all at once. A well-placed WHERE clause or filter, or paging through 25 at a time a la Google, would be my recommendation in that case.
Did you have an
answer all prepared to go so long as he gave you a
reason that was up to your rigorous standards? Yes, see above.
You
could have said "make sure you really need all that
data, and if you do, then try X, Y, and Z to speed it
up". But to just throw that out with no evidence
you're prepared to help regardless? Why bother?See above. There was a reason for the question.
Oh wait... I get it ... did I mistake your submission
to "The Most Useless, Unhelpful, and Arrogant Post
Contest" for a real post? My bad...Nope, that would be yours, dumb @ss.
% -
How to get number of rows in tableview
Hello,
Could anybody help me on how to get the number of rows in a tableview? Sort of using DESCRIBE TABLE in classic ABAP.
Thanks,
RickyThere is an attribute of the class CL_HTMLB_TABLEVIEW named
ROWCOUNT.
Inside an event u need to get this info use this code:
Replace 'Monitor' for the ID of your tableview
DATA: tv TYPE REF TO CL_HTMLB_TABLEVIEW,
r_count type i,
tv ?= CL_HTMLB_MANAGER=>GET_DATA(
request = runtime->server->request
name = 'tableView'
id = 'Monitor' ).
IF tv IS NOT INITIAL.
DATA: tv_data TYPE REF TO CL_HTMLB_EVENT_TABLEVIEW.
tv_data = tv->data.
r_count = tv_data->rowcount.
ENDIF. -
Resukt:Get number of rows from 3 different table...?
Hi All,
I need to find out number of rows from 3 different table for the same conditions. Instead of writing 3 queries, is it possible to get it thru one query?
For example, i need to find out number of rows in tables where name = 'Ameet' from 3 different table, i will end writing 3 queries.
1. select count(1) from table_a where name = 'Ameet';
1. select count(1) from table_b where name = 'Ameet';
1. select count(1) from table_c where name = 'Ameet';
Is it possible to write a single query to get result of all above 3 queries?
result:
table_a table_b table_c
34 44 2One way:
SELECT (SELECT COUNT(*) AS CNT FROM TABLE_A WHERE name='Ameet') AS TABLE_A
, (SELECT COUNT(*) AS CNT FROM TABLE_B WHERE name='Ameet') AS TABLE_B
, (SELECT COUNT(*) AS CNT FROM TABLE_C WHERE name='Ameet') AS TABLE_C
FROM DUALIf you want the total:
SELECT SUM(CNT)
FROM
SELECT COUNT(*) AS CNT FROM TABLE_A WHERE name='Ameet'
UNION ALL
SELECT COUNT(*) FROM TABLE_B WHERE name='Ameet'
UNION ALL
SELECT COUNT(*) FROM TABLE_C WHERE name='Ameet'
) -
Is there a simple way with ADDT to get the NUMBER of rows in a table that contain a particular ID or other value? Would QueryBuilder be the place to do this, or is there a snippet of code?
For example, to find out how many students have registered for a class with an ID of "100", I would need to query the classes table to find out how many student IDs fall within a row containing the class ID "100"...Hi Brian,
assuming that you´ve established a standard recordset that´s based on such a "WHERE ID = 100" criterion, you can always use DW´s native "Display Record Count -> Display Total Records" behaviour to echo the number of records which match that criterion.
What´s getting evaluated by this behaviour, is the "mysql_num_rows" value, which returns the number of rows in a recordset:
Cheers,
Günter Schenk
Adobe Community Expert, Dreamweaver -
Get Number of rows from a sql query.
I am reading data from a sql query in a BLS transaction and I would like to know the number of rows returned.
Is there an easy way to do this without looping through the table?
Thanks JasperHi Jasper,
You can use the XPATH count function similar to this:
GetTagList.Results{count(/Rowsets/Rowset/Row)}
Kind Regards,
Diana Hoppe -
How do i get the number of rows in Resultset?
Hi,
Is there a way to make the size of string array according to the number of records
from the Resultset? The other problem is the values in saRow are all null.Why is it so? Thanks for helping me in advance
String sql = "SELECT Name FROM UserType" ; << Return "Admin" "User" "CIO"
ResultSet rs = db.ExecuteSQL(sql);
while (rs.next()) {
int i=0,rowCount;
String[] saRow = new String[2]; << supposed to use rowCount
saRow[i++] = rs.getString("Name");
System.out.println(saRow);
//System.out.println
Database Query Success
null << saRow value
null
null
null
null
nullEssentially, you can't. A ResultSet is not a set of rows in memory: it is a cponnection to a back end database. Potentially, you start getting results out of the result set while the database is still searching for more.
I see this all the time on these forums: people doing work in java that is better done on the database. If you want to know the number of records in the database, ask the database: "select count(*) from UserType". Especially once you start needing to search big tables.
However, in this case, where you are just loading up the types: why bother? You know that there wioll only be a few of them. Load the results into an ArrayList. Go to the JavaDocs that you downloaded with the JDK, go to the "guides" section, and see the guide on the collections API. -
Trying to get number of rows in recordset but getRow( returning -3)
Hi all,
I really hope someone here will be able to help me with this, it is driving me mad.
I have a query that is returning records within SQL Server, and i am trying to get the number of records returned for checks within my code. this is what i have at the moment:
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection con = DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs_item;
rs_item = stmt.executeQuery("select TOP 1 ColA, ColB from DATA_TBL_9");
rs_item.last();
int num_recs = rs_item.getRow();
out.println(num_recs); <-- this returns -3 everytime!!!!
I've tried doing:
while (rs_item.next()) {
out.println(rs_item.getRow() + "<br />");
and this increments as expected.
I've read somepeople saying to do a count(*) before each query and to use that, but i am going to have 9 queries running on this page, so would like to use the above method if available to minimize hits on the database, i don't really want to have 18 queries running.
Any ideas?
TIA
TonySarky78 wrote:
Thanks for the quick response, but i'm a novice with jsp. It is not that much related to JSP.
Can you explain what you mean here? Convert ResultSet to List<RowObject>, where RowObject is a javabean representing, well, a single row.
Would this mapping have a performance implication?Not shocking. Maybe a few ms. The benefits are bigger. Clear separation of data and business/view layers, a shorter timespan that the connection has to be open, better and more easy postprocessing in the business and view layers, etcetera. -
How to count number of rows in table
can I get number of row in table except Count(*) in pl/sql
is there any other wayAlso posted and answered here
how to count number of rows in table
count(*) will be the fastest way. It is only slow if the table has a vast number of rows, in which case why do you need to know the tables has 73552436467721 rows and not 73552436467737 rows. It doesn't seem to be much use. Either that or you are counting them a lot, which again seems rather pointless. -
I am getting the following exception in ResultSet updates
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver fo
r JDBC]Can not update, the specified column is not writable.
when i tried to insert a new row in ResultSet object backend is SQL Server
if i sees the privilages of table then it is showing follwing
RezHotel dbo TBL_TEMP dbo dbo DELETE YES
RezHotel dbo TBL_TEMP dbo dbo INSERT YES
RezHotel dbo TBL_TEMP dbo dbo REFERENCES YES
RezHotel dbo TBL_TEMP dbo dbo SELECT YES
RezHotel dbo TBL_TEMP dbo dbo UPDATE YES
means Insert is possible
is it possible to add a new column in ResultSet object
how can i resolve the issue
Message was edited by:I think you need to use the ALTER statement, and have ALTER privilages assigned.
Maybe you are looking for
-
I have recently updated to MacOS 10.5.7 and discovered a strange glitch in viewing certain emails with attachments. For a small minority of emails with attachments, the 'paperclip' icon appears in the mail list, indicating the presence of an attachme
-
Where's the doc for MSI mode?
Edit: I don't need an answer to this, I found the problem. Where can I find the doc to enable/disable it? it says it is "enabled by default", but my managed servers fail to start if the Admin Server is not running (the problem is in the ODSI AppListe
-
Monitor keeps turning off on Wind-Top AE2220
Hi, I've a Wind-Top All-in-One AE2220, starting this morning I keep getting a black screen when I turn on the computer. Pressing the LCD on/off at the bottom right corner will occasionally (2 quick presses seems to do it) get the screen to turn on fo
-
B2B-50079 Transport Error : Bad Message
Hi All, I have an SFTP outbound TPA which is used to transfer a PGP file from our internal system to TP vendor. So the transfer is as follows B2B(Internal System) -> WebServer(acts as proxy while connecting to third party server)-->ThirdParty. The us
-
when i click on an image to edit, then click adjustments (the option next to effects) i reduce the noise in the photo, but when i click done, i don't see the changes ive made to the photo. it only appears when i click edit again.. can you only use th