Using the JDBC Meta Data APIs
We will use the method getT ables to fetch a list of all tables in the database. The
four arguments are: • String catalog: can be used when database systems support catalogs. We will
use null to act as a wildcard match. • String schemaPattern: can be used when database systems support schemas.
We will use null to act as a wildcard match. • String tableNamePattern: a pattern to match table names; we will use “” as
a wildcard match. • String types[]: the types of table names to return. Possible values include
TABLE, VIEW, ALIAS, SYNONYM, and SYSTEM TABLE. The method getT ables
returns a ResultSet so we iterate through returned values just as you would in a regular SQL query using the JDBC APIs:
ResultSet table_rs = md.getTablesnull, null, ,
new String[]{TABLE}; while table_rs.next {
System.out.printlnTable: + table_rs.getString3;
tableNames.addtable_rs.getString3; }
Loop over all tables printing column meta data and the first row:
for String tableName : tableNames { System.out.println\n\n Processing table +
tableName + \n; String query = SELECT from + tableName;
System.out.printlnquery; ResultSet rs = s.executeQueryquery;
ResultSetMetaData table_meta = rs.getMetaData; int columnCount = table_meta.getColumnCount;
System.out.println\nColumn meta data for table:; ListString columnNames = new ArrayListString10;
columnNames.add; for int col=1; col=columnCount; col++ {
System.out.printlnColumn + col + name: +
table_meta.getColumnLabelcol; System.out.println
column data type: + table_meta.getColumnTypeNamecol;
columnNames.addtable_meta.getColumnLabelcol;
184
} System.out.println\nFirst row in table:;
if rs.next { for int col=1; col=columnCount; col++ {
System.out.println + columnNames.getcol +
: + rs.getStringcol; }
} }
} }
Output looks like this: Table: FACTBOOK
Table: USSTATES Processing table FACTBOOK
SELECT from FACTBOOK Column meta data for table:
Column 1 name: NAME column data type: VARCHAR
Column 2 name: LOCATION column data type: VARCHAR
Column 3 name: EXPORT column data type: BIGINT
Column 4 name: IMPORT column data type: BIGINT
Column 5 name: DEBT column data type: BIGINT
Column 6 name: AID column data type: BIGINT
Column 7 name: UNEMPLOYMENT_PERCENT column data type: INTEGER
Column 8 name: INFLATION_PERCENT column data type: INTEGER
First row in table: NAME: Aruba
LOCATION: Caribbean, island in the Caribbean Sea, north of Venezuela
EXPORT: 2200000000
185
IMPORT: 2500000000 DEBT: 285000000
AID: 26000000 UNEMPLOYMENT_PERCENT: 0
INFLATION_PERCENT: 4
Processing table USSTATES SELECT from USSTATES
Column meta data for table: Column 1 name: NAME
column data type: VARCHAR Column 2 name: ABBREVIATION
column data type: CHAR Column 3 name: INDUSTRY
column data type: VARCHAR Column 4 name: AGRICULTURE
column data type: VARCHAR Column 5 name: POPULATION
column data type: BIGINT First row in table:
NAME: Alabama ABBREVIATION: AL
INDUSTRY: Paper, lumber and wood products, mining, rubber and plastic products, transportation
equipment, apparel AGRICULTURE: Poultry and eggs, cattle, nursery stock,
peanuts, cotton, vegetables, milk, soybeans
POPULATION: 4447100 Using the JDBC meta data APIs is a simple technique but can be very useful for
both searching many tables for specific column names and for pulling meta data and row data into local search engines. While most relational databases provide support
for free text search of text fields in a database it is often better to export specific text columns in a table to an external search engine.
We will spend the rest of this chapter on index and search techniques. While we usually index web pages and local document repositories, keep in mind that data
in relational databases can also easily be indexed either with hand written export utilities or automated techniques using the JDBC meta-data APIs that we used in
this section.
186