Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 390499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:58:14+00:00 2026-05-12T15:58:14+00:00

This is another debated subject, but this time i am searching only for the

  • 0

This is another debated subject, but this time i am searching only for the simple and documented answers. The scenario :

Let’s assume the following method:

 public static Hashtable<Long, Dog> getSomeDogs(String colName, String colValue) {
  Hashtable<Long, Dog> result = new Hashtable<Long, Dog>();
  StringBuffer sql = null;
  Dog dog = null;
  ResultSet rs = null;
      try {
          sql = new StringBuffer();
          sql.append("SELECT * FROM ").append("dogs_table");
          sql.append(" WHERE ").append(colName).append("='");
          sql.append(colValue).append("'");
          rs = executeQuery(sql.toString());
              while (rs.next()) {
                  dog= new Dog();
                  //...initialize the dog from the current resultSet row
              result.put(new Long(dog.getId()), dog);
              }
          }
     catch (Exception e) {
         createErrorMsg(e);
         result = null; //i wonder....
         }
     finally {
         closeResultSet(rs); //this method tests for null rs and other stuff when closing the rs.
     }
   return result;
 }

Questions :

1. What ways do u suggest on improving this technique of returning some dogs, with some attribute?

2. rs.next() will return false for a null ResultSet, or will generate an exception, like this one :

String str = null;

System.out.println(str.toString());

3. What if, while initializing a dog object from the current row of the ResultSet, something bad happens, like : connection failure, incompatible value has been passed to dog property setter, etc? I might have 10 elements in the hashtable by now, or none (first row). What will be the next action : a) return null hashtable; b) return the result hashtable, the way it is at this stage; c) throw an exception : what will be the exception type here?

4. I think u will all agree on this one : nothing bad happens, there are no rows on the interrogation, a null value will be return. However, @Thorbjørn Ravn Andersen said here that i should return a NullObject instead of a null value. I wonder what that is.

5. I’ve noticed people and groups of people saying that one should split the application into layers, or levels of some kind. Considering the example above, what layers are here, except these ones i can think of :

Layer1 :: Database layer, where actions are performed : this method.

Layer2 :: ??? : some layer where the new Dog object is constructed : my Dog object.

Layer3 :: ? : some layer where i intend to do something with the collection of dogs : GUI layer, mostly, or a sub-layer of user interface.

Following the application flow, if an exception occures in the 1st layer, what is the best think to do with it? My thoughts : catch the exception, log the exception, return some value. Is that the best practice?

Manny thanks for your answers, i look forward to see what other people think of these matters.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:58:14+00:00Added an answer on May 12, 2026 at 3:58 pm

    You ask five questions

    1. What ways do u suggest on improving this technique of returning some dogs, with some attribute?

    Several, actually.

    • Your method is static – this isn’t terrible, but leads to you using another static “executeQuery”, which smells of Singleton to me…
    • The class “Dogs” violates an OO naming practice – plural nouns don’t make good class names unless one instance of the class holds a collection of things – and it appears that Dogs is actually “Dog”.
    • HashTable is all but deprecated. HashMap or ConcurrentHashMap give better performance.
    • I can’t see a reason to create the first part of your query with multiple appends – it’s not bad, but it’s less readable than it might be, so sql.append (“SELECT * FROM dogs_table WHERE “); makes a more sensible beginning if you’re just going to hardcode the selected columns (*) and table name (dogs_table) anyway.

    2. rs.next() will return false for a null ResultSet, or will generate an exception

    This doesn’t seem to be a question, but yes, rs.next() returns false as soon as there’s no longer any rows to process.

    3. What if, while initializing a dog object from the current row of the ResultSet, something bad happens

    If “something bad happens”, what you do next is up to you and your design. There’s the forgiving approach (return all the rows you can), and the unforgiving (throw an exception). I tend to lean towards the “unforgiving” approach, since with the “forgiving” approach, users won’t know that you haven’t returned all the rows that exist – just all the ones you’d gotten before the error. But there might be cases for the forgiving approach.

    4. I think u will all agree on this one : nothing bad happens, there are no rows on the interrogation, a null value will be return.

    This isn’t something on which there’s an obvious right answer. First, it’s not what’s happening in the method as written. It’s going to return an empty HashTable (this is what was meant by a “null object”). Second, null isn’t always the answer in the “no results found” case.

    I’ve seen null, but I’ve also seen an empty result variable instead. I claim they’re both correct approaches, but I prefer the empty result variable. However, it’s always best to be consistent, so pick a method of returning “no results” and stick with it.

    5. I’ve noticed people and groups of people saying that one should split the application into layers, or levels of some kind.

    This is harder to answer than the others, without seeing the rest of your application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this another MS Access 2000(2007)-ism? If I type the following: foo = blah
(I asked this question in another way , and got some interesting responses but
This isn't another debate on brackets vs dot-notation. I noticed in Xcode that when
If I've missed this in another question I apologize; I looked for a good
i need to show a menu that derive another menu and this derive another
Ok, this is another one in the theory realm for the CS guys around.
I have a file like this: This is a sentence. This is another sentence.
I'm writing bug tracking software in PHP, and today I saw this in another
This question follows another, just solved here Now I want to do a different
I had this answer on another post I asked: I believe the VS designer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.