PreparedStatement From DataSource Queries

From Apache OpenOffice Wiki
Jump to: navigation, search

Use the to get the necessary statement objects to open predefined queries and tables in a data source, and to execute arbitrary SQL statements:

 com::sun::star::sdbc::XPreparedStatement prepareCommand(
                   [in] string command, [in] long commandType)

If the value of the parameter is TABLE or QUERY, pass a table name or query name that exists in the of the connection. The value COMMAND makes prepareCommand() expect an SQL string. The result is a prepared statement object that can be parameterized and executed.

The following fragment opens a predefined query in a database Ada01:

 // retrieve the DatabaseContext and get its interface
 XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(
     XNameAccess.class, _rMSF.createInstance(""));
 Object dataSource = xNameAccess.getByName("Ada01");
 XDataSource xDataSource = (XDataSource)UnoRuntime.queryInterface(XDataSource.class, dataSource);
 Object interactionHandler = _rMSF.createInstance("");
 XInteractionHandler xInteractionHandler = (XInteractionHandler)UnoRuntime.queryInterface(
     XInteractionHandler.class, interactionHandler);
 XCompletedConnection xCompletedConnection = (XCompletedConnection)UnoRuntime.queryInterface(
   XCompletedConnection.class, dataSource);
 XConnection xConnection = xCompletedConnection.connectWithCompletion(xInteractionHandler);
 XCommandPreparation xCommandPreparation = (XCommandPreparation)UnoRuntime.queryInterface(
     XCommandPreparation.class, xConnection);
 XPreparedStatement xPreparedStatement = xCommandPreparation.prepareCommand(
    "Query1", CommandType.QUERY);
 XResultSet xResult = xPreparedStatement.executeQuery();
 XRow xRow = (XRow)UnoRuntime.queryInterface(XRow.class, xResult);
 while (xResult != null && {
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages