Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What would be returned when the record is empty?

 
Chit Ming Chong
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.
 
Paul Sturrock
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?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
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.
 
Srinivasa Raghavan
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: 34095
337
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