001         package com.croftsoft.core.sql;
002    
003         import java.io.Serializable;
004         import java.sql.*;
005    
006         import com.croftsoft.core.lang.NullArgumentException;
007    
008         /*********************************************************************
009         * Initializer object for creating JDBC Connections.
010         *
011         * <p>
012         * Java 1.1 compatible.
013         * </p>
014         *
015         * @version
016         *   2001-06-13
017         * @since
018         *   2001-06-13
019         * @author
020         *   <a target="_blank" href="https://www.croftsoft.com/">David W. Croft</a>
021         *********************************************************************/
022    
023         public final class  ConnectionInit
024           implements Serializable
025         //////////////////////////////////////////////////////////////////////
026         //////////////////////////////////////////////////////////////////////
027         {
028    
029         private static final long  serialVersionUID = 1L;
030    
031         public final String  jdbcDriverClassName;
032    
033         public final String  jdbcUrlString;
034    
035         public final String  username;
036    
037         public final String  password;
038    
039         //////////////////////////////////////////////////////////////////////
040         //////////////////////////////////////////////////////////////////////
041    
042         /*********************************************************************
043         * Main constructor method.
044         *
045         * @throws NullArgumentException
046         *
047         *   If any of the arguments is null.
048         *********************************************************************/
049         public  ConnectionInit (
050           String  jdbcDriverClassName,
051           String  jdbcUrlString,
052           String  username,
053           String  password )
054         //////////////////////////////////////////////////////////////////////
055         {
056           NullArgumentException.check (
057             this.jdbcDriverClassName = jdbcDriverClassName );
058    
059           NullArgumentException.check ( this.jdbcUrlString = jdbcUrlString );
060    
061           NullArgumentException.check ( this.username      = username      );
062    
063           NullArgumentException.check ( this.password      = password      );
064         }
065    
066         /*********************************************************************
067         * Convenience constructor that takes an array of String arguments.
068         *
069         * <p>
070         * The argument ordering is the same as the main constructor method.
071         * </p>
072         *
073         * @throws NullArgumentException
074         *
075         *   If the args is null or any of its elements is null.
076         *
077         * @throws IllegalArgumentException
078         *
079         *   If args.length != 4.
080         *********************************************************************/
081         public  ConnectionInit ( String [ ]  args )
082         //////////////////////////////////////////////////////////////////////
083         {
084           NullArgumentException.check ( args );
085    
086           if ( args.length != 4 )
087           {
088             throw new IllegalArgumentException ( "args.length != 4" );
089           }
090    
091           NullArgumentException.check (
092             this.jdbcDriverClassName = args [ 0 ] );
093    
094           NullArgumentException.check ( this.jdbcUrlString = args [ 1 ] );
095    
096           NullArgumentException.check ( this.username      = args [ 2 ] );
097    
098           NullArgumentException.check ( this.password      = args [ 3 ] );
099         }
100        
101         //////////////////////////////////////////////////////////////////////
102         //////////////////////////////////////////////////////////////////////
103    
104         /*********************************************************************
105         * Convenience method for creating connections.
106         *
107         * <p>
108         * Does not retain reference to newly created Connection.
109         * </p>
110         *********************************************************************/
111         public Connection  createConnection ( )
112           throws ClassNotFoundException, SQLException
113         //////////////////////////////////////////////////////////////////////
114         {
115           Class.forName ( jdbcDriverClassName );
116    
117           return DriverManager.getConnection (
118             jdbcUrlString, username, password );
119         }
120    
121         //////////////////////////////////////////////////////////////////////
122         //////////////////////////////////////////////////////////////////////
123         }