File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Checking criteria[] size and data[] size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Checking criteria[] size and data[] size" Watch "Checking criteria[] size and data[] size" New topic
Author

Checking criteria[] size and data[] size

Tanya Verevkina
Greenhorn

Joined: Oct 11, 2004
Posts: 5
Hello everybody!
I've just started to work on my assignment. I have a question and I was not be able to find answer here.

Does anybody check if the criteria[] size in find[] is the same as number of fields?
Same question is about data[] in create.
Here is the signatures:
public int [] find(String [] criteria) throws RecordNotFoundException;
public int create(String [] data) throws DuplicateKeyException;
Luc Feys
Greenhorn

Joined: Nov 21, 2005
Posts: 20
Tanya,

I don't check the length of my data[]. If it contains more elements than there are fields in the table, those elements will simply be ignored. If it contains less elements, an ArrayIndexOutOfBounds exception will be thrown.
The criteria[] on the other hand my contain less elements than there are fields since that situation does not in any way obstruct the searching functionality. And if it contains more elements, they will be ignored also.

That's just the way I see it. But maybe other people have better suggestions?

Good luck with the assignment. Hope you have as much fun with it as I do.


Luc Feys
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
I personally think you should check the number of fields for both the create and find methods.

For the create method it provides a simple validation to ensure data consistency, i.e I don't think you can assume that if too many records are provided that you can just ignore the fields at the end. What would happen to your data if the extra field was in the middle of the data array? All the remaining data would be inserted into the wrong field columns, and you would ignore the last field which would be the customer id.

The criteria array for the find method should always be the same length as the number of fields, but the values may be null, Therefore checking the length will help to validate that you are searching against the correct fields.

You can't use JDBC for this assignment, but consider what happens in JDBC if you try to insert a record into the database where the data does not match the fields, you get an SQLException.

Jason
[ March 31, 2006: Message edited by: Jason Moors ]
Carlo Spelgatti
Greenhorn

Joined: Mar 06, 2006
Posts: 10
Hello Tanja,

I check the criteria for a null value and the length musst be the same
as the number of fields in the record. To just ignore invalid arguments, hm, I don't think this is such a good idea. Think of someone who wants to add records to the database. He passes the records with ten fields but
just 8 are stored in the database, because a record has just 8 fields.
After to months someone want to evaluate the data in the database and finds
that information is missing and the whole record is useless (e.g. a bank that records all ATM withdrawals and the last two fields are the amount withdrawn and the customer id) and the missing information can not be restored.

greeting, carlo


Carlo Spelgatti<br />Dipl. Informatik Ing. ETH / SCJP / SCJD<br />Ch´┐Żngengass 3<br />8805 Richterswil<br /> <br />P: 044 793 10 50<br />M: 078 698 07 92<br />ICQ: 401810960
Luc Feys
Greenhorn

Joined: Nov 21, 2005
Posts: 20
Jason,

you're right. Your solution is a lot more user-friendly and will prevent the user of the database to make silly mistakes with the data that is passed to the add, update and find methods.
I will add a check on the number of fields in all the required methods and while I'm add it, I'll also check the length of the field data that is provided by the clients. Now field data that is too long was silently truncated.

Thanks for making me aware of the advantages of not being lazy.

Luc Feys
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
Yes, I forgot to mention the checking of the string against the field length. In the create and update methods I compare the length of the String provided against the metadata for each field.

Jason.
Tanya Verevkina
Greenhorn

Joined: Oct 11, 2004
Posts: 5
Thank you all very much. While waiting for anybody to reply, I made my own decision to check the size of those arrays. Still, I'm very glad that I am not the only one who consider to do that.
 
 
subject: Checking criteria[] size and data[] size