Solved

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

Posted on 2001-08-24
23
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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
 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

717 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