Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Using JDBC to Create Database Objects

By James W. Cooper
2003-05-24


Getting Information on Tables

catalog The name of the catalog to look in for table names. For JDBC-ODBC databases, and many others, this can be set to null. The catalog entry for these databases is actually their absolute pathname in the file system. 
schema The database "schema" to include. Many databases do not support schema, and for others it is the user name of the owner of the database. It is usually set to null. 
tablemask A mask describing the names of the tables you want to retrieve. If you want to retrieve all table names, set it to the wildcard character %. Note that the wildcard character in SQL is the %-sign and not the usual PC user's *-sign. 
types[] This is an array of Strings describing the kinds of tables you want to retrieve. Databases frequently contain a number of tables for internal housekeeping that are of little value to you as a user. If this is null, you will get all these tables. If you make this a one-element array containing the string "TABLES", you will get only the tables of interest to users. 

Simple code for getting the table names in a database amounts to getting the DatabaseMetaData object and retrieving the table names from it:




con = DriverManager.getConnection(url);
//get the database metadata
dma =con.getMetaData();
//now dump out the names of the tables in the database
String[] types = new String[1];
types[0] = "TABLES"; //set table type mask
//note the %-sign is a wild card (not '*')
results = dma.getTables(null, null, "%", types);

Then we can print out the table names, just as we did above:



boolean more = results.next();
while (more)
{
for (i = 1; i <= numCols; i++)
System.out.print(results.getString(i)+" ");
System.out.println();
more = results.next();
}

Enclose all the code in a try block as before.



Tutorial Pages:
» What is a Database?
» Getting Data out of Databases
» Kinds of Databases
» ODBC
» What Is JDBC?
» Installing and Using JDBC
» Types of JDBC Drivers
» Two-Tier and Three-Tier Models
» Writing JDBC Code to Access Databases
» Registering Your Database with ODBC
» Connecting to a Database
» Accessing the Database
» The ResultSet
» ResultSetMetaData
» DatabaseMetaData
» Getting Information on Tables
» Executing SQL Queries, Printing out ResultSets, A Simple JDBC Program
» Building Higher Level JDBC Objects
» Building a Database Object, A Visual Database Program
» Executing a Query
» The Query Result Dialog
» Example Files
» Summary


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT