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 URLyBird - Data.java 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 "URLyBird - Data.java" Watch "URLyBird - Data.java" New topic
Author

URLyBird - Data.java

John R Roberts
Ranch Hand

Joined: Dec 27, 2006
Posts: 36
Hi all, I have a question regarding the Data.java class. Is it permissible to add new methods to the class? My Data.java implements DB.java which has the following methods..


So how do I read all records initially to populate the JTable in the GUI? I will not know the recNo that the read method requires. Is it OK to add a readAll method that will read every record from the database?

thanks,
John R Roberts
Ranch Hand

Joined: Dec 27, 2006
Posts: 36
Hi, I was thinking about a possible solution and came up with the following.
In my Data.java class I have the method . Now as I mentioned before, at startup I believe the user would want to see all records but as the software/user wont know anything about how many records there are or the individual record numbers, I will pass a -1 to the method and use this as an indication to read everything and pass the array of results back to the adapter class that calls into the Data.java class.
Any thoughts....
josine wilms
Ranch Hand

Joined: Jan 23, 2006
Posts: 45
Hi,

Yes it's permissable to add methods to the Data class. It is not permissible to add methods to the DB interface.

Cheers,
Josine


SCJP 1.4<br />SCWCD 1.4<br />SCJD <br />SCEA (new version) Work in progress...
John R Roberts
Ranch Hand

Joined: Dec 27, 2006
Posts: 36
so what do you all think of my proposed solution to read all records from the database by passing a '-1' to the method to indicate that all records should be returned? As this method returns a String[], returning all records would not be an more difficult than returning one if the method was passed a non-negative number.
any thoughts???
what has anyone else done for the URLyBird exam???

many thanks
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
I followed a completely different approach. I simply implemented the the DBAccess interface in the Data class, I added a couple of helper private methods in order to refactor some code, but nothing new.

In order to get all records, I reused the findByCriteria() method by means of passing empty Strings. Now, this method just returns an array with the record numbers. You could go over that array and reuse the read() method to get the record.

That is why I am not using the Data class directly. I created a wrapper class that offers more business-aware methods that reuse the core functionality offered by the Data class. Among these methods in this wrapper class I offer functionality to get all records using the underlying findByCriteria() method.

Evidently using the findByCriteria() method to achieve this task has a drawback: the comparison algorithm is irredeemably invoked while fetching every record in order to check if the record matches the defined search criteria, since we are fetching all the records, every comparison will finish in match. So all records will match, and then we will have to perform a second read to get every matched record.

So, this approach implies two file reads and the execution of the comparison algorithm.

Probably an expensive alternative, but I did not want to offer any functionality that was not completely based on the interface provided by Sun.

In the approach you are suggesting, the problem is that you are actually violating the DBAccess interface contract. Your implementation is doing more than what the interface contract suggest.

If you find necessary to implement this method here, I'd rather suggest to overload the read() method, with no parameters, and return an array of arrays or a collection of arrays, or another kind of complex structure that you can come up with.
[ December 29, 2006: Message edited by: Edwin Dalorzo ]
John R Roberts
Ranch Hand

Joined: Dec 27, 2006
Posts: 36
thanks for the response, sounds like a more sensible approach. I was concerned about using the -1 as an indicator to read all records.

cheers...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: URLyBird - Data.java
 
Similar Threads
[B&S] lock/update/read method
IOException in db find method
URLyBird interface?
Trouble understanding the Data.java logic
Bad start: Errors in the interface.