Difference between revisions of "Documentation/DevGuide/Database/PreparedStatement From DataSource Queries"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
 
Line 9: Line 9:
 
<!--<idltopic>com.sun.star.sdb.XCommandPreparation;com.sun.star.sdb.CommandType</idltopic>-->
 
<!--<idltopic>com.sun.star.sdb.XCommandPreparation;com.sun.star.sdb.CommandType</idltopic>-->
 
Use the <idl>com.sun.star.sdb.XCommandPreparation</idl> to get the necessary statement objects to open predefined queries and tables in a data source, and to execute arbitrary SQL statements:
 
Use the <idl>com.sun.star.sdb.XCommandPreparation</idl> to get the necessary statement objects to open predefined queries and tables in a data source, and to execute arbitrary SQL statements:
 
+
<syntaxhighlight lang="idl">
 
   com::sun::star::sdbc::XPreparedStatement prepareCommand(
 
   com::sun::star::sdbc::XPreparedStatement prepareCommand(
 
                     [in] string command, [in] long commandType)
 
                     [in] string command, [in] long commandType)
 
+
</syntaxhighlight>
 
If the value of the parameter <idl>com.sun.star.sdb.CommandType</idl> is <code>TABLE</code> or <code>QUERY</code>, pass a table name or query name that exists in the <idl>com.sun.star.sdb.DataSource</idl> of the connection. The value <code>COMMAND</code> makes <code>prepareCommand()</code> expect an SQL string. The result is a prepared statement object that can be parameterized and executed.
 
If the value of the parameter <idl>com.sun.star.sdb.CommandType</idl> is <code>TABLE</code> or <code>QUERY</code>, pass a table name or query name that exists in the <idl>com.sun.star.sdb.DataSource</idl> of the connection. The value <code>COMMAND</code> makes <code>prepareCommand()</code> 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:
 
The following fragment opens a predefined query in a database Ada01:
 
+
<syntaxhighlight lang="java">
 
   // retrieve the DatabaseContext and get its com.sun.star.container.XNameAccess interface
 
   // retrieve the DatabaseContext and get its com.sun.star.container.XNameAccess interface
 
   XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(
 
   XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(
Line 42: Line 42:
 
       System.out.println(xRow.getString(1));
 
       System.out.println(xRow.getString(1));
 
   }
 
   }
 
+
</syntaxhighlight>
 
{{PDL1}}
 
{{PDL1}}
  
 
[[Category:Documentation/Developer's Guide/Database Access]]
 
[[Category:Documentation/Developer's Guide/Database Access]]

Latest revision as of 14:59, 21 December 2020



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