API/Samples/Java/Database/TableStructure

From Apache OpenOffice Wiki
< API‎ | Samples‎ | Java
Jump to: navigation, search

Table Structure

The database meta data interface gives you access to many information about the structure of the database. The example below show how to display all tables of the database including the column names using Java.

// displays the structure of the first table
    public static void displayTableStructure(final XConnection con) throws com.sun.star.uno.Exception
    {
        XDatabaseMetaData dm = con.getMetaData();
        XResultSet rsTables = dm.getTables(null, "%", "SALES", null);
        XRow rowTB = (XRow) UnoRuntime.queryInterface(XRow.class, rsTables);
        while ( rsTables.next() )
        {
            String catalog = rowTB.getString(1);
            if ( rowTB.wasNull() )
            {
                catalog = null;
            }
 
            String schema = rowTB.getString(2);
            if ( rowTB.wasNull() )
            {
                schema = null;
            }
 
            String table = rowTB.getString(3);
            String type = rowTB.getString(4);
            System.out.println("Catalog: " + catalog + " Schema: " + schema + " Table: " + table + " Type: " + type);
            System.out.println("------------------ Columns ------------------");
            XResultSet rsColumns = dm.getColumns(catalog, schema, table, "%");
            XRow rowCL = (XRow) UnoRuntime.queryInterface(XRow.class, rsColumns);
            while ( rsColumns.next() )
            {
                System.out.println("Column: " + rowCL.getString(4) + " Type: " + rowCL.getInt(5) + " TypeName: " + rowCL.getString(6));
            }
 
        }
    }
Personal tools