Database-Independent Methods of Obtaining Table Information

9.8.1 Problem

You want a way to get table information that doesn't use MySQL-specific queries like SHOW COLUMNS.

9.8.2 Solution

This isn't possible for all APIs. One exception is JDBC, which provides a standard interface to table metadata.

9.8.3 Discussion

The preceding methods for obtaining table information used specific SHOW or SELECT queries and showed how to process them using each API. These techniques are MySQL-specific. JDBC provides a way to access this information through a standard interface that makes no reference to particular queries, so you can use it portably with database engines other than MySQL. With this interface, you use your connection object to obtain a database metadata object, then invoke the getColumns( ) method of that object to retrieve column information. getColumns( ) returns a result set containing one row per column name, so you must run a fetch loop to retrieve information about successive columns. Elements of result set rows that are relevant for MySQL are:

Index

Meaning

3

Table name

4

Column name

6

Column type name

7

Column size (for numeric columns, this is the precision)

8

Number of decimal places, for numeric columns

18

Whether or not column values can be NULL

Here's an example that shows how to use getColumns( ) to print a list of column names and types:

DatabaseMetaData md = conn.getMetaData ( );
ResultSet rs = md.getColumns (dbName, "", tblName, "%");
int i = 0;
while (rs.next ( ))
{
 i++;
 System.out.println ("--- Column " + i + " ---");
 System.out.println ("Name: " + rs.getString (4));
 System.out.println ("Type: " + rs.getString (6));
}
rs.close ( );

If the value of the tblName variable is "item", the output looks like this:

--- Column 1 ---
Name: id
Type: int
--- Column 2 ---
Name: name
Type: char
--- Column 3 ---
Name: colors
Type: enum

The four arguments to getColumns( ) are the names of the catalog, schema, and table, followed by a SQL pattern that column names must match to be selected. For MySQL, these arguments have the following meanings:

  • The catalog name is the database name. To use the current database, pass an empty string.
  • MySQL has no concept of schema, so the schema name argument is irrelevant and can be the empty string.
  • The table name argument is a string naming the table.
  • The column name pattern is analogous to using the LIKE clause in a SHOW COLUMNS statement. The example shown above uses "%", which matches all column names. You can pass a specific column name to get information for a single column. (Remember to escape any % and _ characters with a backslash if you want to match them literally.)

Using the mysql Client Program

Writing MySQL-Based Programs

Record Selection Techniques

Working with Strings

Working with Dates and Times

Sorting Query Results

Generating Summaries

Modifying Tables with ALTER TABLE

Obtaining and Using Metadata

Importing and Exporting Data

Generating and Using Sequences

Using Multiple Tables

Statistical Techniques

Handling Duplicates

Performing Transactions

Introduction to MySQL on the Web

Incorporating Query Resultsinto Web Pages

Processing Web Input with MySQL

Using MySQL-Based Web Session Management

Appendix A. Obtaining MySQL Software

Appendix B. JSP and Tomcat Primer

Appendix C. References



MySQL Cookbook
MySQL Cookbook
ISBN: 059652708X
EAN: 2147483647
Year: 2005
Pages: 412
Authors: Paul DuBois

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net