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