Maciej Olborski wrote:I am implementing DAO completely by myself because its an assigment and we've got to show off our code.
Fair enough!
Let me give you a few other tips/hints/suggestions:
you should definitely use automatic resource management (try-with-resources), because your current code could have resource leaks. Only possible if you are using Java 7 or later (mentioned by Dave Tolls as well)use of prepared statements is really great
the conn instance variable must be private (encapsulation)in authenticate: don't use Boolean, use boolean insteadgetConnection and closeConnection should definitely not be part of the public API of UserDAO
If you really want to show off a really good class design, then
getConnection and
closeConnection should not even be part of the
UserDAOImpl class. Why not? Because each class (and even each method) should have 1 specific purpose, the more specific this purpose, the better (in OO terms, that's called "high cohesion"). So your UserDAO should be responsible for the user management (reading, creating, updating, deleting,...) but not for connection management (creating, opening, closing,...). That's the responsibility of a connection factory class (or even better a connection pool).
I could share all the required code, but it's your assignment so you have to do the coding

I'll just give some pointers:
1/ create a connection factory class
make it a singletonin (private) constructor (it's a singleton) you already load the (mysql) drivercreate a (private) createConnection method which gets a connection using url, user and passwordcreate a (public) static getConnection method which invokes the createConnection method (on the single instance of this class)One of the major benefits of introducing this class: you can reuse this class for other DAOs as well (code reuse).
2/ rework your UserDAOImpl class
If you have finished the 1st step, you have to redo every method of this class using this connection factory class and in the end you'll be able to remove the
conn instance variable and all connection-related methods from this class. Yay! If you combine with using the Java 7 try-with-resources you'll get very clean code. Your code will end up looking like this:
It will be slightly more difficult using a PreparedStatement, because you have to set its parameters (which can't be done in the try-with-resources block). But you could solve this using a private method. For example:
And then you can call this method from the try-with-resources block.
3/ rework your
main method
After you have finished the 2nd step, you'll have to fix a few compiler errors in this code

And one other important OO rule: you should always program to an interface (or an abstract class). So you should write
UserDAO userDAO = new UserDAOImpl(); instead.
If you would encounter any problems while implementing any of these improvements, just let us now and we are happy to help you!
Hope it helps!
Kind regards,
Roel