Atttached is the fix to the network server demos

Andrew McIntyre (JIRA) wrote:
     [ http://issues.apache.org/jira/browse/DERBY-247?page=all ]

Andrew McIntyre updated DERBY-247:
----------------------------------

    Attachment: simpleapp.diff

Hi Lance,

For this release, I would like to support JCC as an option in the demos. I have 
attached a diff for SimpleApp as an example, kindly provided to me by Stan 
Bradbury.



Network Server demo program should support Derby network client driver
----------------------------------------------------------------------

        Key: DERBY-247
        URL: http://issues.apache.org/jira/browse/DERBY-247
    Project: Derby
       Type: Improvement
 Components: Demos/Scripts
   Versions: 10.1.0.0
   Reporter: Andrew McIntyre
   Assignee: Lance Andersen
   Priority: Minor
    Fix For: 10.1.0.0
Attachments: simpleapp.diff

Currently, the Network Server demo programs require the IBM Universal JDBC 
Driver (JCC) for client functionality. The demo should be enhanced to also 
support using the Derby client driver.




--
Lance Andersen                       email: [EMAIL PROTECTED]
Sun Microsystems Inc.                phone: (781) 442-2037
1 Network Drive, UBUR02-301          fax  : (781) 442-1610
Burlington, MA 01803
Index: NsSample.java
===================================================================
--- NsSample.java       (revision 189970)
+++ NsSample.java       (working copy)
@@ -40,7 +40,8 @@
  The program:
 
  1.    starts the Derby Network Server
- 2.    loads the IBM DB2 JDBC Universal driver
+ 2.    loads the IBM DB2 JDBC Universal driver or derby client JDBC driver
+        (default is the derby client JDBC driver)
  3. creates the database if not already created
  4. checks to see if the schema is already created, and if not,
  5. creates the schema which includes the table SAMPLETBL and corresponding 
indexes.
@@ -65,6 +66,7 @@
 public class NsSample {
 
        public static final String DB2_JDBC_UNIVERSAL_DRIVER = new 
String("com.ibm.db2.jcc.DB2Driver");
+        public static final String DERBY_CLIENT_DRIVER = 
"org.apache.derby.jdbc.ClientDriver";
        public static int NUM_ROWS = 50; /* Number of rows to load initially */
        public static int ITERATIONS = 10;  //Each client does these many 
iterations
        public static int NUM_CLIENT_THREADS = 2;
@@ -80,19 +82,33 @@
        // This URL describes the target database for type 4 connectivity
        // Notice that the properties may be established via the URL syntax
        private static final String CS_NS_DBURL= 
"jdbc:derby:net://localhost:"+NETWORKSERVER_PORT+"/NSSampledb;create=true;retrieveMessagesFromServerOnGetMessage=true;deferPrepares=true;";
+        // URL for the Derby client JDBC driver.
+       private static final String DERBY_CLIENT_URL= 
"jdbc:derby://localhost:"+NETWORKSERVER_PORT+"/NSSampledb;create=true;deferPrepares=true";
 
+        // Default to using the Derby Client JDBC Driver for database 
connections
+        String url = DERBY_CLIENT_URL;
+        String jdbcDriver = DERBY_CLIENT_DRIVER;
+
        public static void main(String[] args) throws Exception {
 
+                   new nserverdemo.NsSample().startSample(args);
+        }
+       public void startSample(String[] args) throws Exception {
          NetworkServerUtil nwServer;
 
-         // DB2Connection provides additional functionality than 
java.sql.Connection
-         // One can use either depending on the requirements
          Connection conn = null;
 
          PrintWriter pw = null;
+          
+      
 
          try  {
+
+                // Determine which JDBC driver we are using with Derby
+                parseArguments(args);
+
                pw = new PrintWriter(System.out,true);  // to print messages
+               pw.println("Using JDBC driver: " + jdbcDriver);
 
                /* Start - In order to start the network server do the following
                   In case you want to start the server as a script or another 
program
@@ -138,11 +154,11 @@
                pw.println("[NsSample] Sample Derby Network Server program demo 
starting. ");
                pw.println("Please wait .....................");
 
-               // Load the JCC Driver
+               // Load the JDBC Driver
                try     {
-                       Class.forName(DB2_JDBC_UNIVERSAL_DRIVER).newInstance();
+                       Class.forName(jdbcDriver).newInstance();
                } catch (Exception e) {
-                       pw.println("[NsSample] Unable to load JCC driver. 
Following exception was thrown");
+                       pw.println("[NsSample] Unable to load the JDBC driver. 
Following exception was thrown");
                        e.printStackTrace();
                        System.exit(1);  //critical error, so exit
                  }
@@ -156,10 +172,10 @@
                properties.setProperty("user","cloud");
                properties.setProperty("password","scape");
 
-               // Get database connection using the JCC client via 
DriverManager api
+               // Get database connection via DriverManager api
                try     {
                        
-                       conn =  (Connection) 
DriverManager.getConnection(CS_NS_DBURL, properties);
+                       conn =  (Connection) DriverManager.getConnection(url, 
properties);
                } catch(Exception e) {
                        pw.println("[NsSample] Connection request unsuccessful, 
exception thrown was: ");
                        pw.println("[NsSample] Please check if all the jar 
files are in the classpath and the dbUrl is set correctly.");
@@ -191,7 +207,7 @@
                   Please be aware of the database URL for obtaining a client 
connection
                 */
                for (int i=1; i<NUM_CLIENT_THREADS; i++) {
-                       clientThreads[i] = new 
NsSampleClientThread(i+1,CS_NS_DBURL,properties,pw);
+                       clientThreads[i] = new 
NsSampleClientThread(i+1,url,properties,pw);
                        clientThreads[i].start();
 
                }
@@ -213,4 +229,34 @@
                if(pw != null) pw.close();
       }
         }
+
+    /**
+     * Determine which jdbc driver to use by parsing the command line args.
+     *  Accepted values:
+     *  jccjdbclient   - The DB2 type 4 universal driver
+     *  derbyclient    - The Derby network driver (default).
+     *  Note: because this is just a sample, we only care about whether
+     *  the above values are specified.  If they are not, then we default
+     *  to the Derby network driver.
+     */
+    private void parseArguments(String[] args)
+    {
+        int length = args.length;
+
+        for (int index = 0; index < length; index++)
+        {
+            if (args[index].equalsIgnoreCase("jccjdbcclient"))
+            {
+                jdbcDriver = DB2_JDBC_UNIVERSAL_DRIVER;
+                url = CS_NS_DBURL;
+                break;
+            } else if (args[index].equalsIgnoreCase("derbyClient"))
+            {
+                jdbcDriver = DERBY_CLIENT_DRIVER;
+                url = DERBY_CLIENT_URL;
+                break;
+            }
+        }
+    }
+
 }
Index: SimpleNetworkClientSample.java
===================================================================
--- SimpleNetworkClientSample.java      (revision 189970)
+++ SimpleNetworkClientSample.java      (working copy)
@@ -31,7 +31,8 @@
  * and interact with Derby Network Server
  *
  * In particular,this sample program
- * 1)   loads the DB2 Universal JDBC Driver
+ * 1)   loads the DB2 Universal JDBC Driver or the Derby Network Client driver
+   (default is the derby network client driver)
  * 2)  obtains a client connection using the Driver Manager
  * 3)  obtains a client connection using a DataSource
  * 4)  tests the database connections by executing a sample query
