Java MySQL data abstraction



  • Hi, I've been working on a internal quality assurance application as of late. I'm using a mysql database, as well as a swing gui client app. Also have a custom XMLmessenger app on the database server. Anyways, this is my first time doing something like this and it feels like it's grown out of my control. I'm not really sure how I should be designing this. Anyways, if you'd like, feel free to look at my code and shout wtf. 🙂

     

     This is a sample of three of the many backend classes I've written

    http://papernapkin.org/pastebin/view/2780 



  • [quote user="kaamoss"]

    Hi, I've been working on a internal quality assurance application as of late. I'm using a mysql database, as well as a swing gui client app. Also have a custom XMLmessenger app on the database server. Anyways, this is my first time doing something like this and it feels like it's grown out of my control. I'm not really sure how I should be designing this. Anyways, if you'd like, feel free to look at my code and shout wtf. 🙂

     

     This is a sample of three of the many backend classes I've written

    http://papernapkin.org/pastebin/view/2780 

    [/quote]

     

    First, get a list of requirements.

    Second, look for an existing solution elsewhere.

    If no solution exists, determine what can be done manually and what must be automated.

    Develope the solution thusly:

    a. Pick a sufficiantly small feature to implement.
    b. Write a unit test (a test that reports either success of failure) that will fail until the feature is implemented.
    c. Run the test. It should fail, or go back to b.
    d. Implement the feature as quickly as possible. Don't worry about design yet.
    e. Run the test. It should pass, or go back to d.
    f. Refactor your working code into a better design. Using small steps. For every step, run the all the tests to ensure that you haven't broken anything.
    g. Start with a new feature at a.

    This works, believe it or not.  Its called Test Driven Development. Good knowledge of patterns and refactorying a plus.  I suggest Refactoring (Fowler) as a place to start.  Search for TDD on google for more information.

     



  • Thanks for the reply. I'm relativly framiliar with using junit, and have a few tests for most of my packages. I was looking into using something like dbUnit but it seemed overwhelming at the time. Perhaps I should take a look again. Do you know of any java apis that you've used to unit testing database code?



  • This is a really late reply, i know that ;), I just wanted to share my thoughts on db-driven applications using java...

     I really recommend that you try hiberhate (www.hibernate.org). It gives you an abstraction layer (yay :)) so that you can swap databases easily and aggregate fetched data into object. It's very object orientated and has a query language very similar to SQL (you can also use SQL queries directly if you need to). It lets you write queries like "select p from my.company.Person p where p.sibling.name = "Foobar"".

    However, Hibernate configuration will introduce you to xml hell if you're not using java 1.5 (in which case you can use annotations, a lot nicer in my opinion).

     The new EJB 3.0/JEE spec is really good aswell, it gives you concurrency support, storage mechanisms (very similar to hibernate), authorization/authentication and a lot of other stuff that is really useful! If you have time, this is what I would recommend learning.

     If you like UML and would like to draw large parts of your application instead of writing it (more fun! 🙂 ), try androMDA (www.androMDA.org). It's a really cool tool that genereates all the boilerplate code for you (from an UML diagram) and also makes it a lot easier (not trivial though) to switch target platforms (currently .NET/Java). AndroMDA also supports the XP design paradigm, so you can generate, write some business logic code, test, alter the model, regenereate etc. No fuzz, no magic, it just works. Really cool. Try it out if you have time on your hands. It's tricky to get started, but it gets better.

     If you feel like the EJB approach is a bit heavyweight, you could try spring (www.springframework.com). It's a bunch of classes helping you with loads of stuff, and it's really useful for most applications in one way or another. It eases JDBC development by simplifying a lot of stuff, and can also help you with RPC, creation and wiring of your objects and lots of other things. In fact, if you choose not to use EJB 3.0, spring can also help you get rid of configuration hell with Hibernate. It's really good in the way that it lets you forget about the hard stuff (getting the connection, connecting, catching exceptions, flushing etc) and lets you focus on the really important code (the queries etc). More correctly, it promotes you to separate these two issues in a clean way.

    There are tons of utilities and frameworks out there, and I don't know how good you are, but my favourite tool collection is jakarta commons (http://jakarta.apache.org/commons/). There you'll find a huge collection of really useful utilities that saves hours of coding. Can't live without it 🙂

    Hope this helps!

     

    Regards
    Alex 

     

     

     

     


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.