12 April 2014: The OpenOffice Wiki is not, and never was, affected by the heartbleed bug. Users' passwords are safe and wiki users do not need take any actions.

Using the getXXX Methods

From Apache OpenOffice Wiki
Jump to: navigation, search



To get column values from the current row, use the interface com.sun.star.sdbc.XRow. It offers a large number of get methods for all SDBC data types, or rather getXXX methods. The XXX stands for the type retrieved by the method.

Usually, the getXXX method is used for the appropriate type to retrieve the value in each column. For example, the first column in each row of xResult is FIRSTNAME. It is the first column and contains a value of SQL type VARCHAR. The appropriate method to retrieve a VARCHAR value is getString(). It should be used for the second column, as well. The third column BIRTHDATE stores DATE values, the method for date types is getDate(). SDBC is flexible and allows a number of type conversions through getXXX. See the table below for details.

The following code accesses the values stored in the current row of xResult and prints a line with the column values separated by tabs. Each time next() is invoked, the next row becomes the current row, and the loop continues until there are no more rows in xResult.

  public static void selectSalespersons(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);
 
      // create statement and execute query
      XStatement xStatement = xConnection.createStatement();
      XResultSet xResult = xStatement.executeQuery("SELECT FIRSTNAME, LASTNAME, BIRTHDATE FROM SALESMAN");
 
      // process result
      XRow xRow = (XRow)UnoRuntime.queryInterface(XRow.class, xResult);
      while (xResult != null && xResult.next()) {
          String firstName = xRow.getString(1);
          String lastName = xRow.getString(2);
          com.sun.star.util.Date birthDate = xRow.getDate(3);
          System.out.println(firstName + "\t" + lastName + "\t\t" +
              birthDate.Month + "/" + birthDate.Day + "/" + birthDate.Year);
      }
  }

The output looks like this:

 Joseph    Smith        7/2/1946
 Frank     Jones        12/24/1963
 Jane      Esperanza    4/1/1972
 George    Flint        2/13/1953
 Bob       Meyers       9/7/1949

In this code, how the getXXX methods work are shown and the two getXXX calls are examined.

  String firstName = xRow.getString(1);

The method getString() is invoked on xRow, that is, getString() gets the value stored in column no. 1 in the current row of xResult, which is FIRSTNAME. The value retrieved by getString() has been converted from a VARCHAR to a String in the Java programming language, and assigned to the String object firstname.

The situation is similar with the method getDate(). It retrieves the value stored in column no. 3 (BIRTHDATE), which is an SQL DATE, and converts it to a com.sun.star.util.Date before assigning it to the variable birthDate.

Note that the column number refers to the column number in the result set, not in the original table.

SDBC is flexible as to which getXXX methods can be used to retrieve the various SQL types. For example, the method getInt() can be used to retrieve any of the numeric or character types. The data it retrieves is converted to an int; that is, if the SQL type is VARCHAR, SDBC attempts to parse an integer out of the VARCHAR. To be sure that no information is lost, the method getInt() is only recommended for SQL INTEGER types, and it cannot be used for the SQL types BINARY, VARBINARY, LONGVARBINARY, DATE, TIME, or TIMESTAMP.

Although getString() is recommended for the SQL types CHAR and VARCHAR, it is possible to retrieve any of the basic SQL types with it. The new SQL3 data types can not be retrieved with it. Getting values with getString() can be useful, but has its limitations. For instance, if it is used to retrieve a numeric type, getString() converts the numeric value to a Java String object, and the value has to be converted back to a numeric type before it can be used for numeric operations.

The value will be treated as a string, so if an application is to retrieve and display arbitrary column values of any standard SQL type other than SQL3 types, use getString().

The illustration below shows all getXXX() methods and the corresponding SDBC data types defined in com.sun.star.sdbc.DataType. The illustration above shows which methods can legally be used to retrieve SQL types, and which methods are recommended for retrieving the various SQL types.

Methods to Retrieve SQL Types
  • x with grey background indicates that the getXXX() method is the recommended method to retrieve an SDBC data type. No data will be lost due to type conversion.
  • x indicates that the getXXX() method may legally be used to retrieve the given SDBC type. However, type conversion will take place and affect the values you obtain.
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages