Difference between revisions of "API/Samples/Java/Database/TableStructure"
From Apache OpenOffice Wiki
Line 1: | Line 1: | ||
== Table Structure == | == 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 | + | 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]]. |
<source lang="java"> | <source lang="java"> | ||
Line 39: | Line 39: | ||
[[Category:API]] | [[Category:API]] | ||
+ | [[Category:Java]] |
Latest revision as of 16:21, 4 January 2019
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)); } } }