• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database brand from connection

 
Jon Strayer
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to tell what brand of database you are connection to given only a connection?
I have some code that depends on value of the errorCode returned by an SQLException. I'd like to make it a bit more robust by testing for the brand of db and throwing an exception if it changes (and the code is now suspect).
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you have to know what kind of database you are connecting to in order to build the Connection object to begin with?
Maybe I am not understanding your question.
 
Jon Strayer
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, not really. I've got a factory class that I ask for connections. It needs to know (actually, all the data is in a property file). But the code that needs to know what database it's connected to has connections passed in to it. It's only because of limitations in the JDBC api that I need to know the database, so I don't want to pass that data all around the system.
But someone in a mailing list pointed me to Connection.getMetaData().getDatabaseProductName()
That will do the job for me.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good deal. That was my next suggestion. Glad you figured it out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic