L
Lew
Are said:I see your point. The idea is to isolate what has got to do with the
actual initialization and maintainance of the server connection. Only
this class needs to know about JDBC driver and such. That way it can
also be reused.
Alright let me revise. I guess I wrote this in a hurry.
(Error catching left out)
public class DatabaseConnector {
private String serverURL = "...";
If you're going to hard-code these values, make them static final Strings (and
therefore use all upper case in their names).
private String driverName = "...";
private Connection con;
private PreparedStatement stmt1 = new PreparedStatement("...");
//...
public DatabaseConnector(user, pass) {
Class.forName(driverName);
con = DriverManager.getConnection(serverURL, user, pass);
}
You don't need to load the driver class but once, not every time you make an
object. You could do it in a static initializer for the class (assuming you
followed my advice to make the driver name a static variable, too).