![]() |
|||
![]() ![]() |
![]() |
|
![]() |
Most of these methods need arguments such as catalog name, schema name, procedure, table, and column name. All the parameters named xxxPattern do not have to match a particular database object name. If necessary, they may be composed of the % and _ matching characters. The % matches zero or more characters, while _ matches any one character. All these methods return ResultSets. Indeed, they usually return multiple values, which means providing results through ResultSets is very convenient. As Figure 7-6 shows, the usual method is used to scan those result sets.
Listing 7-6 includes some database metadata calls that show how to query metadata information using these last methods. Listing 7-6: Metadata on database objects. // retrieving results import java.sql.*; class SimpleExample { public static void main(String args[]) { String url = jdbc:odbc:mysource; try { Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); Connection myConnection = DriverManager.getConnection(url, javauser, hotjava); DatabaseMetaData dmtd = myConnection.getMetaData(); // list catalogs managed by this dbms scanRS(Info on + dmtd.getCatalogTerm() + (s):, dmtd.getCatalogs()); // list all tables in current catalog, belonging // to all schemas, of all types scanRS(Info on tables:, dmtd.getTables(myConnection.getCatalog(), %, %, null)); // list all procedures in current catalog, // belonging to all schemas scanRS(Info on + dmtd.getProcedureTerm() + (s):, dmtd.getProcedures(myConnection.getCatalog(), %, %)); myConnection.close(); } catch(java.lang.Exception ex) { ex.printStackTrace(); } } private static void scanRS(String info, ResultSet rs) throws SQLException { System.out.println(info); System.out.println(); if (rs != null) { int i; // we have a ResultSet ResultSetMetaData rsmd = rs.getMetaData(); int numCols = rsmd.getColumnCount(); // display column headers for (i = 1; i <= numCols; i++) { if (i > 1) System.out.print(, ); System.out.print(rsmd.getColumnLabel(i)); } System.out.println(); // step through the rows while (rs.next()) { // process the columns for (i = 1; i <= numCols; i++) { if (i > 1) System.out.print(, ); System.out.print(rs.getString(i)); } System.out.println(); } } else { System.out.println(no data returned...); } System.out.println (); } } The Listing 7-6 example prints information similar to the following listing. The data returned here came from a Sybase System 11 server. The users current catalog is the default sample database provided by Sybase. Info on database(s) TABLE_QUALIFIER master model pubs2 sybsecurity sybsyntax sybsystemprocs tempdb test testdb2 userdb Info on tables TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, TABLE_TYPE, REMARKS pubs2, dbo, sysalternates, SYSTEM TABLE, null pubs2, dbo, syscolumns, SYSTEM TABLE, null pubs2, dbo, syscomments, SYSTEM TABLE, null pubs2, dbo, sysconstraints, SYSTEM TABLE, null pubs2, dbo, sysdepends, SYSTEM TABLE, null pubs2, dbo, sysindexes, SYSTEM TABLE, null pubs2, dbo, syskeys, SYSTEM TABLE, null pubs2, dbo, syslogs, SYSTEM TABLE, null pubs2, dbo, sysobjects, SYSTEM TABLE, null pubs2, dbo, sysprocedures, SYSTEM TABLE, null pubs2, dbo, sysprotects, SYSTEM TABLE, null pubs2, dbo, sysreferences, SYSTEM TABLE, null pubs2, dbo, syssegments, SYSTEM TABLE, null pubs2, dbo, systypes, SYSTEM TABLE, null pubs2, dbo, sysusermessages, SYSTEM TABLE, null pubs2, dbo, sysusers, SYSTEM TABLE, null pubs2, dbo, au_pix, TABLE, null pubs2, dbo, authors, TABLE, null pubs2, dbo, blurbs, TABLE, null pubs2, dbo, discounts, TABLE, null pubs2, dbo, publishers, TABLE, null pubs2, dbo, roysched, TABLE, null pubs2, dbo, sales, TABLE, null pubs2, dbo, salesdetail, TABLE, null pubs2, dbo, stores, TABLE, null pubs2, dbo, titleauthor, TABLE, null pubs2, dbo, titles, TABLE, null Info on Stored Procedure(s) PROCEDURE_QUALIFIER, PROCEDURE_OWNER, PROCEDURE_NAME, NUM_INPUT_PARAMS, NUM_OUTPUT_PARAMS, NUM_RESULT_SETS, REMARKS, PROCEDURE_TYPE pubs2, dbo, byroyalty, null, null, null, null, 1 pubs2, dbo, discount_proc, null, null, null, null, 1 pubs2, dbo, history_proc, null, null, null, null, 1 pubs2, dbo, insert_sales_proc, null, null, null, null, 1 pubs2, dbo, insert_salesdetail_proc, null, null, null, null, 1 pubs2, dbo, storeid_proc, null, null, null, null, 1 pubs2, dbo, storename_proc, null, null, null, null, 1 pubs2, dbo, title_proc, null, null, null, null, 1 pubs2, dbo, titleid_proc, null, null, null, null, 1 pubs2, guest, testproc, null, null, null, null, 1 The ResultSetMetaData InterfaceOnce a ResultSet has been returned by the database, it is possible to get all kinds of information concerning the ResultSet, such as the columns it contains and its type and label. The ResultSetMetaData interface offers the following methods for finding out about a result sets metadata. These methods also work on ResultSets returned by some of the DatabaseMetaData class because those ResultSets are just ResultSets. Information on ResultSet ColumnsThe ResultSetMetaData interface is a rich interface providing many methods for obtaining information about the columns of a result set. ResultSetMetaData int getColumnCount(); int getColumnDisplaySize(int column); String getColumnLabel(int column); String getColumnName(int column); String getSchemaName(int column); int getPrecision(int column); int getScale(int column); String getTableName(int column); String getCatalogName(int column); int getColumnType(int column); String getColumnTypeName(int column);
|
![]() |
|