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

how to handle result set = null

 
Steve Rodgers
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Database was set up prior to me coming on board. If the data exists, it is loaded into the database table otherwise it is left off. No flags. For example, we have a users table and we have a cars table. If a user has a car, then the user_id is on the car table. If a user does not have a car, the user_id is not on the car table.

I have to return whether or not a user has a car. If they have a car, I need to return 'Car', otherwise 'No Car'. I cannot change the tables. Any suggestions?
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the JDBC forum...
 
Steve Rodgers
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies for the code. Let me know if there is a simpler more efficient way. Basically I am reading through a collection if users fed in elsewhere and determining if they have a car.



 
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
Steve,
A resultset can never be null. If there aren't any results, you will get a resultset object whose next() method returns false on the first call.
 
Steve Rodgers
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne,

Thanks! Ahhhhh what was I thinking? :roll:

My new approach is to write an isCar() and setCar().

Then I can do something like (foo.isCar()?"Car":"No Car")

This way I can go through my collection of Users and determine if they have a car or not. Implementing this is taking me a bit though. I haven't figured it out. How do I put this in? Any pointers would be helpful. Thanks
 
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
I would actually make car a boolean field in your object, rather than a string. It saves you the string comparison as you can just use the boolean directly.
 
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
Steve,
I just re-read the initial post and want to clarify what I just posted. While you can't change the database, you can still have your java object store a boolean if you want. You can even have an isCar() method that returns the boolean and a getCar() method that returns 'Car' or 'No Car'. That way the logic is more centralized. But that's more refactoring than your actual question.

For the query, you can do something like this:

This query will return a result set with one row and one column. That entry is zero if the answer is no car and more than zero if the answer is car.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic