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.
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.
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.
Great - thanks for the replies. I'll look into the command pattern I think.