home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-20 | 12.3 KB | 359 lines |
- //
- // Copyright (c) 1996 Symantec. All Rights Reserved. *
- //
- // JDBCShell
- // A template to get you up and running with JDBC quickly.
- //
- // Author: Ron Emrick
- // Date : 2/1/97
- //
- // See the usage() method below for acceptable command-line
- // arguments. Override parseCommandLineArgs() and displayUsage()
- // if you plan on changing/modifying the existing command-line
- // argument set.
- //
- // Override methods as you see fit. If you want all the default functionality of
- // this class, then all you need to override is the execute() method in you
- // derived class.
- //
- // The methods with a [*] by them are the ones which allow you to customize the
- // shell code to meet your needs better.
- //
- // Program Flow:
- // ------------
- //
- // main()
- // |
- // +-- [*] init()
- // |
- // +-- [*] setConnectionProperties()
- // |
- // +-- parseCommandLineArgs()
- // | |
- // | +-- [*] parseAppDefinedArgs()
- // |
- // +-- run()
- // | |
- // | +-- connect()
- // | +-- [*] execute()
- // | +-- disconnect()
- // |
- // +-- [*] finish()
- //
- //
-
- import java.util.Properties;
-
- import symjava.sql.*;
- import symjava.math.*;
-
- //////////////////////////////////////////////////////////////////////////
- //
- // JDBCShell
- //
- //////////////////////////////////////////////////////////////////////////
-
- public class JDBCShell
- {
- //
- // Data members
- //
- static protected Properties m_propsConnect = null; // connection properties
- static protected Driver m_jdbcDriver = null; // JDBC Driver
- static protected Connection m_jdbcConn = null; // JDBC Connection
-
- static protected int m_nTestRuns = 1; // number of times to run connect, execute and disconnect
- static protected int m_nTestExecutions = 1; // number of times to execute each individual test
-
- static protected String m_strAppName = "JDBCShell"; // default application name
- static protected boolean m_bPrintStackTrace = false; // print call stack when exception occurs?
-
- ///////////////////////////////////////////////////////////////////////////
- public void shellMain( String args[] )
- {
- // Call any initialization code
- init();
-
- // Set the default connection properties
- setConnectionProperties();
-
- // Parse all command-line arguments
- if ( !parseCommandLineArgs( args ) ) {
- return;
- }
-
- // User friendly message
- if ( args.length == 0 ) {
- System.out.println( "(using default connection properties)" );
- }
-
- // Run the application
- // This will run *all* test n times
- run();
-
- // Call any finishing/cleanup code
- finish();
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void displayUsage()
- {
- System.out.println( "" );
-
- System.out.println( "usage: java " + m_strAppName + " [options]" );
- System.out.println( " ? - print this screen" );
- System.out.println( " ?? - print the default connection properties" );
- System.out.println( " -e <engine> - engine name" );
- System.out.println( " -d <database> - database name" );
- System.out.println( " -s <server> - server name" );
- System.out.println( " -p <port> - port #" );
- System.out.println( " -i <ipaddress> - ip address" );
- System.out.println( " -u <user> - user name" );
- System.out.println( " -pw <password> - password" );
- System.out.println( " -r <number> - number of times to execute " + m_strAppName );
- System.out.println( " -m <number> - number of times to execute" );
- System.out.println( " -x[+|-] - print exception call stack" );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected void setConnectionProperties()
- {
- m_propsConnect = new Properties();
-
- m_propsConnect.put( "ip", "localhost" );
- m_propsConnect.put( "port", "8889" );
- m_propsConnect.put( "engine", "watcom" );
- m_propsConnect.put( "server", "Sademo" );
- m_propsConnect.put( "database", "Sademo" );
- m_propsConnect.put( "user", "dba" );
- m_propsConnect.put( "password", "sql" );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected void displayConnectionProperties()
- {
- System.out.println( "" );
-
- System.out.println( "IP Address: " + m_propsConnect.getProperty( "ip" ) );
- System.out.println( "Port #: " + m_propsConnect.getProperty( "port" ) );
- System.out.println( "Engine: " + m_propsConnect.getProperty( "engine" ) );
- System.out.println( "Server: " + m_propsConnect.getProperty( "server" ) );
- System.out.println( "Database: " + m_propsConnect.getProperty( "database" ) );
- System.out.println( "User: " + m_propsConnect.getProperty( "user" ) );
- System.out.println( "Password: " + m_propsConnect.getProperty( "password" ) );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected boolean parseCommandLineArgs( String args[] )
- {
- boolean bRetVal = true;
-
- // Check for "?"
- if ( args.length == 1 ) {
- if ( args[0].compareTo( "?" ) == 0 ) {
- displayUsage();
- return ( false );
- }
- else if ( args[0].compareTo( "??" ) == 0 ) {
- displayConnectionProperties();
- return ( false );
- }
- else {
- System.out.println( "Unknown option: " + args[0] );
- displayUsage();
- return( false );
- }
- }
-
- try {
- int idx = 0;
-
- while ( idx < args.length ) {
- int iRetVal = parseAppDefinedArgs( idx, args );
- if ( iRetVal >= 0 ) {
- idx = iRetVal + 1;
- continue;
- }
-
- if ( args[idx].compareTo( "-e" ) == 0 ) {
- m_propsConnect.put( "engine", args[++idx] );
- }
- else if ( args[idx].compareTo( "-d" ) == 0 ) {
- m_propsConnect.put( "database", args[++idx] );
- }
- else if ( args[idx].compareTo( "-s" ) == 0 ) {
- m_propsConnect.put( "server", args[++idx] );
- }
- else if ( args[idx].compareTo( "-p" ) == 0 ) {
- m_propsConnect.put( "port", args[++idx] );
- }
- else if ( args[idx].compareTo( "-i" ) == 0 ) {
- m_propsConnect.put( "ipaddress", args[++idx] );
- }
- else if ( args[idx].compareTo( "-u" ) == 0 ) {
- m_propsConnect.put( "user", args[++idx] );
- }
- else if ( args[idx].compareTo( "-pw" ) == 0 ) {
- m_propsConnect.put( "password", args[++idx] );
- }
- else if ( args[idx].compareTo( "-r" ) == 0 ) {
- m_nTestRuns = new Integer( args[++idx] ).intValue();
- }
- else if ( args[idx].compareTo( "-m" ) == 0 ) {
- m_nTestExecutions = new Integer( args[++idx] ).intValue();
- }
- else if ( args[idx].compareTo( "-x-" ) == 0 ) {
- m_bPrintStackTrace = false;
- }
- else if ( args[idx].compareTo( "-x+" ) == 0 ) {
- m_bPrintStackTrace = true;
- }
- else if ( args[idx].compareTo( "-x" ) == 0 ) {
- m_bPrintStackTrace = true;
- }
- else {
- System.out.println( "Unknown option: " + args[idx] );
- displayUsage();
- return( false );
- }
-
- idx++;
- }
- }
- catch( ArrayIndexOutOfBoundsException x ) {
- handleException( x, "ArrayIndexOutOfBoundsException", "Missing command-line argument" );
- displayUsage();
- bRetVal = false;
- }
- catch( Exception x ) {
- handleException( x, "Exception", "Error parsing command-line arguments" );
- displayUsage();
- bRetVal = false;
- }
-
- return ( bRetVal );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected int parseAppDefinedArgs( int idx, String args[] )
- {
- // Define the application specific arguments in your derived class
- return ( -1 );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected void handleException( Exception x,
- String strType,
- String strMsg )
- {
- System.out.println( "" );
- System.out.println( strType + ": " + x.getMessage() );
- System.out.println( strMsg );
- System.out.println( "" );
-
- // Print the call stack (?)
- if ( m_bPrintStackTrace == true ) {
- x.printStackTrace();
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public String getURL()
- {
- String strURL = "jdbc:dbaw://";
-
- strURL += m_propsConnect.getProperty( "ip" ) + ":";
- strURL += m_propsConnect.getProperty( "port" ) + "/";
- strURL += m_propsConnect.getProperty( "engine" ) + "/";
- strURL += m_propsConnect.getProperty( "server" ) + "/";
- strURL += m_propsConnect.getProperty( "database" );
-
- return ( strURL );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected void run()
- {
- System.out.println( "" );
-
- for ( int i = 1; i <= m_nTestRuns; i++ ) {
- System.out.println( "\nRun #" + i );
-
- connect();
- execute();
- disconnect();
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void connect()
- {
- System.out.println( ">> Connecting to... " + getURL() );
-
- try {
- m_jdbcDriver = (symjava.sql.Driver) Class.forName( "symantec.itools.db.jdbc.Driver" ).newInstance();
- m_jdbcConn = m_jdbcDriver.connect( getURL(), m_propsConnect );
- }
- catch( SQLException x ) {
- handleException( x, "SQLException", "failed" );
- return;
- }
- catch( InstantiationException x ) {
- handleException( x, "InstantiationException", "failed" );
- return;
- }
- catch( ClassNotFoundException x ) {
- handleException( x, "ClassNotFoundException", "failed" );
- return;
- }
- catch( IllegalAccessException x ) {
- handleException( x, "IllegalAccessException", "failed" );
- return;
- }
- catch( Exception x ) {
- handleException( x, "Exception", "failed" );
- return;
- }
-
- System.out.println( ">> Success" );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void disconnect()
- {
- System.out.println( ">> Disconnecting... " );
-
- try {
- if ( m_jdbcConn != null ) {
- m_jdbcConn.close();
- }
- }
- catch( SQLException x ) {
- handleException( x, "SQLException", "Disconnection failed" );
- System.out.println( "failed" );
- return;
- }
-
- System.out.println( ">> Done" );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected void init()
- {
- // Override
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void execute()
- {
- // Override
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected void finish()
- {
- // Override
- }
-
- } // class JDBCShell
-