Solved

how we can get the no of records from a resutset object?

Posted on 2001-08-24
23
170 Views
Last Modified: 2008-02-01
hi everybody,
I want to get the no of records(count) after executing a select statement using jdbc1.0

Regards

Harish
0
Comment
Question by:hkg
  • 8
  • 6
  • 2
  • +6
23 Comments
 
LVL 2

Expert Comment

by:ia_ia_ia_1
ID: 6421539
U can't do that - as a matter of fact I have not seen any implementation for jdbc 2.0 as well but there might be some.
The only solution is to execute one more query for getting the count.
0
 

Expert Comment

by:isond
ID: 6422142
Hi,
Instead of running another query to get the count you could just run through the resultset in a loop.

Something like

count=0;
while(rs.next())
{
   count++;

}

count will contain the number of records. You could either do the rest of your processing in a loop or use rs.first() to go back to the first record assuming your result set is not TYPE_FORWARD_ONLY.

Hope this helps.

Darren.
0
 
LVL 2

Accepted Solution

by:
ia_ia_ia_1 earned 100 total points
ID: 6422242
The first() method is since JDBC2. When using jdbc 1.0 u can't go back in a result set nor use an absolute positioning (as far as I remember).
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
ID: 6422268
hkg, if u give us some more details, maybe we could find the most suitable solution
0
 
LVL 1

Expert Comment

by:kanthonym
ID: 6422728
Use the ResultSetMetaData, this will give you infomraiton about your query.

An object that can be used to get information about the types and properties of the columns in a ResultSet object. The following code fragment creates the ResultSet object rs, creates the ResultSetMetaData object rsmd, and uses rsmd to find out how many columns rs has and whether the first column in rs can be used in a WHERE clause.


     ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
     ResultSetMetaData rsmd = rs.getMetaData();
     int numberOfColumns = rsmd.getColumnCount();
     boolean b = rsmd.isSearchable(1);

 
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
ID: 6426673
kanthonym, the number of the rows in needed - not the number of columns
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
ID: 6426683
Harish, u have two possibilities:
1. If u need the count before if u have rolled to the end of the result set, u'll have to perform an additional query.
2. Otherwise, u could count the rows as u go along the result set
0
 
LVL 92

Expert Comment

by:objects
ID: 6427043
ia ia ia 1,

Don't mean to hassle but the ee guidelines for proposing an answer are that it will solve the question and doesn't duplicate previous comments. Both those possibilities have already been mentioned (and possibly doesn't answer the question).

Proposing answers also takes the question out of the main pool lowering it's visibility to other experts who may have valuable suggestions, and should only rarely be used if ever IMO.
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
ID: 6427775
objects, I've met such a question (on the count of the rows in a result set) so often and I have never found an answer different from mine - I'll be very surprised if such an answer could be provided here (and glad, too). Why then not posting answer - it could be rejected at any time...
And besides, the EE staff had not provided the Answer radiobutton for the sheer beauty of it, I reckon...
0
 
LVL 92

Expert Comment

by:objects
ID: 6427785
> Why then not posting answer

Cause it moves the question out of the open question list, reducing the chance of someone else contributing to find a solution.

I'll turn your statement around and ask 'Why post an answer (as opposed to a comment)?'

:)
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
ID: 6427818
Cause I am answering - not commenting :)
Comment is when one is not quite sure or provides a partial solution - as I see it.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 92

Expert Comment

by:objects
ID: 6427834
Read the guidelines below.
Generally people just post comments, and in fact there is no reason to post an answer.

Personally don't know why they provide the option.
0
 

Expert Comment

by:harshita
ID: 6434498
I have an answer for it. But right now question is locked. if it is open again i can give the good answer.It is possible to get the row count in the query itself.

Let me know if you need the answer.
Could you give me 100 poiints for the answer?

Regards,
Murthy
A.Murthy@gecapital.com
0
 
LVL 92

Expert Comment

by:objects
ID: 6434698
Good point, how did that option get missed :)
0
 
LVL 2

Expert Comment

by:ia_ia_ia_1
ID: 6435553
Surely it is possible to combine the count query and the real one in one query - I myself haven't used this approach. In fact I don't like it since everything I've seen of the kind involves running the select count() query for each result set's row.
As in:
select a.*, (select count(*) from iportal_owner.t_domain_value a where domain_id =15)
from iportal_owner.t_domain_value a where domain_id =15

It may be that harshita's implementation is different, of course - I'll be glad to see it, in that case.
And I admit I missed to mention the option, objects :)
Harish, if you prefer the harshita's approach, u can always reject my answer. I was searching for the withdraw answer functionality but it seems to be disappeared.
0
 
LVL 92

Expert Comment

by:objects
ID: 6435584
> Could you give me 100 poiints for the answer?

Are we bargaining for points now?

I'll give u the answer for the 50 on offer :-)

> And I admit I missed to mention the option, objects :)

:-)

0
 

Expert Comment

by:harshita
ID: 6437600
I was just kidding. please don't take it serious.
ALways there is a way to do this type of things.
Accordng to the question, this is the right answer for this question. And there is no wrong in doing this type of query. My answer is also the same.


Regards
Murthy
A.Murthy@gecapital.com

0
 
LVL 92

Expert Comment

by:objects
ID: 6438696
No worries, I wasn't hassling.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6727981
This question is LOCKED with a Proposed Answer.  If it helps you, great, accept it and grade it to close.  If not, reject it and comment as to why or what else is needed.

Few additional experts will join this collaboration effort, once a question has been locked.  Just want to confirm this to you.  If more than one expert helps you, you can always split points or award additional help in a new question, within the same topic area.  If you need help from us, post a zero point question here, and include the link:

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

Moondancer
Community Support Moderator @ Experts Exchange
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7114478
  Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY );

   ResultSet rst = stmt.executeQuery( strSQL );

   rst.last();
   int numberOfRows = rst.getRow();
   rst.beforeFirst();
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7115082
Asker is not finalizing this, and I request Expert guidance here to close this.
Thanks,
Moondancer - EE Moderator
0
 
LVL 5

Expert Comment

by:vemul
ID: 7773779
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points to  ia_ia_ia_1

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 7861672
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now