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?
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....
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???
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 ]