Skip to Main Content
MySQL Cookbook
book

MySQL Cookbook

by Paul DuBois
October 2002
Intermediate to advanced content levelIntermediate to advanced
1024 pages
27h 26m
English
O'Reilly Media, Inc.
Content preview from MySQL Cookbook

Database-Independent Methods of Obtaining Table Information

Problem

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

Solution

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

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++; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

MySQL Reference Manual

MySQL Reference Manual

Michael Widenius, David Axmark, Kaj Arno
High Performance MySQL

High Performance MySQL

Jeremy D. Zawodny, Derek J. Balling
MySQL Stored Procedure Programming

MySQL Stored Procedure Programming

Guy Harrison, Steven Feuerstein

Publisher Resources

ISBN: 0596001452Catalog PageErrata