ResultSetMetaData

From Apache OpenOffice Wiki
Jump to: navigation, search



When you develop applications that allow users to create their own SQL statements, for example, through a user interface, information about the result set to be displayed is required. For this reason, the result set supports a method to examine the meta data, that is, information about the columns in the result set. This information could cover items, such as the name of the column, if it is null, if it is an auto increment column, or a currency column. For detailed information, see the interface com.sun.star.sdbc.XResultSetMetaData. The following code fragment shows the use of the XResultSetMetaData interface:

  XStatement stmt = con.createStatement();
 
  XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, stmt);
  xProp.setPropertyValue("ResultSetType",new java.lang.Integer(ResultSetType.SCROLL_INSENSITIVE));
  xProp.setPropertyValue("ResultSetConcurrency", new java.lang.Integer(ResultSetConcurrency.READ_ONLY));
 
  XResultSet rs = stmt.executeQuery("SELECT NAME, PRICE FROM SALES");
  XResultSetMetaDataSupplier xRsMetaSup = (XResultSetMetaDataSupplier)UnoRuntime.queryInterface(
  XResultSetMetaDataSupplier.class, rs);
  XResultSetMetaData xRsMetaData = xRsMetaSup.getMetaData();
 
  int nColumnCount = xRsMetaData.getColumnCount();
 
  for (int i=1 ;i <= nColumnCount; ++i) {
      System.out.println("Name: " + xRsMetaData.getColumnName(i) + " Type: " +
      xRsMetaData.getColumnType(i));
  }

The printout looks similar to this:

 Name:   NAME    Type:   12
 Name:   PRICE   Type:   3

Notice that the Type returned is the number for the corresponding SQL data type. In this case, VARCHAR has the value 12 and the type 3 is the SQL data type DECIMAL. The whole list of data types can be found at com.sun.star.sdbc.DataType.

Note that the com.sun.star.sdbc.XResultSetMetaData can be requested before you move to the first row.

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