Connecting Using the DriverManager and a Database URL

From Apache OpenOffice Wiki
< Documentation‎ | DevGuide
Revision as of 12:37, 15 February 2008 by Ccornell (Talk | contribs)

Jump to: navigation, search



The database context and establishing connections to a database even if there is no data source for it in OpenOffice.org API can be avoided.

To create a connection ask the driver manager for it. The com.sun.star.sdbc.DriverManager manages database drivers. The methods of its interface com.sun.star.sdbc.XDriverManager are used to connect to a database using a database URL:

 // establish connection
 com::sun::star::sdbc::XConnection getConnection( [in] string url)
 com::sun::star::sdbc::XConnection getConnectionWithInfo( [in] string url, 
         [in] sequence < com::sun::star::beans::PropertyValue > info)
 
 // timeout for connection failure
 void setLoginTimeout( [in] long seconds)
 long getLoginTimeout()

Additionally, the driver manager enumerates all available drivers, and is used to register and deregister drivers. A URL that identifies a driver and contains information about the database to connect to must be known. The DriverManager chooses the first registered driver that accepts this URL. The following line of code illustrates it generally:

 Connection xConnection = DriverManager.getConnection(url);

The structure of the URL consists of a protocol name, followed by the driver specific sub-protocol. The data source administration dialog shows the latest supported protocols. Some protocols are platform dependent. For example, ADO is only supported on Windows.

The URLs and conditions for the various drivers are explained in section Driver Specifics below.

Frequently a connection needs additional information, such as a user name, password or character set. Use the method getConnectionWithInfo() to provide this information. The method getConnectionWithInfo() takes a sequence of com.sun.star.beans.PropertyValue structs. Usually user and password are supported. For other connection info properties, refer to the section Driver Specifics.

 // create the DriverManager
 Object driverManager = xMultiServiceFactory.createInstance("com.sun.star.sdbc.DriverManager");
 
 // query for the interface XDriverManager
 com.sun.star.sdbc.XDriverManager xDriverManager;
 
 xDriverManager = (XDriverManager)UnoRuntime.queryInterface(
     XDriverManager.class, driverManager);
 
 if (xDriverManager != null) {
     // first create the database URL
     String adabasURL = "sdbc:adabas::MYDB0";
   
     // create the necessary sequence of PropertyValue structs for user and password
     com.sun.star.beans.PropertyValue [] adabasProps = new com.sun.star.beans.PropertyValue[] { 
         new com.sun.star.beans.PropertyValue("user", 0, "Scott",
             com.sun.star.beans.PropertyState.DIRECT_VALUE),
         new com.sun.star.beans.PropertyValue("password", 0, "huutsch",
             com.sun.star.beans.PropertyState.DIRECT_VALUE)
         };
 
     // now create a connection to Adabas
     XConnection xConnection = xDriverManager.getConnectionWithInfo(adabasURL, adabasProps);
     
     if (adabasConnection != null) {
         System.out.println("Connection was created!");
     
         // now we dispose the connection to close it
         XComponent xComponent = (XComponent)UnoRuntime.queryInterface(
             XComponent.class, xConnection );
     
         if (xComponent != null) {
             // connection must be disposed to avoid memory leaks
             xComponent.dispose();
             System.out.println("Connection disposed!");
         }
     } else {
         System.out.println("Connection could not be created!");
     }
 }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools