home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-21 | 10.9 KB | 339 lines |
- //
- // Copyright (c) 1996 Symantec. All Rights Reserved. *
- //
- // jdbcViewer
- //
- // Author: Ron Emrick
- // Date : 2/1/97
- //
- // Description:
- // jdbcViewer allows you to "view" information about a specified table
- // by displaying the table's column information along with its contents.
- //
- // Steps:
- // 1) Type at the DOS prompt: java jdbcViewer -t customer
- // (Assumes the database you're connected to has a customer table)
- // This application will verify the table exists and report a problem
- // if it doesn't
- // 2) Watch the output
- //
- // jdbcViewer is a Java Console application so you must specify the
- // table name you wish to view using the -t command-line argument.
- // For example, "java jdbcViewer -t customers" will display
- // information on the customer table in the current database you're
- // connected to with dbANYWHERE Server. For connection options
- // and command-line arguments, see the inherited class JDBCShell.
- // Also, passing a ? will display the usage of jdbcViewer.
- //
-
- import java.util.Properties;
-
- import symjava.sql.*;
-
- //////////////////////////////////////////////////////////////////////////
- //
- // jdbcViewer
- //
- //////////////////////////////////////////////////////////////////////////
-
- class jdbcViewer extends JDBCShell
- {
- //
- // Data members
- //
- static protected String m_strTable = null; // table to display information/contents
- static protected String m_strSQL = null; // SQL to execute
-
- ///////////////////////////////////////////////////////////////////////////
- public static void main( String args[] )
- {
- jdbcViewer jVw = new jdbcViewer();
- jVw.shellMain( args );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void displayUsage()
- {
- super.displayUsage();
-
- System.out.println( " -t <tablename> - table to display information/contents" );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected void init()
- {
- m_strAppName = "jdbcViewer";
- }
-
- ///////////////////////////////////////////////////////////////////////////
- //
- // Return a number < 0 if no arguments match
- //
- protected int parseAppDefinedArgs( int idx, String args[] )
- {
- if ( args[idx].compareTo( "-t" ) == 0 ) {
- m_strTable = args[++idx];
- return ( idx );
- }
-
- return ( -1 );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void execute()
- {
- tableViewer( m_nTestExecutions );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void tableViewer( int iterations )
- {
- for ( int i = 0; i < iterations; i++ ) {
- processTable( m_strTable );
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void processTable( String strTablename )
- {
- if ( strTablename == null ) {
- System.out.println( "Please use the -t option to specify a table to view" );
- return;
- }
-
- try {
- Statement jdbcStmt = prepareQuery( strTablename );
- if ( jdbcStmt == null ) {
- System.out.println( "Invalid Statement" );
- return;
- }
-
- ResultSet jdbcRs = jdbcStmt.executeQuery( m_strSQL );
- if ( jdbcRs == null ) {
- System.out.println( "Invalid ResultSet" );
- return;
- }
-
- displayColumnInfo( jdbcRs );
- displayTableData( jdbcRs );
-
- jdbcStmt.close();
- }
- catch( SQLException x ) {
- handleException( x, "SQLException", "Unable to view table: " + strTablename );
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public Statement prepareQuery( String strTablename ) throws SQLException
- {
- Statement jdbcStmt = null;
-
- try {
- // Make sure the table exists in the database
- verifyTableExists( strTablename );
-
- // Create the SQL string to execute
- m_strSQL = new String( "select * from " + strTablename );
-
- // Create the JDBC Statement
- jdbcStmt = m_jdbcConn.createStatement();
- }
- catch( SQLException x ) {
- throw new SQLException( "[Query preparation error] " + x.getMessage() );
- }
-
- return ( jdbcStmt );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public boolean verifyTableExists( String strTablename ) throws SQLException
- {
- boolean bRetVal = false;
-
- try {
- DatabaseMetaData meta = m_jdbcConn.getMetaData();
- if ( meta == null ) {
- throw new SQLException( "Invalid DatabaseMetaData encountered [null]" );
- }
-
- ResultSet rs = meta.getTables( null, null, strTablename, null );
- bRetVal = rs.next();
- rs.close();
- }
- catch( SQLException x ) {
- throw new SQLException( "[Table verification error] " + x.getMessage() );
- }
-
- return ( bRetVal );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void displayColumnInfo( ResultSet rs ) throws SQLException
- {
- try {
- ResultSetMetaData meta = rs.getMetaData();
- if ( meta == null ) {
- throw new SQLException( "Invalid ResultSetMetaData encountered [null]" );
- }
-
- System.out.println( "" );
- System.out.println( "** Column Information **" );
- System.out.println( "" );
-
- int nCols = meta.getColumnCount();
- for ( int i = 1; i <= nCols; i++ ) {
- System.out.println( "\tColumn " + i );
- System.out.println( "\t Column Name : " + meta.getColumnName( i ) );
- System.out.println( "\t Column Label : " + meta.getColumnLabel( i ) );
- System.out.println( "\t Column Type Name: " + meta.getColumnTypeName( i ) );
- System.out.println( "\t Column Type : " + meta.getColumnType( i ) );
- System.out.println( "\t---------" );
- }
- }
- catch( SQLException x ) {
- throw new SQLException( "[Column information display error] " + x.getMessage() );
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void displayTableData( ResultSet rs ) throws SQLException
- {
- try {
- ResultSetMetaData meta = rs.getMetaData();
- if ( meta == null ) {
- throw new SQLException( "Invalid ResultSetMetaData encountered [null]" );
- }
-
- System.out.println( "" );
- System.out.println( "** Table Data **" );
- System.out.println( "" );
-
- int nRows = 0;
- int nCols = meta.getColumnCount();
- while ( rs.next() ) {
- System.out.println( "\tRow (" + ( nRows + 1 ) + ")" );
-
- for ( int i = 1; i <= nCols; i++ ) {
- System.out.println( "\t[" + meta.getColumnName( i ) + "] " +
- getColumnDataValue( i, meta.getColumnType( i ), rs ) );
- }
-
- System.out.println( "\t---------" );
- nRows++;
- }
-
- System.out.println( "Total number of rows = " + nRows );
-
- }
- catch( SQLException x ) {
- throw new SQLException( "[Table data display error] " + x.getMessage() );
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public String getColumnDataValue( int colNum, int colType, ResultSet rs ) throws SQLException
- {
- String strValue = "";
-
- switch ( colType ) {
- case Types.BIT:
- strValue = String.valueOf( rs.getBoolean( colNum ) );
- break;
-
- case Types.TINYINT:
- strValue = String.valueOf( rs.getByte( colNum ) );
- break;
-
- case Types.SMALLINT:
- strValue = String.valueOf( rs.getShort( colNum ) );
- break;
-
- case Types.INTEGER:
- strValue = String.valueOf( rs.getInt( colNum ) );
- break;
-
- case Types.BIGINT:
- strValue = String.valueOf( rs.getLong( colNum ) );
- break;
-
- case Types.FLOAT:
- strValue = String.valueOf( rs.getFloat( colNum ) );
- break;
-
- case Types.REAL:
- strValue = String.valueOf( rs.getFloat( colNum ) );
- break;
-
- case Types.DOUBLE:
- strValue = String.valueOf( rs.getDouble( colNum ) );
- break;
-
- case Types.NUMERIC:
- case Types.DECIMAL:
- {
- ResultSetMetaData meta = rs.getMetaData();
- symjava.math.BigDecimal n = rs.getBigDecimal( colNum, meta.getScale( colNum ) );
- if ( n != null ) {
- strValue = String.valueOf( n );
- }
- break;
- }
-
- case Types.CHAR:
- case Types.VARCHAR:
- strValue = rs.getString( colNum );
- break;
-
- case Types.LONGVARCHAR:
- strValue = "<LONGVARCHAR DATA>";
- break;
-
- case Types.DATE:
- symjava.sql.Date dt = rs.getDate( colNum );
- if ( dt != null ) {
- strValue = dt.toString();
- }
- break;
-
- case Types.TIME:
- Time tm = rs.getTime( colNum );
- if ( tm != null ) {
- strValue = tm.toString();
- }
- break;
-
- case Types.TIMESTAMP:
- Timestamp ts = rs.getTimestamp( colNum );
- if ( ts != null ) {
- strValue = ts.toString();
- }
- break;
-
- case Types.BINARY:
- case Types.VARBINARY:
- case Types.LONGVARBINARY:
- strValue = "<BINARY DATA>";
- break;
-
- case Types.OTHER:
- strValue = "Other: " + rs.getString( colNum );
- break;
-
- case Types.NULL:
- strValue = "<<NULL>>";
- break;
-
- default:
- strValue = "Default: " + rs.getString( colNum );
- break;
- }
-
- if ( rs.wasNull() == true ) {
- strValue = "<<NULL>>";
- }
-
- return strValue;
- }
-
- } // jdbcViewer