#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    61
    Rep Power
    15

    Java servlet - abstracting database access logic


    I have some experience with PHP, and I thought I'd try experimenting with Java servlets. However, one thing is confusing me: how do I abstract the database logic to ensure there is no mySQL (for example) code in my classes? Are there any good tutorials for learning about this? My biggest flaw when coding PHP was to add mySQL code into my objects, which in hindsight wasn't a great idea.

    Thanks
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    I do not know of a good tutorial, but what I do is make a broker object. This object is responsible for connecting to the database. I also use a properties file the first time it is accessed to read in the database connection string, username and password it should use. I don't have an example with me or I would post it. If I remember when I get to work I can post it.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    12
    Rep Power
    0
    I use Jakarta Struts, which is a great framework for separating presentation, logic and data models. There is a learning curve getting up to Struts, but it's worth it if you are going to build apps in java.

    If you'd rather not take on struts, you can use the "command pattern." Basically, create a class to access the database and perform logic. Create an instance of it from your servlet and pass it the arguments you need. Then "execute" it, returning the data. I know this is an over simplification, but I hope it points you in the right direction.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    61
    Rep Power
    15
    Great - thanks for the replies. I'll look into the command pattern I think.

    Cheers

IMN logo majestic logo threadwatch logo seochat tools logo