• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

extending java.sql.Connection?  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!