wood burning stoves 2.0*
The moose likes JDBC and the fly likes how to handle result set = null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to handle result set = null" Watch "how to handle result set = null" New topic
Author

how to handle result set = null

Steve Rodgers
Greenhorn

Joined: Jul 03, 2003
Posts: 21
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

Joined: Jul 11, 2001
Posts: 15299
    
    6

Moving to the JDBC forum...


GenRocket - Experts at Building Test Data
Steve Rodgers
Greenhorn

Joined: Jul 03, 2003
Posts: 21
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30085
    
149

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

Joined: Jul 03, 2003
Posts: 21
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30085
    
149

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30085
    
149

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to handle result set = null
 
Similar Threads
inheritance and schema creation using JPA annotations
Mapping BigInteger java object into a ResultSet Data Type
HQL
help me understand this key mapping in "Hibernate in Action"
With out using resultset..?