Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "DTP FAQ"

Line 31: Line 31:
  
 
   public java.sql.Connection getJavaConnectionForProfile (IConnectionProfile profile) {
 
   public java.sql.Connection getJavaConnectionForProfile (IConnectionProfile profile) {
IManagedConnection managedConnection = ((IConnectionProfile)profile).
+
      IManagedConnection managedConnection = ((IConnectionProfile)profile).
  getManagedConnection ("java.sql.Connection");
+
        getManagedConnection ("java.sql.Connection");
if (managedConnection != null) {
+
      if (managedConnection != null) {
  return (java.sql.Connection) managedConnection.getConnection().getRawConnection();
+
        return (java.sql.Connection) managedConnection.getConnection().getRawConnection();
}
+
      }
return null;
+
      return null;
 
   }
 
   }
  

Revision as of 17:43, 2 October 2009

Back to DTP Main Page


This page should be a repository for all those "Frequently Asked Questions" or FAQs posted on the DTP newsgroups, mailing lists, and whatnot.

Connectivity

Q: How do I programmatically access an existing connection profile?

A: Access to existing connection profiles (persisted or transient) is done using the ProfileManager class (org.eclipse.datatools.connectivity.ProfileManager). So this is pretty simple:

  IConnectionProfile profile = ProfileManager.getInstance().getProfileByName("myprofile");

Q: How do I connect to an existing connection profile programmatically?

A: Again, pretty simple:

  IConnectionProfile profile = ProfileManager.getInstance().getProfileByName("myprofile");
  IStatus status = profile.connect();
  if (status.getCode() == IStatus.OK)) {
     // success
  } else {
     // failure :(
     if (status.getException() != null) {
        status.getException().printStackTrace();
     }
  } 

Q: How do I get the raw JDBC Connection from my connected connection profile?

A: The ProfileConnectionManager class provides a method for retrieving a particular type of connection class from a connected profile. A "java.sql.Connection" is one of the two different types (the other being the ConnectionInfo class) you can retrieve for connected database connection profiles.

  public java.sql.Connection getJavaConnectionForProfile (IConnectionProfile profile) {
     IManagedConnection managedConnection = ((IConnectionProfile)profile).
        getManagedConnection ("java.sql.Connection");
     if (managedConnection != null) {
        return (java.sql.Connection) managedConnection.getConnection().getRawConnection();
     }
     return null;
  }

Q: How do I get at the database model from my connected connection profile?

A: This is a little more difficult, but along the same lines of getting a JDBC connection. We just have a different connection type under the covers that lumps a reference to the Database model object with some other bits and pieces about the connection called "ConnectionInfo". Once you get the Database object you can use it like any other EMF model object - looking at its properties, child objects, and so on. Generally it's modeled the same way as JDBC is. From the Database object you can get a list of catalogs or schemas. From a schema you can get a list of tables, procedures, and so on.

  public org.eclipse.datatools.modelbase.sql.schema.Database (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) {
              Database database = connectionInfo.getSharedDatabase();
              return database;
           }
        } catch (Exception e) {
           e.printStackTrace();
        }
     }
     return null;
  }

Back to the top