PreparedStatement From DataSource Queries

From Apache OpenOffice Wiki
Jump to: navigation, search



Use the com.sun.star.sdb.XCommandPreparation 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 com.sun.star.sdb.CommandType is TABLE or QUERY, pass a table name or query name that exists in the com.sun.star.sdb.DataSource 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 com.sun.star.container.XNameAccess interface
  XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(
      XNameAccess.class, _rMSF.createInstance("com.sun.star.sdb.DatabaseContext"));
 
  Object dataSource = xNameAccess.getByName("Ada01");
  XDataSource xDataSource = (XDataSource)UnoRuntime.queryInterface(XDataSource.class, dataSource);
  Object interactionHandler = _rMSF.createInstance("com.sun.star.sdb.InteractionHandler");
  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 && xResult.next()) {
      System.out.println(xRow.getString(1));
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages