Difference between revisions of "Documentation/DevGuide/Database/Creating Statements"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
Line 18: Line 18:
 
Once a <code>Statement</code> is obtained, choose the appropriate execution method for the SQL command. For a <code>SELECT</code> statement, use the method <code>executeQuery()</code>. For <code>UPDATE</code>, <code>DELETE</code> and <code>INSERT</code> statements, the proper method is <code>executeUpdate()</code>. To have multiple result sets returned, use <code>execute()</code> together with the interface <idl>com.sun.star.sdbc.XMultipleResults</idl> of the statement.
 
Once a <code>Statement</code> is obtained, choose the appropriate execution method for the SQL command. For a <code>SELECT</code> statement, use the method <code>executeQuery()</code>. For <code>UPDATE</code>, <code>DELETE</code> and <code>INSERT</code> statements, the proper method is <code>executeUpdate()</code>. To have multiple result sets returned, use <code>execute()</code> together with the interface <idl>com.sun.star.sdbc.XMultipleResults</idl> of the statement.
  
{{Documentation/Tip|Data definition commands, such as <tt>CREATE</tt>, <tt>DROP</tt>, <tt>ALTER</tt>, and <tt>GRANT</tt>, can be issued with <tt>executeUpdate()</tt>.}}
+
{{Tip|Data definition commands, such as <tt>CREATE</tt>, <tt>DROP</tt>, <tt>ALTER</tt>, and <tt>GRANT</tt>, can be issued with <tt>executeUpdate()</tt>.}}
  
 
Consider how an <code>XConnection</code> is used to create an <code>XStatement</code> in the following example:
 
Consider how an <code>XConnection</code> is used to create an <code>XStatement</code> in the following example:

Revision as of 19:53, 14 July 2018



A Statement object is required to send SQL statements to the Database Management System (DBMS). A Statement object is created using createStatement() at the com.sun.star.sdbc.XConnection interface of the connection. It returns a com.sun.star.sdbc.Statement service. This Statement is generic, that is, it does not contain any SQL command. It can be used for all kinds of SQL commands. Its main interface is com.sun.star.sdbc.XStatement:

  com::sun::star::sdbc::XResultSet executeQuery( [in] string sql)
  long executeUpdate( [in] string sql)
  boolean execute( [in] string sql)
  com::sun::star::sdbc::XConnection getConnection()

Once a Statement is obtained, choose the appropriate execution method for the SQL command. For a SELECT statement, use the method executeQuery(). For UPDATE, DELETE and INSERT statements, the proper method is executeUpdate(). To have multiple result sets returned, use execute() together with the interface com.sun.star.sdbc.XMultipleResults of the statement.

Tip.png Data definition commands, such as CREATE, DROP, ALTER, and GRANT, can be issued with executeUpdate().


Consider how an XConnection is used to create an XStatement in the following example:

  public static void executeSelect(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"));
 
      // connect
      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);
 
      // the connection creates a statement
      XStatement xStatement = xConnection.createStatement();
 
      // The XStatement interface is used to execute a SELECT command
      // Double quotes for identifiers in the SELECT string must be escaped in Java
      XResultSet xResult = xStatement.executeQuery("Select * from \"Table1\"");
 
      // process the result ...
      XRow xRow = (XRow)UnoRuntime.queryInterface(XRow.class, xResult);
      while (xResult != null && xResult.next()) {
          System.out.println(xRow.getString(1));
      }
  }

The remainder of this section discusses how to enter data into a table and retrieving the data later, using INSERT and SELECT commands with a com.sun.star.sdbc.Statement.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages