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 38: Line 38:
 
'''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?'''
  
'''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".
+
'''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.  
  
   IManagedConnection managedConnection = ((IConnectionProfile)profile).getManagedConnection("org.eclipse.datatools.connectivity.sqm.core.connection.ConnectionInfo");
+
   public org.eclipse.datatools.modelbase.sql.schema.Database (IConnectionProfile profile) {
  if (managedConnection != null) {
+
      IManagedConnection managedConnection = ((IConnectionProfile)profile).getManagedConnection("org.eclipse.datatools.connectivity.sqm.core.connection.ConnectionInfo");
      try {
+
      if (managedConnection != null) {
        ConnectionInfo connectionInfo = (ConnectionInfo) managedConnection.getConnection().getRawConnection();
+
        try {
        if (connectionInfo != null) {
+
            ConnectionInfo connectionInfo = (ConnectionInfo) managedConnection.getConnection().getRawConnection();
            Database database = connectionInfo.getSharedDatabase();
+
            if (connectionInfo != null) {
            // do something with the database reference… this is an EMF object, so you can query for any underlying properties of the object such as
+
              Database database = connectionInfo.getSharedDatabase();
             // child Catalogs, Schemas, Tables, etc... Just work your way down the hierarchy
+
              return database;
 +
             }
 +
        } catch (Exception e) {
 +
            e.printStackTrace();
 
         }
 
         }
      } catch (Exception e) {
 
        e.printStackTrace();
 
 
       }
 
       }
 +
      return null;
 
   }
 
   }
  
 
[[Category:Data_Tools_Platform]]
 
[[Category:Data_Tools_Platform]]

Revision as of 12:55, 1 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: 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.equals(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?

  public java.sql.Connection getJavaConnectionForProfile (IConnectionProfile profile) {
     IConnection connection = ProfileConnectionManager.getProfileConnectionManagerInstance().getConnection(profile,"java.sql.Connection");
     if (connection != null) {
        return (java.sql.Connection) connection.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