home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / DBServ / SAMPLES / SAMPLES.ZIP / JDBCDML / jdbcViewer.java < prev   
Encoding:
Java Source  |  1997-02-21  |  10.9 KB  |  339 lines

  1. //
  2. // Copyright (c) 1996 Symantec.  All Rights Reserved.                                              *
  3. //
  4. // jdbcViewer
  5. //
  6. // Author: Ron Emrick
  7. // Date  : 2/1/97
  8. //
  9. // Description:
  10. //   jdbcViewer allows you to "view" information about a specified table
  11. //   by displaying the table's column information along with its contents.
  12. //
  13. // Steps:
  14. //   1) Type at the DOS prompt: java jdbcViewer -t customer
  15. //      (Assumes the database you're connected to has a customer table)
  16. //      This application will verify the table exists and report a problem
  17. //      if it doesn't
  18. //   2) Watch the output
  19. //
  20. //   jdbcViewer is a Java Console application so you must specify the
  21. //   table name you wish to view using the -t command-line argument.
  22. //   For example, "java jdbcViewer -t customers" will display
  23. //   information on the customer table in the current database you're
  24. //   connected to with dbANYWHERE Server.  For connection options
  25. //   and command-line arguments, see the inherited class JDBCShell.
  26. //   Also, passing a ? will display the usage of jdbcViewer.
  27. //
  28.  
  29. import java.util.Properties;
  30.  
  31. import symjava.sql.*;
  32.  
  33. //////////////////////////////////////////////////////////////////////////
  34. //
  35. // jdbcViewer
  36. //
  37. //////////////////////////////////////////////////////////////////////////
  38.  
  39. class jdbcViewer extends JDBCShell
  40. {
  41.     //
  42.     // Data members
  43.     //
  44.     static protected String m_strTable = null;  // table to display information/contents
  45.     static protected String m_strSQL   = null;  // SQL to execute
  46.  
  47.     ///////////////////////////////////////////////////////////////////////////
  48.     public static void main( String args[] )
  49.     {
  50.         jdbcViewer jVw = new jdbcViewer();
  51.         jVw.shellMain( args );
  52.     }
  53.  
  54.     ///////////////////////////////////////////////////////////////////////////
  55.     public void displayUsage()
  56.     {
  57.         super.displayUsage();
  58.  
  59.         System.out.println( "  -t  <tablename> - table to display information/contents" );
  60.     }
  61.  
  62.     ///////////////////////////////////////////////////////////////////////////
  63.     protected void init()
  64.     {
  65.         m_strAppName = "jdbcViewer";
  66.     }
  67.  
  68.     ///////////////////////////////////////////////////////////////////////////
  69.     //
  70.     // Return a number < 0 if no arguments match
  71.     //
  72.     protected int parseAppDefinedArgs( int idx, String args[] )
  73.     {
  74.         if ( args[idx].compareTo( "-t" ) == 0 ) {
  75.             m_strTable = args[++idx];
  76.             return ( idx );
  77.         }
  78.  
  79.         return ( -1 );
  80.     }
  81.  
  82.     ///////////////////////////////////////////////////////////////////////////
  83.     public void execute()
  84.     {
  85.         tableViewer( m_nTestExecutions );
  86.     }
  87.  
  88.     ///////////////////////////////////////////////////////////////////////////
  89.     public void tableViewer( int iterations )
  90.     {
  91.         for ( int i = 0; i < iterations; i++ ) {
  92.             processTable( m_strTable );
  93.         }
  94.     }
  95.  
  96.     ///////////////////////////////////////////////////////////////////////////
  97.     public void processTable( String strTablename )
  98.     {
  99.         if ( strTablename == null ) {
  100.             System.out.println( "Please use the -t option to specify a table to view" );
  101.             return;
  102.         }
  103.  
  104.         try {
  105.             Statement jdbcStmt = prepareQuery( strTablename );
  106.             if ( jdbcStmt == null ) {
  107.                 System.out.println( "Invalid Statement" );
  108.                 return;
  109.             }
  110.  
  111.             ResultSet jdbcRs = jdbcStmt.executeQuery( m_strSQL );
  112.             if ( jdbcRs == null ) {
  113.                 System.out.println( "Invalid ResultSet" );
  114.                 return;
  115.             }
  116.  
  117.             displayColumnInfo( jdbcRs );
  118.             displayTableData( jdbcRs );
  119.  
  120.             jdbcStmt.close();
  121.         }
  122.         catch( SQLException x ) {
  123.             handleException( x, "SQLException", "Unable to view table: " + strTablename );
  124.         }
  125.     }
  126.  
  127.     ///////////////////////////////////////////////////////////////////////////
  128.     public Statement prepareQuery( String strTablename ) throws SQLException
  129.     {
  130.         Statement jdbcStmt = null;
  131.  
  132.         try {
  133.             // Make sure the table exists in the database
  134.             verifyTableExists( strTablename );
  135.  
  136.             // Create the SQL string to execute
  137.             m_strSQL = new String( "select * from " + strTablename );
  138.  
  139.             // Create the JDBC Statement
  140.             jdbcStmt = m_jdbcConn.createStatement();
  141.         }
  142.         catch( SQLException x ) {
  143.             throw new SQLException( "[Query preparation error] " + x.getMessage() );
  144.         }
  145.  
  146.         return ( jdbcStmt );
  147.     }
  148.  
  149.     ///////////////////////////////////////////////////////////////////////////
  150.     public boolean verifyTableExists( String strTablename ) throws SQLException
  151.     {
  152.         boolean bRetVal = false;
  153.  
  154.         try {
  155.             DatabaseMetaData meta = m_jdbcConn.getMetaData();
  156.             if ( meta == null ) {
  157.                 throw new SQLException( "Invalid DatabaseMetaData encountered [null]" );
  158.             }
  159.  
  160.             ResultSet rs = meta.getTables( null, null, strTablename, null );
  161.             bRetVal = rs.next();
  162.             rs.close();
  163.         }
  164.         catch( SQLException x ) {
  165.             throw new SQLException( "[Table verification error] " + x.getMessage() );
  166.         }
  167.  
  168.         return ( bRetVal );
  169.     }
  170.  
  171.     ///////////////////////////////////////////////////////////////////////////
  172.     public void displayColumnInfo( ResultSet rs ) throws SQLException
  173.     {
  174.         try {
  175.             ResultSetMetaData meta = rs.getMetaData();
  176.             if ( meta == null ) {
  177.                 throw new SQLException( "Invalid ResultSetMetaData encountered [null]" );
  178.             }
  179.  
  180.             System.out.println( "" );
  181.             System.out.println( "** Column Information **" );
  182.             System.out.println( "" );
  183.  
  184.             int nCols = meta.getColumnCount();
  185.             for ( int i = 1; i <= nCols; i++ ) {
  186.                 System.out.println( "\tColumn " + i );
  187.                 System.out.println( "\t  Column Name     : " + meta.getColumnName( i ) );
  188.                 System.out.println( "\t  Column Label    : " + meta.getColumnLabel( i ) );
  189.                 System.out.println( "\t  Column Type Name: " + meta.getColumnTypeName( i ) );
  190.                 System.out.println( "\t  Column Type     : " + meta.getColumnType( i ) );
  191.                 System.out.println( "\t---------" );
  192.             }
  193.         }
  194.         catch( SQLException x ) {
  195.             throw new SQLException( "[Column information display error] " + x.getMessage() );
  196.         }
  197.     }
  198.  
  199.     ///////////////////////////////////////////////////////////////////////////
  200.     public void displayTableData( ResultSet rs ) throws SQLException
  201.     {
  202.         try {
  203.             ResultSetMetaData meta = rs.getMetaData();
  204.             if ( meta == null ) {
  205.                 throw new SQLException( "Invalid ResultSetMetaData encountered [null]" );
  206.             }
  207.  
  208.             System.out.println( "" );
  209.             System.out.println( "** Table Data **" );
  210.             System.out.println( "" );
  211.  
  212.             int nRows = 0;
  213.             int nCols = meta.getColumnCount();
  214.             while ( rs.next() ) {
  215.                 System.out.println( "\tRow (" + ( nRows + 1 ) + ")" );
  216.  
  217.                 for ( int i = 1; i <= nCols; i++ ) {
  218.                     System.out.println( "\t[" + meta.getColumnName( i ) + "] " +
  219.                         getColumnDataValue( i, meta.getColumnType( i ), rs ) );
  220.                 }
  221.  
  222.                 System.out.println( "\t---------" );
  223.                 nRows++;
  224.             }
  225.  
  226.             System.out.println( "Total number of rows = " + nRows );
  227.  
  228.         }
  229.         catch( SQLException x ) {
  230.             throw new SQLException( "[Table data display error] " + x.getMessage() );
  231.         }
  232.     }
  233.  
  234.     ///////////////////////////////////////////////////////////////////////////
  235.     public String getColumnDataValue( int colNum, int colType, ResultSet rs ) throws SQLException
  236.     {
  237.         String strValue = "";
  238.  
  239.         switch ( colType ) {
  240.             case Types.BIT:
  241.                 strValue = String.valueOf( rs.getBoolean( colNum ) );
  242.                 break;
  243.  
  244.             case Types.TINYINT:
  245.                 strValue = String.valueOf( rs.getByte( colNum ) );
  246.                 break;
  247.  
  248.             case Types.SMALLINT:
  249.                 strValue = String.valueOf( rs.getShort( colNum ) );
  250.                 break;
  251.  
  252.             case Types.INTEGER:
  253.                 strValue = String.valueOf( rs.getInt( colNum ) );
  254.                 break;
  255.  
  256.             case Types.BIGINT:
  257.                 strValue = String.valueOf( rs.getLong( colNum ) );
  258.                 break;
  259.  
  260.             case Types.FLOAT:
  261.                 strValue = String.valueOf( rs.getFloat( colNum ) );
  262.                 break;
  263.  
  264.             case Types.REAL:
  265.                 strValue = String.valueOf( rs.getFloat( colNum ) );
  266.                 break;
  267.  
  268.             case Types.DOUBLE:
  269.                 strValue = String.valueOf( rs.getDouble( colNum ) );
  270.                 break;
  271.  
  272.             case Types.NUMERIC:
  273.             case Types.DECIMAL:
  274.             {
  275.                 ResultSetMetaData meta = rs.getMetaData();
  276.                 symjava.math.BigDecimal n = rs.getBigDecimal( colNum, meta.getScale( colNum ) );
  277.                 if ( n != null ) {
  278.                     strValue = String.valueOf( n );
  279.                 }
  280.                 break;
  281.             }
  282.  
  283.             case Types.CHAR:
  284.             case Types.VARCHAR:
  285.                 strValue = rs.getString( colNum );
  286.                 break;
  287.  
  288.             case Types.LONGVARCHAR:
  289.                 strValue = "<LONGVARCHAR DATA>";
  290.                 break;
  291.  
  292.             case Types.DATE:
  293.                 symjava.sql.Date dt = rs.getDate( colNum );
  294.                 if ( dt != null ) {
  295.                     strValue = dt.toString();
  296.                 }
  297.                 break;
  298.  
  299.             case Types.TIME:
  300.                 Time tm = rs.getTime( colNum );
  301.                 if ( tm != null ) {
  302.                     strValue = tm.toString();
  303.                 }
  304.                 break;
  305.  
  306.             case Types.TIMESTAMP:
  307.                 Timestamp ts = rs.getTimestamp( colNum );
  308.                 if ( ts != null ) {
  309.                     strValue = ts.toString();
  310.                 }
  311.                 break;
  312.  
  313.             case Types.BINARY:
  314.             case Types.VARBINARY:
  315.             case Types.LONGVARBINARY:
  316.                 strValue = "<BINARY DATA>";
  317.                 break;
  318.  
  319.             case Types.OTHER:
  320.                 strValue = "Other: " + rs.getString( colNum );
  321.                 break;
  322.  
  323.             case Types.NULL:
  324.                 strValue = "<<NULL>>";
  325.                 break;
  326.  
  327.             default:
  328.                 strValue = "Default: " + rs.getString( colNum );
  329.                 break;
  330.         }
  331.  
  332.         if ( rs.wasNull() == true ) {
  333.             strValue = "<<NULL>>";
  334.         }
  335.  
  336.         return strValue;
  337.     }
  338.  
  339. } // jdbcViewer