First of all I'd like to thank everyone here who had helped me before. I've been on the Learning
Java Path for almost 7 months now and at the beginning of this month I finished my first working project which uses database connectivity, XML Transformations, Swing, JavaMail, and probably a lot of other stuff I didn't know much about earlier. Anyway, the program I made is functional and does what it's supposed to but it...sucks. It's just not tidy enough and although it's not too difficult to follow, I'm starting from scratch and making it bigger and better.
I do have to say that apart from reading books, it was the forum answers and working on my own project that helped me the most. Books are great, but until you get your hands dirty, it's difficult to connect the dots. The new project I'm working on uses inheritance and so things like calls to super(withparams) and
polymorphism begin to align and you say to yourself, "Oh, so this is how it works." The knowledge was there beforehand, but it just didn't click. I used to want to get things done just right the first time and I'd never get anything done. Now I'm just inching forward with trial and error/learning and actually accomplishing something.
Anyway, so I'm here to ask more questions.
1. Primary keys
Okay, so this isn't strictly Java, but does it make sense to use a VARCHAR as a primary key if the table is a
word table? Or should I be using a PK INT and VARCHAR word combo? The database is embedded (Apache Derby), it's a single user application and there will be as many words as there are in a particular language. Not a huge database, single-user, so performance will probably not be an issue either way, but just curious.
2. Packages
My project is getting bigger. I've set up GIT for versioning and all is well, but there are just too many classes in the tree so I've decided to break them up into different packages as such:
and
The problem is that I've given most classes in Package default access and so Subpackage code can't see them. I could just make them public, but I'm thinking - how is this usually done? I mean, I don't want a tree with 20+ classes, I want to group them.
3. toString() and Swing components
Let's say I have a JTree and I need to load people's names from a database. So the Person class mirrors the db structure and a new Person object is created for each database Person record and added to a collection.
Now I want all those people in the JTree, but if I use Person references to create DefaultMutableTreeNode objects, the tree list will be a list of toString() calls. So what if the toString() also lists an address, e-mail, phone number? I don't want that. I just want lastName, firstName on the list.
Here's how I did it before, but I don't like it:
1. The list would show personID, lastName, firstName
2. To get to the person object, the program would parse personID and retrieve the appropriate person from the collection.
I'd like to have direct access to Person. I could modify the toString(), I could also parse the toString() to get the firstName and lastName, but it all seems so clunky to me. Surely there must be a better way.
4. Enum singleton initialization
I used the enum singleton
pattern to get access tp the Apache Derby embedded database. The enum is called DerbyDB and there's an INSTANCE in there. The problem here is that the database connection is opened when DerbyDB is first accessed, but I want it to load with the program and perform appropriate initialization. I've read about static initializers and think it might be the way to go, but I'm not exactly sure how to do it.
So I suppose that's it. As always, all input and constructive criticism are greatly appreciated.