aspose file tools*
The moose likes JDBC and the fly likes What would be returned when the record is empty? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "What would be returned when the record is empty?" Watch "What would be returned when the record is empty?" New topic
Author

What would be returned when the record is empty?

Chit Ming Chong
Ranch Hand

Joined: Sep 09, 2004
Posts: 49
Dear All,

I need to check whether a record has been created. If not, I will create it using JDBC. I was wondering what would the ResultSet contain if no record is returned? Would it be null? or something else? I would have tested it however I don't have a compiler handy at this moment

Currently, i am doing it using count(*) to see it return 0 or not.

Thanks for your help.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If you are using count(*) you know that your ResultSet will always contain 1 record. If you do a "Select * from whatever" and the table is empty, so is your ResultSet.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Chit Ming Chong
Ranch Hand

Joined: Sep 09, 2004
Posts: 49
Thanks Paul

so the answer is an emoty resultset instead of null?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30938
    
158

Chit,
Queries never return null resultsets. So yes, you would get an empty resultset back.

That said, I prefer the select count(*) approach. It better expresses your intent by showing you are only using the count, not the data. It can also be faster depending on your indexes and schema.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Chit Ming, When ever a select query doesn't return any rows
rs.next() returns false when you call it for the first time itself.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30938
    
158

Right. Noting that the 0 returned by select count(*) is a row with a value.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What would be returned when the record is empty?