This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing data in ResultSet object

 
lavanya shastri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I need a little help with using the ResultSet object.
Basically, I have an sql query that is something like below

select count ( * ) from "UserBook" where "orderStatusID"= (select id from "OrderStatus" where name='NEW')

I execute this query and read it into a ResultSet object.Now the my problem is since the data baing returned is an Integer and not a bunch of rows/columns how do I access it from the ResultSet object

String query = select count ( * ) from "UserBook" where "orderStatusID"= (select id from "OrderStatus" where name='NEW') ;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);

How do I access the integer returned to the ResultSet object.

Thanks,
LS
 
M K Rayapudi
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you tried?
rs.getInt(1);

 
Anshul Agrawal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
write this query as
select count ( * ) AS COUNT from "UserBook" where "orderStatusID"= (select id from "OrderStatus" where name='NEW')

and fetch the record like
if(rs.next())
{
int count = rs.getInt("COUNT");
OR
int count = rs.getInt(1);
}
 
lavanya shastri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rayapudi Murali Krishna wrote:have you tried?
rs.getInt(1);



Yes it works now

@Anshul Agrawal Thanks!Appreciate the quick response
 
Scott Selikoff
author
Saloon Keeper
Posts: 4028
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note, I really dislike reading code that uses integers for result sets, such as rs.getString(1). Name your projection variables, even if they are aggregate functions! Using rs.getInt("COUNT") is so much more illustrative.

There are so many reasons integer indexes can be bad, I'll let you guys fill in the blanks...
 
Neena Rangnani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Scott,

The one reason I can explain is If we use integer numbers to access columns , in that case if the table is altered later on (any column is deleted or appended) , then at every place in code there is need to modify numbered projections.

however if we use named projection variable , there is no impact on existing code. Only need to alter relevant code for(deleted/new column).

Let me know if you have anything to add up.

Thanks,
Neena
(New Member)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic