• Post Reply Bookmark Topic Watch Topic
  • New Topic

What would be returned when the record is empty?  RSS feed

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chit Ming Chong
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul

so the answer is an emoty resultset instead of null?
 
author & internet detective
Marshal
Posts: 37698
575
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37698
575
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. Noting that the 0 returned by select count(*) is a row with a value.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!