This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes extending java.sql.Connection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
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 ]
It is sorta covered in the JavaRanch Style Guide.
subject: extending java.sql.Connection?
Similar Threads
Clean-up objects in session.
Database Connection and Session
Question on Best Practice for Database Connections
Serializing a Connection.
Connection Design