This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes how to define criteria for search Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "how to define criteria for search" Watch "how to define criteria for search" New topic
Author

how to define criteria for search

Zonglin Li
Greenhorn

Joined: Aug 29, 2008
Posts: 12
I am doing the Bodgitt and Scarper. I am a little bit confusing about how to define the criteria in my �find� method.

My find is defined like this:
Field n in the database file is described by criteria[n]. A null value in criteria[n] matches any field value. A non-null value in criteria[n] matches any field value that begins with criteria[n]. (For example, "Fred" matches "Fred" or "Freddy".)

Could anyone explain this please!

Thank you very much!!
Alecsandru Cocarla
Ranch Hand

Joined: Feb 29, 2008
Posts: 158
If you have a record and criteria that look like:

you have a match, and should return the record (or it's id) as found.

If you have:

then you don't have a match.

Is this an answer that you expected? If not, please rephrase your question.


SCJP 1.4 100%
SCJD 99.5%
Zonglin Li
Greenhorn

Joined: Aug 29, 2008
Posts: 12
Hi Alecsandru:
Thank you very much for your reply. It is very clear.

If I want to get all the records, I could just set the criteria to: null null null null null. So I don�t need to create a method to get all the records. Do you think so?

Cheers
jin Young
Greenhorn

Joined: Nov 03, 2008
Posts: 21
Hello, i append question, thanks.

The requirement of search function may only contains some of the fields. For example name and location is required to search, so the criteria[] array may only need to contain the two element? is it not neccessary to contains all fields in the db data file?

Thanks a lot.
Alecsandru Cocarla
Ranch Hand

Joined: Feb 29, 2008
Posts: 158
Originally posted by Zonglin Li:
If I want to get all the records, I could just set the criteria to: null null null null null. So I don�t need to create a method to get all the records. Do you think so?

To be on the safe side, I'd say you are not allowed to change the interface you received in the assignment. If you have a method in the interface which is something like getAllRecords(), then you must implement it. If not, then, in order to retrieve all the records, you should call the find() method with an all-null criteria. (This is also valid the other way round - if you call the method find() with all-null criteria, you should return all records).

I'd also recommend that you test your implementation with something like JUnit in order to be sure that everything works as expected.
Alecsandru Cocarla
Ranch Hand

Joined: Feb 29, 2008
Posts: 158
Originally posted by jin Young:
Hello, i append question, thanks.

The requirement of search function may only contains some of the fields. For example name and location is required to search, so the criteria[] array may only need to contain the two element? is it not neccessary to contains all fields in the db data file?

Thanks a lot.

The database access layer provides a lower-level access to data (for example uses arrays, and provides basic functions - like lock, unlock, update, delete, etc - functions that you must call in a certain sequence in order to "do" something - e.g. lock/update/unlock for "updating"). But, at the same time, it's broader - meaning you can do almost anything with it, including searching for something that your client side will not need (like matches on something else than "location" and "name").

So, yes, I'd say you should implement a criteria with all fields, not only 2 of them. But the decision is yours, and of you decide otherwise, you should document it in your choices file.
I wouldn't (and I didn't) implement searching only for two fields...
Martin Castellanos
Greenhorn

Joined: Sep 23, 2008
Posts: 10
Hello,

I'm a little confused about this:

In the User Interface section says:
The user interface for this assignment must satisfy the following criteria:

* It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user.


But in the DB Interface I got:
// Returns an array of record numbers that match the specified
// criteria. Field n in the database file is described by
// criteria[n]. A null value in criteria[n] matches any field
// value. A non-null value in criteria[n] matches any field
// value that begins with criteria[n]
. (For example, "Fred"
// matches "Fred" or "Freddy".)


Which one should I implement? It would very silly to implement the db in a way that finds partially matched criterias and then filter those results to provide exact matches only.


Any recommendations? Regards
Alecsandru Cocarla
Ranch Hand

Joined: Feb 29, 2008
Posts: 158
If you read carefully my previous post, you'll see that what DB provides is different from what the client really uses. So you must apply some more restrictions in another layer (business layer, for a three tier architecture, or client if you're using a 2-tier).

Anyway, try to search around for answers to questions like these. They've been asked and answered quite often until now.
[ November 15, 2008: Message edited by: Alecsandru Cocarla ]
Karen Smrha
Greenhorn

Joined: Nov 13, 2008
Posts: 11
Martin,

You asked about the search (exact match) for the GUI vs. the
starts with search defined in DBMain.

Which one should I implement? It would very silly to implement the db in a way that finds partially matched criterias and then filter those results to provide exact matches only.


Your Data class must contain a find
method that exactly matches the functionality and signature defined
in your instructions. SUN tests your GUI and they also directly
connect to you Data class to check that DBMain is implemented.

DBMain is an ugly interface. Design-wise you need to decide how
you want to handle these limitations. The simplest solution is
to "work with the limitations". For searching that would mean
that whatever calls Data has to filter the results.

If you want to have your database provide "exact match" searching
you need to design your system so that it implements
"more than just DBMain".

One option is to have a second interface that
provides extra and/or overloaded methods. The second
interface can extend DBMain so that Data implements all
of the methods SUN expects plus the ones you added.

There are many useful design discussion on this forum.
If you are not in a rush, I recommend taking time to think about
your design and draw it on paper before you start coding.

Good luck with your project,
Karen


SCJD working on B&S
Martin Castellanos
Greenhorn

Joined: Sep 23, 2008
Posts: 10
Thank you very much for the clarification.

Regards
 
Don't get me started about those stupid light bulbs.
 
subject: how to define criteria for search
 
Similar Threads
A null value in criteria[n] matches any field value
URLBird find by Hotel or Name
NX: findByCriteria Method
B&S: Passed 373/400
Urly bird find method: are search criteria "and"- or "or"-composed?