Database helper class with PreparedStatements

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).
 
A

Are Nybakk

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 = "...";
private String driverName = "...";
private Connection con;
private PreparedStatement stmt1 = new PreparedStatement("...");
//...

public DatabaseConnector(user, pass) {
Class.forName(driverName);
con = DriverManager.getConnection(serverURL, user, pass);
}

public void insertSomeObject(SomeObject obj) {
stmt.setXxx(obj.getZzz);
//...

stmt.executeWww();
}

Commenting myself again =)
This makes it hard to reuse the class. It would be better to have a
getPreparedStatement and let DatabaseHandler take care of the actual
queries.
 
A

Are Nybakk

Lew said:
If you're going to hard-code these values, make them static final
Strings (and therefore use all upper case in their names).


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).

Hmm, good idea.
 
A

Are Nybakk

Arne said:
What does that class provide that Connection does not ?

Let me quote from a thread in comp.lang.java.databases:


out of connections because your objects never let them go?

David said:
> Better *not* to let each object create its own connection. That way,
madness lies. As you say, this argues strongly for a connection pool.

The application code doesn't change when you switch to a connection
pool. The semantics of the close() call change to "release to the pool"
instead of "destroy"."


Now I better understand why I've learned to do it this way. This wrapper
class is a connection pool of sorts. One with only one connection.

The pool could contain more connections if needed (several databases
perhaps) and a time-out for closing unused, open connections. It should
also have a way of "locking" the connection.
That DatabaseConnector is not needed is emphasized by the fact
that you have both the DatabaseConnector wrapper and the underlying
Connection as fields here.

Furthermore you should really open and close connection in the
insert method. If you keep the DatabaseHandler around then you
collect way to many database connections. If you construct and
close DatabaseHandler for every insert call, then you could
just as well do everything in the insert method.

Then rather, get a reference to a connection from the pool and "free" it
afterwards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top