• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find a value between two date range in Java and MySql

 
Mohammad Ashari Rahman
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. here is my code



am I right? please tell me the correct way if this is wrong.

thanks
 
Paul Clapham
Sheriff
Posts: 21557
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a bad try, actually. You used a PreparedStatement, which is a good thing to do and we often see code here where people do horrible string concatenations which don't quite work out. So +1 for that.

But there are a few things worth commenting on.

1. You should leave lines 11 to 13 out; if the ResultSet is empty then just return an empty list, not null. Your users will not thank you for having to consider the possibility of null values being returned.

2. Once you have finished reading from the ResultSet you should make sure to close it. Either use a finally block, or preferably if you can use a try-with-resources statement to make sure it's closed. And the ResultSet isn't used outside the method so the variable rs should be a local variable.

3. You should create an object -- let's call it Event -- from each row to hold the five data items, and return a list of Event objects. That's going to be much easier to deal with in the calling code than a list of the pieces of each event.

4. Your method's return type should be List<Event>, not ArrayList<Event>. Unless there's some particular reason that you MUST have an ArrayList and not a LinkedList or any other kind of List, you shouldn't force the calling code to deal with that implementation detail.

5. What does your method do? The name you chose suggests it searches event dates. But that isn't quite what it does. It actually returns a list of events. So calling it "getEvents" would be better.

6. You're passing String objects for the second and third parameters. There's a built-in assumption that those String objects can be handled by the Date.valueOf() method, in other words that they are formatted correctly. This could cause problems; you might have to expend a lot of coding effort to make sure that happens, or if you don't then it might not happen and unchecked exceptions would be thrown. Passing Date objects instead would be preferable, if you could arrange that.

That's all I've got for now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic