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

From Apache OpenOffice Wiki
Jump to: navigation, search
m (1 revision(s))
m
Line 9: Line 9:
 
<!--<idltopic>com.sun.star.sdbc.Statement</idltopic>-->
 
<!--<idltopic>com.sun.star.sdbc.Statement</idltopic>-->
 
A Statement object is required to send SQL statements to the Database Management System (DBMS). A Statement object is created using <code>createStatement()</code> at the <idl>com.sun.star.sdbc.XConnection</idl> interface of the connection. It returns a <idl>com.sun.star.sdbc.Statement</idl> service. This <code>Statement</code> 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 <idl>com.sun.star.sdbc.XStatement</idl>:
 
A Statement object is required to send SQL statements to the Database Management System (DBMS). A Statement object is created using <code>createStatement()</code> at the <idl>com.sun.star.sdbc.XConnection</idl> interface of the connection. It returns a <idl>com.sun.star.sdbc.Statement</idl> service. This <code>Statement</code> 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 <idl>com.sun.star.sdbc.XStatement</idl>:
 
+
<source lang="idl">
 
   com::sun::star::sdbc::XResultSet executeQuery( [in] string sql)
 
   com::sun::star::sdbc::XResultSet executeQuery( [in] string sql)
 
   long executeUpdate( [in] string sql)
 
   long executeUpdate( [in] string sql)
 
   boolean execute( [in] string sql)
 
   boolean execute( [in] string sql)
 
   com::sun::star::sdbc::XConnection getConnection()
 
   com::sun::star::sdbc::XConnection getConnection()
 
+
</source>
 
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.
  
Line 20: Line 20:
  
 
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:
 
+
<source lang="java">
 
   public static void executeSelect(XMultiServiceFactory _rMSF) throws com.sun.star.uno.Exception {
 
   public static void executeSelect(XMultiServiceFactory _rMSF) throws com.sun.star.uno.Exception {
 
           // retrieve the DatabaseContext and get its com.sun.star.container.XNameAccess interface
 
           // retrieve the DatabaseContext and get its com.sun.star.container.XNameAccess interface
Line 49: Line 49:
 
       }
 
       }
 
   }
 
   }
 
+
</source>
 
The remainder of this section discusses how to enter data into a table and retrieving the data later, using <code>INSERT</code> and <code>SELECT</code> commands with a <idl>com.sun.star.sdbc.Statement</idl>.
 
The remainder of this section discusses how to enter data into a table and retrieving the data later, using <code>INSERT</code> and <code>SELECT</code> commands with a <idl>com.sun.star.sdbc.Statement</idl>.
  
 
{{PDL1}}
 
{{PDL1}}
 
[[Category: Database Access]]
 
[[Category: Database Access]]

Revision as of 15:12, 31 May 2008



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.

Template:Documentation/Tip

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