Data Sources in Apache OpenOffice

From Apache OpenOffice Wiki
Jump to: navigation, search
  • Data Sources in Apache OpenOffice API



DatabaseContext

In the Apache OpenOffice graphical user interface (GUI), define OpenOffice database files using the database application Apache OpenOffice Base, and register them in the Tools → Options → OpenOffice Base → Databases dialog in order to access them in the database browser. A data source has five main aspects. It contains the following:

  • The general information necessary to connect to a data source.
  • Settings to control the presentation of tables, and queries.
  • SQL query definitions.
  • Database forms.
  • Database reports.

From the API perspective, these functions are mirrored in the com.sun.star.sdb.DatabaseContext service. The database context is a container for data sources. It is a singleton, that is, it may exist only once in a running Apache OpenOffice API instance and can be accessed by creating it at the global service manager of the office.

The Dialog "Database Registration"

The database context is the entry point for applications that need to connect to a data source already defined in the Apache OpenOffice API. Additionally, it is used to create new data sources and add them to Apache OpenOffice API. The following figure shows the relationship between the database context, the data sources and the connection over a data source.

The database context is used to get a data source that provides a com.sun.star.sdb.Connection through its com.sun.star.sdb.XCompletedConnection interface.

Existing data sources are obtained from the database context at its interfaces com.sun.star.container.XNameAccess and com.sun.star.container.XEnumeration. Their methods getByName() and createEnumeration() deliver the com.sun.star.sdb.DataSource services defined in the Apache OpenOffice GUI.

Since OpenOffice.org 2.0.0, getByName() can also be used to obtain data sources that are not registered. You only need to pass a URL pointing to a valid database file, which is then automatically loaded by the context.

The code below shows how to print all available registered data sources:

  // prints all data sources
  public static void printDataSources(XMultiServiceFactory _rMSF) throws com.sun.star.uno.Exception {
      // retrieve the DatabaseContext and get its com.sun.star.container.XNameAccess interface
      XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(
          XNameAccess.class, _rMSF.createInstance("com.sun.star.sdb.DatabaseContext"));
 
      // print all DataSource names
      String aNames [] = xNameAccess.getElementNames();
      for (int i=0;i<aNames.length;++i)
          System.out.println(aNames[i]);
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages