wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Database constant visibility 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 "Database constant visibility" Watch "Database constant visibility" New topic
Author

Database constant visibility

Pete Palmer
Ranch Hand

Joined: Oct 21, 2008
Posts: 94
Hello,

The name, location and customer text fields are provided on the GUI, and the length of these are limited as per the database schema. In the data layer I have a class DatabaseSchema, which captures this :-



At the GUI layer, I need to constrain, the length of name, location and customer in line with the above. To facilitate this, I can either :-
1) reference the above constants directly from the GUI.
I feel it is not good OO principle to expose such detail at the GUI layer.
2) define new constants in the GUI,
By defining new constants at the GUI layers is effectectively duplication of the same information as held in the data layer.

Any opinions on which approach is good or if there is an alternative.

Regards

Pete


Anne Crace
Ranch Hand

Joined: Aug 29, 2005
Posts: 223
I use combo boxes that get populated from the database for the name and location, so there's no chance of a) typos b) overwriting the field length. For the owner field I use a MaskFormatter to force input to 8 digits. There is an example in Andrew's book on how to do this. Declaring new constants is an OK option for the GUI if you want to use text inputs.


SCJP, SCJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Hi Anne,

Just curious to know if you already decided when to repopulate your combo boxes? I was planning to use that approach too, but because i was doubting when to refresh the data (and keeping it easy and simple) just used the jtextfield, using a DocumentFilter to allow a maximum number of characters (or digits).

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Anne Crace
Ranch Hand

Joined: Aug 29, 2005
Posts: 223
Hi Roel,

I am currently testing exceptional conditions in my GUI. I have listeners registered on the combos, but so far, I only populate them once. I have other issues to fix right now
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2267
    
    3

Howdy, Pete! Good to see you around, champ!

Partner, here's what I did: the JTextFields where the user has to provided the required info are limited to the maximum length of each field. For instance, the customer ID is limited to 8 characters. In the Data class, before updating or creating a record, each String in the String arrays is filtered. For instance, when creating a record, for each String in the String array, I verify if its length is greater than the maximum length; if so, I just get the first n characters of it.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2597
    
    9

Ah that JTextField vs JComboBox debate again. I used JComboBox for my name and location in my search dialog. Roel asked when should these boxes repopulate? Well in my app, everytime the search dialog opens. Unless the RMI server shutdowns that would make the whole app to exit too.

In my GUI, I provided create dialog too. This dialog I used JTextField. To keep track of how many characters/digits are inputted, I used a DocumentListener on the text field. Say the name can have up to 64 chars, when user enter 65 chars, it pops up error. You get the idea.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Hi K. Tsang,

The original question of Pete was/is: if you have a static database schema (hard coded all values) what do you do in the GUI to limit the characters a user can enter in a JTextField? Do you reuse the DatabaseSchema-values in your GUI too? do you create a seperate interface (in the GUI package) keeping all these constants (a 2nd time)? ... or do you follow an other approach (like Roberto: substring in the Data class any value that is longer than allowed by the database schema)?

The combobox vs jtextfield was an off-topic remark of me (we had a big discussion some weeks/months ago about when to refresh these combos) and i was curious if Anne had already made a decision (because she was still doubting about it then, like me). And your solution is an excellent one if you have of course a seperate search dialog, otherwise it would be hard doing it like that At the end I decided just to use simple JTextFields (because scjd assignment is hard enough ), but still interested how other people solved the refreshing issue

Kind regards,
Roel
Pete Palmer
Ranch Hand

Joined: Oct 21, 2008
Posts: 94
Firstly thank you all for your helpful responses.

Anne, I am pretty much commited using the JTextField approach, using the JComboBox at this late stage for me is probably not an option. Just might go with your suggestion of defining a constant in the GUI.

Roberto, thats an interesting and obviously a successful approach, however, I perform a check on each of the array elements and throw IllegalArgumentException if any don't agree with the schema.

K, the 64 LOCATION_LENGTH limit, is that a constant you have defined in your GUI or are you reading it from the data layer? If it is the former, is it effectively duplicating the constant already defined in the data layer ?

Roel, Thank you for the clarification of my query.

Regards

Pete


K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2597
    
    9

Pete Palmer wrote:K, the 64 LOCATION_LENGTH limit, is that a constant you have defined in your GUI or are you reading it from the data layer? If it is the former, is it effectively duplicating the constant already defined in the data layer ?


Hi Pete, I do have a constant class but did not have the constants for the database schema - rather constants like default server port, local/network option, magic cookie value. The schema field length for the gui is hard-coded. I didn't read it off from my Data class which I could have done but that would make my interface clumsy.
Pete Palmer
Ranch Hand

Joined: Oct 21, 2008
Posts: 94
K,
I think I will go with defining a constant in the GUI.
Thank You.

Pete
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database constant visibility