Difference between revisions of "DTP FAQ"

From Eclipsepedia

Jump to: navigation, search
Line 17: Line 17:
 
   IConnectionProfile profile = ProfileManager.getInstance().getProfileByName("myprofile");
 
   IConnectionProfile profile = ProfileManager.getInstance().getProfileByName("myprofile");
 
   IStatus status = profile.connect();
 
   IStatus status = profile.connect();
   if (status.equals(IStatus.OK)) {
+
   if (status.getCode() == IStatus.OK)) {
 
       // success
 
       // success
 
   } else {
 
   } else {
Line 31: Line 31:
  
 
   public java.sql.Connection getJavaConnectionForProfile (IConnectionProfile profile) {
 
   public java.sql.Connection getJavaConnectionForProfile (IConnectionProfile profile) {
      IConnection connection = ProfileConnectionManager.getProfileConnectionManagerInstance().getConnection(profile,"java.sql.Connection");
+
IManagedConnection managedConnection = ((IConnectionProfile)profile).
      if (connection != null) {
+
  getManagedConnection ("java.sql.Connection");
        return (java.sql.Connection) connection.getRawConnection();
+
if (managedConnection != null) {
      }
+
  return (java.sql.Connection) managedConnection.getConnection().getRawConnection();
      return null;
+
}
   }  
+
return null;
 +
   }
  
 
'''Q: How do I get at the database model from my connected connection profile?'''
 
'''Q: How do I get at the database model from my connected connection profile?'''

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;
  }