how to (extend) a java.sql.Connection object

W

Wiseguy

ok. so I know that I cannot (extend) an interface but I got your
attention.

Consider the following:

public class DBconnection {

public Connection conn;
public boolean error=false;


public DBconnection() {
String URL=new String("jdbc:postgresql://localhost/mydatabase");
String UNAME=new String("me");
String PASSWD=new String("");
String DRIVER=new String("org.postgresql.Driver");
try {URL=new String(System.getProperty("URL"));}
catch (Exception e) {}
try {UNAME=new String(System.getProperty("UNAME"));}
catch (Exception e) {}
try {PASSWD=new String(System.getProperty("PASSWD"));}
catch (Exception e) {}
try {DRIVER=new String(System.getProperty("jdbc.drivers"));}
catch (Exception e) {}
try {
Class.forName(DRIVER);
// load the driver classes
conn=DriverManager.getConnection(URL,UNAME,PASSWD);
// get a connection
}
catch (Exception e) {
System.out.println("-- Cannot access DB --");
System.out.println("jdbc.drivers='"+DRIVER+"'");
System.out.println("URL='"+URL+"'");
System.out.println("UNAME='"+UNAME+"'");
System.out.println("PASSWD='"+PASSWD+"'");
System.out.println("Exception is:"+e.toString());
System.out.println("----------------------");
error=true;
}
}


Having to reference the (conn) member to get a connection is a needless
step. I'd like the above class to implement the Connection interface for
the postgres driver so that the DBConnection object can be used anywhere
that a normal Connection is used.

Here's the booger though. Completely implementing the Connection
interface in the DBconnection class is a hell-of-a-lot of coding. Isn't
there a quicker way to make the DBConnection contain the functionality of
Connection without actually implementing it?
 
F

Filip Larsen

Completely implementing the Connection
interface in the DBconnection class is a hell-of-a-lot of coding. Isn't
there a quicker way to make the DBConnection contain the functionality of
Connection without actually implementing it?

I think you should consider what service you want your DBConnection
class to provide. As you presented it, it doesn't do much other that
getting a new connection, something that a simple factory method could
do instead, or better, one of the Connection pool implementations out
there. Perhaps you can use something like
http://sourceforge.net/projects/proxool instead. I haven't tried that
particular one, but it seems to fit your purpose.

If you really want to implement a Connection by delegation there is not
much you can do that are easier than writing a lot of delegation code.
Delegation code is boring to write, but it is pretty straightforward to
do. If you dont want to write that code, you could use either a code
generator like what is build in to some Java IDEs (for instance, it took
me 20 sec to make a delegating Connection in Eclipse), or you can use
java.reflect.Proxy to create a proxy for a Connection (like what that
Proxool tool do).


Regards,
 

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,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top