API/Samples/Java/Database/TableStructure(SDBCX)
Table Structure (extended)
Beside the database meta data interface, the interfaces in the namespace com.sun.star.sdbcx.* supports a different more objective orientated way on looking at the database.
public void displayTableProperties() throws com.sun.star.uno.Exception
{
// the variable xTabSup is a XTablesSupplier which you get from the XDriver in conjunction with the connection
XNameAccess xTables = xTabSup.getTables();
String[] aTableNames = xTables.getElementNames();
if ( 0 != aTableNames.length )
{
Object table = xTables.getByName(aTableNames[0]);
XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, table);
System.out.println("Name: " + xProp.getPropertyValue(NAME));
System.out.println("CatalogName: " + xProp.getPropertyValue("CatalogName"));
System.out.println("SchemaName: " + xProp.getPropertyValue("SchemaName"));
System.out.println("Description: " + xProp.getPropertyValue("Description"));
// the following property is optional so we first must check if it exists
if ( xProp.getPropertySetInfo().hasPropertyByName(TYPE) )
{
System.out.println("Type: " + xProp.getPropertyValue(TYPE));
}
System.out.println("Columns:");
XColumnsSupplier columnsSupplier = (XColumnsSupplier) UnoRuntime.queryInterface(XColumnsSupplier.class, table);
printColumns(columnsSupplier);
System.out.println("Keys:");
printKeys(columnsSupplier);
}
}
//###########################################################
// 15. example
// print all columns of a XColumnsSupplier
//###########################################################
public static void printColumns(final XColumnsSupplier xColumnsSup) throws com.sun.star.uno.Exception, SQLException
{
System.out.println("Example printColumns");
// the table must be at least support a XColumnsSupplier interface
System.out.println("--- Columns ---");
XNameAccess xColumns = xColumnsSup.getColumns();
String[] aColumnNames = xColumns.getElementNames();
for ( int i = 0; i <= aColumnNames.length - 1; i++ )
{
System.out.println(" " + aColumnNames[i]);
}
}
//###########################################################
// 16. example
// print all keys inclusive the columns of a key
//###########################################################
public static void printKeys(final XColumnsSupplier xColumnsSup) throws com.sun.star.uno.Exception, SQLException
{
System.out.println("Example printKeys");
XKeysSupplier xKeysSup = (XKeysSupplier) UnoRuntime.queryInterface(XKeysSupplier.class, xColumnsSup);
if ( xKeysSup != null )
{
System.out.println("--- Keys ---");
XIndexAccess xKeys = xKeysSup.getKeys();
for ( int i = 0; i < xKeys.getCount(); i++ )
{
Object key = xKeys.getByIndex(i);
XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, key);
System.out.println(" " + xProp.getPropertyValue(NAME));
XColumnsSupplier xKeyColumnsSup = (XColumnsSupplier) UnoRuntime.queryInterface(XColumnsSupplier.class, xProp);
printColumns(xKeyColumnsSup);
}
}
}