Tuesday, July 22, 2008

Ever wonder how to get a Database from a Connection Profile?

Hi all!

Have you ever wondered how to get a DTP SQL Model Database object from a connected connection profile? I seem to run into this problem infrequently, but always have to go through many gyrations to find the answer.

Larry from IBM, another member of the Connectivity team, was kind enough to provide the answer. It resulted in this chunk of code:

public Database getDatabaseForProfile (IConnectionProfile profile) {
IManagedConnection managedConnection = ((IConnectionProfile)profile).
getManagedConnection("org.eclipse.datatools.connectivity.sqm.core.connection.ConnectionInfo");

if (managedConnection != null) {
try {
ConnectionInfo connectionInfo = (ConnectionInfo) managedConnection.
getConnection().getRawConnection();
if (connectionInfo != null) {
return connectionInfo.getSharedDatabase();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}

(Sorry the code's not pretty. I haven't found a great way of including code in Blogger blog posts yet.)

Basically under the covers there is a ConnectionInfo connection adapter that is used to map between the java.sql.Connection object we get from JDBC and the SQL model that's populated via the catalog loaders.

So there you have it! Not that many people have a need for such a thing, but it's handy just in case!

--Fitz

3 comments:

Anonymous said...

Fitz,

Try

http://formatmysourcecode.blogspot.com/

Works great for formatting blogger source code.

scott r

nickb said...

Apparently CodeSnippet for Eclipse is cool too. (Haven't tried it yet.)

Brian Fitzpatrick (aka "Fitz) said...

I've tried the CodeSnippet one and I can't get it to run on 3.3.2 or 3.4 (I'm guessing I'm missing some dependencies somehow). But I'll give the blogspot site a shot. Thanks guys!