• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

URLyBird - Data.java

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
John R Roberts
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
pie. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic