Difference between revisions of "Documentation/DevGuide/Database/Connecting Through a DataSource"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
 
(3 intermediate revisions by one other user not shown)
Line 6: Line 6:
 
|NextPage=Documentation/DevGuide/Database/Connecting Using the DriverManager and a Database URL
 
|NextPage=Documentation/DevGuide/Database/Connecting Using the DriverManager and a Database URL
 
}}
 
}}
{{DISPLAYTITLE:Connecting Through a DataSource}}
+
{{Documentation/DevGuideLanguages|Documentation/DevGuide/Database/{{SUBPAGENAME}}}}
 +
{{DISPLAYTITLE:Connecting Through a DataSource}}
 
<!--<idltopic>com.sun.star.sdbc.XDataSource</idltopic>-->
 
<!--<idltopic>com.sun.star.sdbc.XDataSource</idltopic>-->
Data sources in the database context of {{PRODUCTNAME}} API offer two methods to establish a connection, a non-interactive and an interactive procedure. Use the <idl>com.sun.star.sdbc.XDataSource</idl> interface to connect. It consists of:
+
Data sources in the database context of {{AOo}} API offer two methods to establish a connection, a non-interactive and an interactive procedure. Use the <idl>com.sun.star.sdbc.XDataSource</idl> interface to connect. It consists of:
<source lang="idl">
+
<syntaxhighlight lang="idl">
 
   // establish connection
 
   // establish connection
 
   com::sun::star::sdbc::XConnection getConnection(  
 
   com::sun::star::sdbc::XConnection getConnection(  
Line 17: Line 18:
 
   void setLoginTimeout( [in] long seconds)
 
   void setLoginTimeout( [in] long seconds)
 
   long getLoginTimeout()
 
   long getLoginTimeout()
</source>
+
</syntaxhighlight>
 
If a database does not support logins, pass empty strings to <code>getConnection()</code>. For instance, use <code>getConnection()</code> against dBase data sources like Bibliography:
 
If a database does not support logins, pass empty strings to <code>getConnection()</code>. For instance, use <code>getConnection()</code> against dBase data sources like Bibliography:
<source lang="java">
+
<syntaxhighlight lang="java">
 
   XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(
 
   XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(
 
       XNameAccess.class, _rMSF.createInstance("com.sun.star.sdb.DatabaseContext"));
 
       XNameAccess.class, _rMSF.createInstance("com.sun.star.sdb.DatabaseContext"));
Line 29: Line 30:
 
   // simple way to connect
 
   // simple way to connect
 
   XConnection xConnection = xDS.getConnection("", "");
 
   XConnection xConnection = xDS.getConnection("", "");
</source>
+
</syntaxhighlight>
However if the database expects a login procedure, hard code the user and password, although this is not advisable. Data sources support an advanced login concept. Their interface <idl>com.sun.star.sdb.XCompletedConnection</idl> starts an interactive login, if necessary:
+
However, if the database expects a login procedure, hard code the user and password, although this is not advisable. Data sources support an advanced login concept. Their interface <idl>com.sun.star.sdb.XCompletedConnection</idl> starts an interactive login, if necessary:
<source lang="idl">
+
<syntaxhighlight lang="idl">
 
   com::sun::star::sdbc::XConnection connectWithCompletion(
 
   com::sun::star::sdbc::XConnection connectWithCompletion(
 
                   [in] com::sun::star::task::XInteractionHandler handler)
 
                   [in] com::sun::star::task::XInteractionHandler handler)
</source>
+
</syntaxhighlight>
When you call <code>connectWithCompletion()</code>, {{PRODUCTNAME}} API shows the common login dialog to the user if the data source property <code>IsPasswordRequired</code> is true. The login dialog is part of the <idl>com.sun.star.sdb.InteractionHandler</idl> provided by the global service factory.
+
When you call <code>connectWithCompletion()</code>, {{AOo}} API shows the common login dialog to the user if the data source property <code>IsPasswordRequired</code> is true. The login dialog is part of the <idl>com.sun.star.sdb.InteractionHandler</idl> provided by the global service factory.
<source lang="java">
+
<syntaxhighlight lang="java">
 
   // logs into a database and returns a connection
 
   // logs into a database and returns a connection
 
   // expects a reference to the global service manager
 
   // expects a reference to the global service manager
Line 62: Line 63:
 
       return XConnection;
 
       return XConnection;
 
   }
 
   }
</source>
+
</syntaxhighlight>
 
{{PDL1}}
 
{{PDL1}}
[[Category: Database Access]]
+
 
 +
[[Category:Documentation/Developer's Guide/Database Access]]

Latest revision as of 14:01, 21 December 2020



Data sources in the database context of Apache OpenOffice API offer two methods to establish a connection, a non-interactive and an interactive procedure. Use the com.sun.star.sdbc.XDataSource interface to connect. It consists of:

  // establish connection
  com::sun::star::sdbc::XConnection getConnection( 
                  [in] string user, [in] string password)
 
  // timeout for connection failure
  void setLoginTimeout( [in] long seconds)
  long getLoginTimeout()

If a database does not support logins, pass empty strings to getConnection(). For instance, use getConnection() against dBase data sources like Bibliography:

  XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(
      XNameAccess.class, _rMSF.createInstance("com.sun.star.sdb.DatabaseContext"));
 
  // we use the Bibliography data source
  XDataSource xDS = (XDataSource)UnoRuntime.queryInterface(
      XDataSource.class, xNameAccess.getByName("Bibliography"));
 
  // simple way to connect
  XConnection xConnection = xDS.getConnection("", "");

However, if the database expects a login procedure, hard code the user and password, although this is not advisable. Data sources support an advanced login concept. Their interface com.sun.star.sdb.XCompletedConnection starts an interactive login, if necessary:

  com::sun::star::sdbc::XConnection connectWithCompletion(
                  [in] com::sun::star::task::XInteractionHandler handler)

When you call connectWithCompletion(), Apache OpenOffice API shows the common login dialog to the user if the data source property IsPasswordRequired is true. The login dialog is part of the com.sun.star.sdb.InteractionHandler provided by the global service factory.

  // logs into a database and returns a connection
  // expects a reference to the global service manager
  com.sun.star.sdbc.XConnection logon(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"));
 
      // get an Adabas D data source Ada01 generated in the GUI
      Object dataSource = xNameAccess.getByName("Ada01");
 
      // create a com.sun.star.sdb.InteractionHandler and get its XInteractionHandler interface
      Object interactionHandler = _rMSF.createInstance("com.sun.star.sdb.InteractionHandler");
      XInteractionHandler xInteractionHandler = (XInteractionHandler)UnoRuntime.queryInterface(
          XInteractionHandler.class, interactionHandler);
 
      // query for the XCompletedConnection interface of the data source
      XCompletedConnection xCompletedConnection = (XCompletedConnection)UnoRuntime.queryInterface(
          XCompletedConnection.class, dataSource);
 
      // connect with interactive login
      XConnection xConnection = xCompletedConnection.connectWithCompletion(xInteractionHandler);
 
      return XConnection;
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages