File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes How to Check whether an object contains data/values or not? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to Check whether an object contains data/values or not?" Watch "How to Check whether an object contains data/values or not?" New topic
Author

How to Check whether an object contains data/values or not?

prasad boini
Greenhorn

Joined: Nov 22, 2012
Posts: 8
I want to search a particular employee based on search criteria .

Here I used many If conditions, Is There any Way To simplify the Below code.

Employee.java
===========


===========
EmployeeDao .java
============


Thanks & regards
prasad
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

Not really, no. You need code of about that level of complexity. That's because you need separate code for each field of your model object.

However your code which decides whether to precede a comparison with "where" or "and" is a problem, because (1) it's too complicated, and (2) it won't work. I suggest that you need a local boolean variable which keeps track of whether you've used "where" or not. Use that to output "where" before your first comparison and "and" before the others. That's much simpler than your code which compares longer and longer lists of fields to null, and it should work better too. Your existing code can easily produce invalid SQL.

(Yes, I'm aware that your code isn't meant to be production-level code yet, so it wouldn't be surprising to find flaws in it. I'm just pointing out what I consider to be a major flaw.)
prasad boini
Greenhorn

Joined: Nov 22, 2012
Posts: 8
@ Mr.Paul Clapham,
Thank You very much.
Yes Mr.Paul ,The code whatever i wrote here ,it produces invalid SQL query(it throws exception),
Here I forgot to enclose single quotes ..

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18993
    
    8

Yes, that's the sort of error it's easy to make when you build an SQL statement using string concatenation like that. Normally I would recommend using a PreparedStatement, which simplifies building the SQL and also protects against SQL injection attacks.

However in this case if you do that then your code will need two phases: the first would build the SQL for the PreparedStatement, and then the second would fill in the parameters. Both phases would have to do similar things, i.e. going through the fields of your model object and executing code if the field wasn't null. That's a considerable increase in complexity, so for now I would suggest you don't bother with that. Get a working version of what you have so far. But if the code becomes production code, i.e. not just a school exercise but real-life code, then I would suggest revisiting it and using PreparedStatement with the added complexity.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Check whether an object contains data/values or not?