@@ -62,17 +63,35 @@
         */
        private static final String DB2_JDBC_UNIVERSAL_DRIVER = 
"com.ibm.db2.jcc.DB2Driver";
        private static final String DB2_JCC_DS = 
"com.ibm.db2.jcc.DB2SimpleDataSource";
+       /**
+        * Derby Network Client Driver class names
+        */
 
+public static final String DERBY_CLIENT_DRIVER = 
"org.apache.derby.jdbc.ClientDriver";
+       private static final String DERBY_CLIENT_DS = 
"org.apache.derby.jdbc.ClientDataSource";
+
        /**
         * This URL is used to connect to Derby Network Server using the 
DriverManager.
-        * Also, this url describes the target database for type 4 connectivity
+        * This URL is for the DB2 JDBC Universal Driver
         * Notice that the properties may be established via the URL syntax
         */
        private static final String CS_NS_DBURL= 
"jdbc:derby:net://localhost:"+NETWORKSERVER_PORT+"/"+DBNAME+";retrieveMessagesFromServerOnGetMessage=true;deferPrepares=true;";
 
+        // URL for the Derby client JDBC driver.
+        private static final String DERBY_CLIENT_URL= 
"jdbc:derby://localhost:"+ 
NETWORKSERVER_PORT+"/NSSampledb;create=true;deferPrepares=true";
 
