Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2004

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Wiseguy - 07 Feb 2004 05:19 GMT
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?
Filip Larsen - 07 Feb 2004 11:17 GMT
> 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,
Signature

Filip Larsen



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.