Difference between revisions of "Documentation/DevGuide/Database/Connecting Using the DriverManager and a Database URL"
m (1 revision(s)) |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
|NextPage=Documentation/DevGuide/Database/Connecting Through a Specific Driver | |NextPage=Documentation/DevGuide/Database/Connecting Through a Specific Driver | ||
}} | }} | ||
− | {{DISPLAYTITLE:Connecting Using the DriverManager and a Database URL}} | + | {{Documentation/DevGuideLanguages|Documentation/DevGuide/Database/{{SUBPAGENAME}}}} |
+ | {{DISPLAYTITLE:Connecting Using the DriverManager and a Database URL}} | ||
<!--<idltopic>com.sun.star.sdbc.DriverManager</idltopic>--> | <!--<idltopic>com.sun.star.sdbc.DriverManager</idltopic>--> | ||
− | The database context and establishing connections to a database even if there is no data source for it in {{ | + | The database context and establishing connections to a database even if there is no data source for it in {{AOo}} API can be avoided. |
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: | ||
− | + | <syntaxhighlight 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 21: | ||
void setLoginTimeout( [in] long seconds) | void setLoginTimeout( [in] long seconds) | ||
long getLoginTimeout() | long getLoginTimeout() | ||
− | + | </syntaxhighlight> | |
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: | ||
− | + | <syntaxhighlight lang="java"> | |
Connection xConnection = DriverManager.getConnection(url); | Connection xConnection = DriverManager.getConnection(url); | ||
− | + | </syntaxhighlight> | |
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 32: | ||
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]--> | ||
− | + | <syntaxhighlight 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 73: | ||
} | } | ||
} | } | ||
− | + | </syntaxhighlight> | |
{{PDL1}} | {{PDL1}} | ||
− | [[Category: Database Access]] | + | |
+ | [[Category:Documentation/Developer's Guide/Database Access]] |
Latest revision as of 14:02, 21 December 2020
- Connecting Through a DataSource
- Connecting Using the DriverManager and a Database URL
- Connecting Through a Specific Driver
- Driver Specifics
The database context and establishing connections to a database even if there is no data source for it in Apache OpenOffice 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). |