-       public static void main (String[] args)
-               throws Exception
+        // Default to using the Derby Client JDBC Driver for database 
connections
+        String url = DERBY_CLIENT_URL;
+        String jdbcDriver = DERBY_CLIENT_DRIVER;
+        String jdbcDataSource = DERBY_CLIENT_DS;
+
+       public static void main (String[] args) throws Exception
+        {
+
+                   new SimpleNetworkClientSample().startSample(args);
+
+        }
+       public void startSample (String[] args) throws Exception
        {
                DataSource clientDataSource = null;
                Connection clientConn1 = null;
@@ -82,10 +101,11 @@
                try
                {
                        System.out.println("Starting Sample client program ");
+                        // Determine which JDBC driver to use
+                        parseArguments(args);
 
-                       // load DB2 JDBC UNIVERSAL DRIVER to enable client 
connections to
-                       // Derby Network Server
-                       loadJCCDriver();
+                       // load  the appropriate JDBC Driver
+                       loadDriver();
 
                        // get a client connection using DriverManager
                        clientConn1 = getClientDriverManagerConnection();
@@ -128,7 +148,7 @@
         * @return      returns database connection
         * @throws Exception if there is any error
         */
-       public static Connection getClientDataSourceConn(javax.sql.DataSource 
ds)
+       public Connection getClientDataSourceConn(javax.sql.DataSource ds)
                throws Exception
        {
                Connection conn = ds.getConnection("usr2", "pass2");
@@ -147,10 +167,10 @@
         * @return      returns DataSource
         * @throws Exception if there is any error
         */
-       public static javax.sql.DataSource getClientDataSource(String database, 
String user, String
+       public javax.sql.DataSource getClientDataSource(String database, String 
user, String
                                                                          
password) throws SQLException, ClassNotFoundException, InstantiationException, 
IllegalAccessException, NoSuchMethodException, InvocationTargetException
        {
-               Class nsDataSource = Class.forName(DB2_JCC_DS);
+               Class nsDataSource = Class.forName(jdbcDataSource);
                DataSource ds = (DataSource) nsDataSource.newInstance();
 
                // can also include Derby URL attributes along with the 
database name
@@ -180,10 +200,14 @@
                args = new Object[] {new Integer(1527)};
                portnumber.invoke(ds, args);
 
-               // driver type must be 4 to access Derby Network Server
-               Method drivertype = nsDataSource.getMethod("setDriverType", 
methodParams);
-               args = new Object[] {new Integer(4)};
-               drivertype.invoke(ds, args);
+                // The following is only applicable to the DB2 JDBC driver
+                if(jdbcDataSource.equals( DB2_JCC_DS))
+                {
+                       // driver type must be 4 to access Derby Network Server
+                       Method drivertype = 
nsDataSource.getMethod("setDriverType", methodParams);
+                       args = new Object[] {new Integer(4)};
+                       drivertype.invoke(ds, args);
+                }
 
                return ds;
 
@@ -191,21 +215,21 @@
 
 
        /**
-        * Load DB2 JDBC UNIVERSAL DRIVER
+        * Load the appropriate JDBC driver
         */
-       public static void loadJCCDriver()
+       public void loadDriver()
                throws Exception
        {
-               // Load the JCC Driver
-               Class.forName(DB2_JDBC_UNIVERSAL_DRIVER).newInstance();
+               // Load the  Driver
+               Class.forName(jdbcDriver).newInstance();
        }
 
        /**
         * Get a client connection using the DriverManager
-        * @pre DB2 JDBC Universal driver must have been loaded before calling 
this method
+        * @pre The JDBC driver must have been loaded before calling this method
         * @return Connection   client database connection
         */
-       public static Connection getClientDriverManagerConnection()
+       public Connection getClientDriverManagerConnection()
                throws Exception
        {
 
@@ -217,8 +241,8 @@
                properties.setProperty("user","cloud");
                properties.setProperty("password","scape");
 
-               // Get database connection using the JCC client via 
DriverManager api
-               Connection conn = 
DriverManager.getConnection(CS_NS_DBURL,properties); 
+               // Get database connection  via DriverManager api
+               Connection conn = DriverManager.getConnection(url,properties); 
 
                return conn;
        }
@@ -229,7 +253,7 @@
         * @param       conn    database connection
         * @throws Exception if there is any error
         */
-       public static void test(Connection conn)
+       public void test(Connection conn)
                throws Exception
        {
 
@@ -257,6 +281,38 @@
                        stmt.close();
          }
        }
+   /**
+     * Determine which jdbc driver to use by parsing the command line args.
+     *  Accepted values:
+     *  jccjdbclient   - The DB2 type 4 universal driver
+     *  derbyclient    - The Derby network driver (default).
+     *  Note: because this is just a sample, we only care about whether
+     *  the above values are specified.  If they are not, then we default
+     *  to the Derby network driver.
+     */
+    private void parseArguments(String[] args)
+    {
+        int length = args.length;
+
+        for (int index = 0; index < length; index++)
+        {
+            if (args[index].equalsIgnoreCase("jccjdbcclient"))
+            {
+
+                jdbcDriver = DB2_JDBC_UNIVERSAL_DRIVER;
+                jdbcDataSource = DB2_JCC_DS;
+                url = CS_NS_DBURL;
+                break;
+            } else if (args[index].equalsIgnoreCase("derbyClient"))
+            {
+                jdbcDriver = DERBY_CLIENT_DRIVER;
+                jdbcDataSource = DERBY_CLIENT_DS;
+                url = DERBY_CLIENT_URL;
+                break;
+            }
+        }
+    }
+
 }
 
 
Index: simpleserversample.html
===================================================================
--- simpleserversample.html     (revision 189970)
+++ simpleserversample.html     (working copy)
@@ -112,7 +112,7 @@
 <p>This program:</p>
 
 <ul>
- <li>loads the DB2 JDBC Universal Driver</li>
+ <li>loads the DB2 JDBC Universal Driver or the Derby Network Client JDBC 
Driver</li>
  <li>obtains a client connection using the Driver Manager</li>
  <li>obtains a client connection using a DataSource</li>
  <li>tests the database connections by executing a sample query</li>
@@ -134,12 +134,21 @@
  <li>Clients of Derby Network Server only need the following jar files in the 
CLASSPATH in order to connect to the Network Server.<span>  </span>Set the 
CLASSPATH to include the following jar files:
  <ul>
   <li>the current directory (".")</li>
+<li><tt>derbyclient.jar</tt><br>This jar file must be in your CLASSPATH to use 
t
+he Derby Client Network JDBC Driver.</li>
   <li><tt>db2jcc.jar</tt><br>This jar file must be in your CLASSPATH to use 
the DB2 JDBC Universal Driver.</li>
   <li><tt>db2jcc_license_c.jar</tt><br>This jar file is the license file for 
the Universal Driver for Derby.</li>
  </ul></li>
 <li>Once you have set up your environment correctly, execute the
 application from the <br><span 
class=italic>%DERBY_INSTALL%</span><tt>\demo\programs\nserverdemo\</tt> 
directory:
-<pre><tt>java SimpleNetworkClientSample</tt></pre>
+<pre><tt>java SimpleNetworkClientSample [driverType]</tt></pre>
+<ul>
+<li>Where the possible values for driverType are:
+<ul>
+<li>derbyClient (default)
+<li>jccjdbcclient
+</ul>
+</ul>
 </li>
 </ol>
 
Index: nserverdemo.html
===================================================================
--- nserverdemo.html    (revision 189970)
+++ nserverdemo.html    (working copy)
@@ -12,7 +12,7 @@
 <UL>
   <LI>starts the Derby Network Server 
   <LI>checks if the Derby Network Server is running 
-  <LI>loads the DB2 JDBC Universal Driver 
+  <LI>loads the DB2 JDBC Universal Driver or the Derby Client Network JDBC 
Driver
   <LI>creates the database '<tt>NsSampledb</tt>' if not already created 
   <LI>checks to see if the schema is already created, and if not, 
   creates the schema which includes the <tt>SAMPLETBL</tt> table and 
corresponding 
@@ -81,10 +81,11 @@
   class=italic>%DERBY_INSTALL%</SPAN><tt>\demo\programs\</tt> directory, where 
<SPAN 
   class=italic>%DERBY_INSTALL%</SPAN> is the directory where you installed 
Derby. 
   <LI>Set the CLASSPATH to the current directory (".") and also include the 
following 
-  jar files in order to use the Derby Network Server and the DB2 JDBC 
Universal Driver.
+  jar files in order to use the Derby Network Server and the DB2 JDBC 
Universal Driver or the Derby Client Network JDBC Driver.
 <ul>
 <li><tt>derbynet.jar</tt><br>The Network Server jar file. It must be in your 
CLASSPATH to use any of the Derby Network 
 Server functions.</li>
+<li><tt>derbyclient.jar</tt><br>This jar file must be in your CLASSPATH to use 
the Derby Client Network JDBC Driver.</li>
 <li><tt>db2jcc.jar</tt><br>This jar file must be in your CLASSPATH to use the 
 DB2 JDBC Universal Driver.</li>
 <li><tt>db2jcc_license_c.jar</tt><br>This jar file is the license file for the 
Universal 
@@ -96,7 +97,14 @@
 the DB2 JDBC Universal Driver along with their respective versions.</li>
 <li>Once you have set up your environment correctly, execute the application 
from the <br> <SPAN 
   class=italic>%DERBY_INSTALL%</SPAN><tt>\demo\programs\</tt> directory:<br>
-<pre>java nserverdemo.NsSample</pre>
+<pre>java nserverdemo.NsSample [driverType]</pre>
+<ul>
+<li>Where the possible values for driverType are:
+<ul>
+<li>derbyClient (default)
+<li>jccjdbcclient
+</ul>
+</ul>
 </li>
 </OL>
 You should receive output similar to the following if the 

Reply via email to