Marsoni Hutao wrote:
J. Kevin Robbins wrote:You are off to a good start. Your code looks well organized. There is room for improvement, especially in your DBUtility class, but we can talk about that later if you wish.
Yes Please, I would love to know the flaws/improvement suggestions.
It's good that you've created a separate class to do the database connection. Many people put that code in the
servlet where it doesn't belong. But using DriverManager is inefficient for a multi-user app. I sometimes use it for utility classes that run outside the web environment (cron jobs and so on), but for your web app you need to have a pool of connections that can be re-used.
Here is a document on using pooling with SQLServer. Actually setting up the pool depends on what web server you are running. If you are the server admin and need guidance on setting up the pool, we have other forums where you can get help with the specifics.
But for a moment, let's assume that your server is already setup with a connection pool and you just want to use it. I found an old, but good faq
here. Another one from Oracle can be found
here.
In short, DriverManager creates and then you must destroy the connection with every use. Creating database connections involves a lot of overhead for the server.
Using a pool, the connections are created at server startup and your DAO just grabs one, uses it, and then returns it to the pool for other objects to use. It's far more efficient.
[edit] One more thing that jumps out at me. Instead of using System.err, learn to use logging. I just use the logging included with
Java. Many people like log4j but I've never been able to make it behave the way I want. I find java.util.logging to be much simpler to use.