Difference between revisions of "Documentation/DevGuide/Database/Connecting Using the DriverManager and a Database URL"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (1 revision(s))
m
Line 11: Line 11:
  
 
To create a connection ask the driver manager for it. The <idl>com.sun.star.sdbc.DriverManager</idl> manages database drivers. The methods of its interface <idl>com.sun.star.sdbc.XDriverManager</idl> are used to connect to a database using a database URL:
 
To create a connection ask the driver manager for it. The <idl>com.sun.star.sdbc.DriverManager</idl> manages database drivers. The methods of its interface <idl>com.sun.star.sdbc.XDriverManager</idl> are used to connect to a database using a database URL:
 
+
<source lang="idl">
 
   // establish connection
 
   // establish connection
 
   com::sun::star::sdbc::XConnection getConnection( [in] string url)
 
   com::sun::star::sdbc::XConnection getConnection( [in] string url)
Line 20: Line 20:
 
   void setLoginTimeout( [in] long seconds)
 
   void setLoginTimeout( [in] long seconds)
 
   long getLoginTimeout()
 
   long getLoginTimeout()
 
+
</source>
 
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:
 
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:
 
+
<source lang="java">
 
   Connection xConnection = DriverManager.getConnection(url);
 
   Connection xConnection = DriverManager.getConnection(url);
 
+
</source>
 
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 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.  
  
Line 31: Line 31:
 
Frequently a connection needs additional information, such as a user name, password or character set. Use the method <code>getConnectionWithInfo()</code> to provide this information. The method <code>getConnectionWithInfo()</code> takes a sequence of <idl>com.sun.star.beans.PropertyValue</idl> structs. Usually user and password are supported. For other connection info properties, refer to the section [[Documentation/DevGuide/Database/Driver Specifics|Driver Specifics]].  
 
Frequently a connection needs additional information, such as a user name, password or character set. Use the method <code>getConnectionWithInfo()</code> to provide this information. The method <code>getConnectionWithInfo()</code> takes a sequence of <idl>com.sun.star.beans.PropertyValue</idl> structs. Usually user and password are supported. For other connection info properties, refer to the section [[Documentation/DevGuide/Database/Driver Specifics|Driver Specifics]].  
 
<!--[SOURCE:Database/CodeSamples.java]-->
 
<!--[SOURCE:Database/CodeSamples.java]-->
 
+
<source lang="java">
 
   // create the DriverManager
 
   // create the DriverManager
 
   Object driverManager = xMultiServiceFactory.createInstance("com.sun.star.sdbc.DriverManager");
 
   Object driverManager = xMultiServiceFactory.createInstance("com.sun.star.sdbc.DriverManager");
Line 72: Line 72:
 
       }
 
       }
 
   }
 
   }
 
+
</source>
 
{{PDL1}}
 
{{PDL1}}
 
[[Category: Database Access]]
 
[[Category: Database Access]]

Revision as of 14:44, 31 May 2008



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