Difference between revisions of "Documentation/DevGuide/Database/Connecting Through a DataSource"
m (1 revision(s)) |
m |
||
Line 9: | Line 9: | ||
<!--<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 {{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: | ||
− | + | <source lang="idl"> | |
// establish connection | // establish connection | ||
com::sun::star::sdbc::XConnection getConnection( | com::sun::star::sdbc::XConnection getConnection( | ||
Line 17: | Line 17: | ||
void setLoginTimeout( [in] long seconds) | void setLoginTimeout( [in] long seconds) | ||
long getLoginTimeout() | long getLoginTimeout() | ||
− | + | </source> | |
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"> | |
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 29: | ||
// simple way to connect | // simple way to connect | ||
XConnection xConnection = xDS.getConnection("", ""); | XConnection xConnection = xDS.getConnection("", ""); | ||
− | + | </source> | |
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"> | |
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> | |
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>, {{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. | ||
− | + | <source 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 62: | ||
return XConnection; | return XConnection; | ||
} | } | ||
− | + | </source> | |
{{PDL1}} | {{PDL1}} | ||
[[Category: Database Access]] | [[Category: Database Access]] |
Revision as of 19:08, 10 May 2008
- Connecting Through a DataSource
- Connecting Using the DriverManager and a Database URL
- Connecting Through a Specific Driver
- Driver Specifics
Data sources in the database context of OpenOffice.org 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()
, OpenOffice.org 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). |