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 / jdbcViewer / JDBCShell.java next >
Encoding:
Java Source  |  1997-02-21  |  12.3 KB  |  360 lines

  1. //
  2. // Copyright (c) 1996 Symantec.  All Rights Reserved.                                              *
  3. //
  4. // JDBCShell
  5. //    A template to get you up and running with JDBC quickly.
  6. //
  7. // Author: Ron Emrick
  8. // Date  : 2/1/97
  9. //
  10. //  See the usage() method below for acceptable command-line
  11. //  arguments.  Override parseCommandLineArgs() and displayUsage()
  12. //  if you plan on changing/modifying the existing command-line
  13. //  argument set.
  14. //
  15. //  Override methods as you see fit.  If you want all the default functionality of
  16. //  this class, then all you need to override is the execute() method in you
  17. //  derived class.
  18. //
  19. //  The methods with a [*] by them are the ones which allow you to customize the
  20. //  shell code to meet your needs better.
  21. //
  22. //  Program Flow:
  23. //  ------------
  24. //
  25. //  main()
  26. //    |
  27. //    +-- [*] init()
  28. //    |
  29. //    +-- [*] setConnectionProperties()
  30. //    |
  31. //    +-- parseCommandLineArgs()
  32. //    |    |
  33. //    |    +-- [*] parseAppDefinedArgs()
  34. //    |
  35. //    +-- run()
  36. //    |    |
  37. //    |    +-- connect()
  38. //    |    +-- [*] execute()
  39. //    |    +-- disconnect()
  40. //    |
  41. //    +-- [*] finish()
  42. //
  43. //
  44.  
  45. import java.util.Properties;
  46.  
  47. import symjava.sql.*;
  48. import symjava.math.*;
  49.  
  50.  
  51. //////////////////////////////////////////////////////////////////////////
  52. //
  53. // JDBCShell
  54. //
  55. //////////////////////////////////////////////////////////////////////////
  56.  
  57. public class JDBCShell
  58. {
  59.     //
  60.     // Data members
  61.     //
  62.     static protected Properties m_propsConnect = null;  // connection properties
  63.     static protected Driver     m_jdbcDriver   = null;  // JDBC Driver
  64.     static protected Connection m_jdbcConn     = null;  // JDBC Connection
  65.  
  66.     static protected int     m_nTestRuns        = 1;   // number of times to run connect, execute and disconnect
  67.     static protected int     m_nTestExecutions  = 1;   // number of times to execute each individual test
  68.  
  69.     static protected String  m_strAppName       = "JDBCShell";  // default application name
  70.     static protected boolean m_bPrintStackTrace = false;        // print call stack when exception occurs?
  71.  
  72.     ///////////////////////////////////////////////////////////////////////////
  73.     public void shellMain( String args[] )
  74.     {
  75.         // Call any initialization code
  76.         init();
  77.  
  78.         // Set the default connection properties
  79.         setConnectionProperties();
  80.  
  81.         // Parse all command-line arguments
  82.         if ( !parseCommandLineArgs( args ) ) {
  83.             return;
  84.         }
  85.  
  86.         // User friendly message
  87.         if ( args.length == 0 ) {
  88.             System.out.println( "(using default connection properties)" );
  89.         }
  90.  
  91.         // Run the application
  92.         // This will run *all* test n times
  93.         run();
  94.  
  95.         // Call any finishing/cleanup code
  96.         finish();
  97.     }
  98.  
  99.     ///////////////////////////////////////////////////////////////////////////
  100.     public void displayUsage()
  101.     {
  102.         System.out.println( "" );
  103.  
  104.         System.out.println( "usage: java " + m_strAppName + " [options]" );
  105.         System.out.println( "  ?               - print this screen" );
  106.         System.out.println( "  ??              - print the default connection properties" );
  107.         System.out.println( "  -e  <engine>    - engine name" );
  108.         System.out.println( "  -d  <database>  - database name" );
  109.         System.out.println( "  -s  <server>    - server name" );
  110.         System.out.println( "  -p  <port>      - port #" );
  111.         System.out.println( "  -i  <ipaddress> - ip address" );
  112.         System.out.println( "  -u  <user>      - user name" );
  113.         System.out.println( "  -pw <password>  - password" );
  114.         System.out.println( "  -r  <number>    - number of times to execute " + m_strAppName );
  115.         System.out.println( "  -m  <number>    - number of times to execute" );
  116.         System.out.println( "  -x[+|-]         - print exception call stack" );
  117.     }
  118.  
  119.     ///////////////////////////////////////////////////////////////////////////
  120.     protected void setConnectionProperties()
  121.     {
  122.         m_propsConnect = new Properties();
  123.  
  124.         m_propsConnect.put( "ip", "localhost" );
  125.         m_propsConnect.put( "port", "8889" );
  126.         m_propsConnect.put( "engine", "watcom" );
  127.         m_propsConnect.put( "server", "SQL Anywhere 5.0 Sample" );
  128.         m_propsConnect.put( "database", "SQL Anywhere 5.0 Sample" );
  129.         m_propsConnect.put( "user", "dba" );
  130.         m_propsConnect.put( "password", "sql" );
  131.     }
  132.  
  133.     ///////////////////////////////////////////////////////////////////////////
  134.     protected void displayConnectionProperties()
  135.     {
  136.         System.out.println( "" );
  137.  
  138.         System.out.println( "IP Address: " + m_propsConnect.getProperty( "ip" ) );
  139.         System.out.println( "Port #: " + m_propsConnect.getProperty( "port" ) );
  140.         System.out.println( "Engine: " + m_propsConnect.getProperty( "engine" ) );
  141.         System.out.println( "Server: " + m_propsConnect.getProperty( "server" ) );
  142.         System.out.println( "Database: " + m_propsConnect.getProperty( "database" ) );
  143.         System.out.println( "User: " + m_propsConnect.getProperty( "user" ) );
  144.         System.out.println( "Password: " + m_propsConnect.getProperty( "password" ) );
  145.     }
  146.  
  147.     ///////////////////////////////////////////////////////////////////////////
  148.     protected boolean parseCommandLineArgs( String args[] )
  149.     {
  150.         boolean bRetVal = true;
  151.  
  152.         // Check for "?"
  153.         if ( args.length == 1 ) {
  154.             if ( args[0].compareTo( "?" ) == 0 ) {
  155.                 displayUsage();
  156.                 return ( false );
  157.             }
  158.             else if ( args[0].compareTo( "??" ) == 0 ) {
  159.                 displayConnectionProperties();
  160.                 return ( false );
  161.             }
  162.             else {
  163.                 System.out.println( "Unknown option: " + args[0] );
  164.                 displayUsage();
  165.                 return( false );
  166.             }
  167.         }
  168.  
  169.         try {
  170.             int idx = 0;
  171.  
  172.             while ( idx < args.length ) {
  173.                 int iRetVal = parseAppDefinedArgs( idx, args );
  174.                 if ( iRetVal >= 0 ) {
  175.                     idx = iRetVal + 1;
  176.                     continue;
  177.                 }
  178.  
  179.                 if ( args[idx].compareTo( "-e" ) == 0 ) {
  180.                     m_propsConnect.put( "engine", args[++idx] );
  181.                 }
  182.                 else if ( args[idx].compareTo( "-d" ) == 0 ) {
  183.                     m_propsConnect.put( "database", args[++idx] );
  184.                 }
  185.                 else if ( args[idx].compareTo( "-s" ) == 0 ) {
  186.                     m_propsConnect.put( "server", args[++idx] );
  187.                 }
  188.                 else if ( args[idx].compareTo( "-p" ) == 0 ) {
  189.                     m_propsConnect.put( "port", args[++idx] );
  190.                 }
  191.                 else if ( args[idx].compareTo( "-i" ) == 0 ) {
  192.                     m_propsConnect.put( "ipaddress", args[++idx] );
  193.                 }
  194.                 else if ( args[idx].compareTo( "-u" ) == 0 ) {
  195.                     m_propsConnect.put( "user", args[++idx] );
  196.                 }
  197.                 else if ( args[idx].compareTo( "-pw" ) == 0 ) {
  198.                     m_propsConnect.put( "password", args[++idx] );
  199.                 }
  200.                 else if ( args[idx].compareTo( "-r" ) == 0 ) {
  201.                     m_nTestRuns = new Integer( args[++idx] ).intValue();
  202.                 }
  203.                 else if ( args[idx].compareTo( "-m" ) == 0 ) {
  204.                     m_nTestExecutions = new Integer( args[++idx] ).intValue();
  205.                 }
  206.                 else if ( args[idx].compareTo( "-x-" ) == 0 ) {
  207.                     m_bPrintStackTrace = false;
  208.                 }
  209.                 else if ( args[idx].compareTo( "-x+" ) == 0 ) {
  210.                     m_bPrintStackTrace = true;
  211.                 }
  212.                 else if ( args[idx].compareTo( "-x" ) == 0 ) {
  213.                     m_bPrintStackTrace = true;
  214.                 }
  215.                 else {
  216.                     System.out.println( "Unknown option: " + args[idx] );
  217.                     displayUsage();
  218.                     return( false );
  219.                 }
  220.  
  221.                 idx++;
  222.             }
  223.         }
  224.         catch( ArrayIndexOutOfBoundsException x ) {
  225.             handleException( x, "ArrayIndexOutOfBoundsException", "Missing command-line argument" );
  226.             displayUsage();
  227.             bRetVal = false;
  228.         }
  229.         catch( Exception x ) {
  230.             handleException( x, "Exception", "Error parsing command-line arguments" );
  231.             displayUsage();
  232.             bRetVal = false;
  233.         }
  234.  
  235.         return ( bRetVal );
  236.     }
  237.  
  238.     ///////////////////////////////////////////////////////////////////////////
  239.     protected int parseAppDefinedArgs( int idx, String args[] )
  240.     {
  241.         // Define the application specific arguments in your derived class
  242.         return ( -1 );
  243.     }
  244.  
  245.     ///////////////////////////////////////////////////////////////////////////
  246.     protected void handleException( Exception x,
  247.                                     String strType,
  248.                                     String strMsg )
  249.     {
  250.         System.out.println( "" );
  251.         System.out.println( strType + ": " + x.getMessage() );
  252.         System.out.println( strMsg );
  253.         System.out.println( "" );
  254.  
  255.         // Print the call stack (?)
  256.         if ( m_bPrintStackTrace == true ) {
  257.             x.printStackTrace();
  258.         }
  259.     }
  260.  
  261.     ///////////////////////////////////////////////////////////////////////////
  262.     public String getURL()
  263.     {
  264.         String strURL = "jdbc:dbaw://";
  265.  
  266.         strURL += m_propsConnect.getProperty( "ip" ) + ":";
  267.         strURL += m_propsConnect.getProperty( "port" ) + "/";
  268.         strURL += m_propsConnect.getProperty( "engine" ) + "/";
  269.         strURL += m_propsConnect.getProperty( "server" ) + "/";
  270.         strURL += m_propsConnect.getProperty( "database" );
  271.  
  272.         return ( strURL );
  273.     }
  274.  
  275.     ///////////////////////////////////////////////////////////////////////////
  276.     protected void run()
  277.     {
  278.         System.out.println( "" );
  279.  
  280.         for ( int i = 1; i <= m_nTestRuns; i++ ) {
  281.             System.out.println( "\nRun #" + i );
  282.  
  283.             connect();
  284.             execute();
  285.             disconnect();
  286.         }
  287.     }
  288.  
  289.     ///////////////////////////////////////////////////////////////////////////
  290.     public void connect()
  291.     {
  292.         System.out.println( ">> Connecting to... " + getURL() );
  293.  
  294.         try {
  295.             m_jdbcDriver = (symjava.sql.Driver) Class.forName( "symantec.itools.db.jdbc.Driver" ).newInstance();
  296.             m_jdbcConn = m_jdbcDriver.connect( getURL(), m_propsConnect );
  297.         }
  298.         catch( SQLException x ) {
  299.             handleException( x, "SQLException", "failed" );
  300.             return;
  301.         }
  302.         catch( InstantiationException x ) {
  303.             handleException( x, "InstantiationException", "failed" );
  304.             return;
  305.         }
  306.         catch( ClassNotFoundException x ) {
  307.             handleException( x, "ClassNotFoundException", "failed" );
  308.             return;
  309.         }
  310.         catch( IllegalAccessException x ) {
  311.             handleException( x, "IllegalAccessException", "failed" );
  312.             return;
  313.         }
  314.         catch( Exception x ) {
  315.             handleException( x, "Exception", "failed" );
  316.             return;
  317.         }
  318.  
  319.         System.out.println( ">> Success" );
  320.     }
  321.  
  322.     ///////////////////////////////////////////////////////////////////////////
  323.     public void disconnect()
  324.     {
  325.         System.out.println( ">> Disconnecting... " );
  326.  
  327.         try {
  328.             if ( m_jdbcConn != null ) {
  329.                 m_jdbcConn.close();
  330.             }
  331.         }
  332.         catch( SQLException x ) {
  333.             handleException( x, "SQLException", "Disconnection failed" );
  334.             System.out.println( "failed" );
  335.             return;
  336.         }
  337.  
  338.         System.out.println( ">> Done" );
  339.     }
  340.  
  341.     ///////////////////////////////////////////////////////////////////////////
  342.     protected void init()
  343.     {
  344.         // Override
  345.     }
  346.  
  347.     ///////////////////////////////////////////////////////////////////////////
  348.     public void execute()
  349.     {
  350.         // Override
  351.     }
  352.  
  353.     ///////////////////////////////////////////////////////////////////////////
  354.     protected void finish()
  355.     {
  356.         // Override
  357.     }
  358.  
  359. } // class JDBCShell
  360.