File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes extending java.sql.Connection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "extending java.sql.Connection?" Watch "extending java.sql.Connection?" New topic

extending java.sql.Connection?

Rolf Mueller

Joined: Mar 15, 2004
Posts: 9
Hello everyone,
I know that the db-driver (mySQLjdbc.jar e.g.) implements the connection interface. I would like to add new functions to a connection, such as retrieving unique keys. How do I do that?
Thanks in advance,
Rolf Mueller
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
I'm not sure this sort of thing is possible. The connections are returned from the drivers provided by the database vendor. So, even though you can implement your own connection or somehow extend the connection returned by your driver's implementation, there's no way to force your driver to use the newly improved connection.
You would basically have to hack all the little pieces of your driver to do the whole thing (using the connection and everything else). Of course, at that point, you wouldn't be using a JDBC-compliant driver, which would bring more misery to your life.
On a plus side, JDBC3 lets you retrieve generated primary keys. In earlier versions (I'm assuming you're using mySQL), the mySql driver let you cast something so you could retrieve the generated primary key for inserts. My point: make sure you thoroughly research what you need and other ways to get what you want. Just because you can develop yourself some solution doesn't mean there aren't other, much-easier ways of doing the same thing.

Rolf Mueller

Joined: Mar 15, 2004
Posts: 9
Sorry for posting this without having a morning cafe
Now I found an easy solution, which seems to work. Looks like that:

I use this class in my connection-pool, and can use the added functionality in my persistence-controler.
[ March 22, 2004: Message edited by: Rolf Mueller ]
I agree. Here's the link:
subject: extending java.sql.Connection?
jQuery in Action, 3rd edition