db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From myrn...@apache.org
Subject svn commit: r497686 - in /db/derby/code/trunk/java/demo/simple: SimpleApp.java example.html
Date Fri, 19 Jan 2007 02:16:52 GMT
Author: myrnavl
Date: Thu Jan 18 18:16:51 2007
New Revision: 497686

URL: http://svn.apache.org/viewvc?view=rev&rev=497686
Log:
DERBY-2216 - enable SimpleApp for J2ME

- patch contributed by Stephen More


Modified:
    db/derby/code/trunk/java/demo/simple/SimpleApp.java
    db/derby/code/trunk/java/demo/simple/example.html

Modified: db/derby/code/trunk/java/demo/simple/SimpleApp.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/demo/simple/SimpleApp.java?view=diff&rev=497686&r1=497685&r2=497686
==============================================================================
--- db/derby/code/trunk/java/demo/simple/SimpleApp.java (original)
+++ db/derby/code/trunk/java/demo/simple/SimpleApp.java Thu Jan 18 18:16:51 2007
@@ -56,6 +56,9 @@
     public String framework = "embedded";
     public String driver = "org.apache.derby.jdbc.EmbeddedDriver";
     public String protocol = "jdbc:derby:";
+    
+    public String username = "user1";
+    public String password = "user1";
 
     public static void main(String[] args)
     {
@@ -67,6 +70,14 @@
         /* parse the arguments to determine which framework is desired*/
         parseArguments(args);
 
+        /* check for J2ME specification - J2ME must use a DataSource further on */
+        String javaspec = System.getProperty( "java.specification.name" );
+        boolean java2me = false;
+        if( javaspec.indexOf( "J2ME" ) > -1 )
+        {
+            java2me = true;
+        }
+        
         System.out.println("SimpleApp starting in " + framework + " mode.");
 
         try
@@ -75,26 +86,56 @@
                The driver is installed by loading its class.
                In an embedded environment, this will start up Derby, since it is not already
running.
              */
-            Class.forName(driver).newInstance();
-            System.out.println("Loaded the appropriate driver.");
-
+            org.apache.derby.jdbc.EmbeddedSimpleDataSource ds = null;
             Connection conn = null;
             Properties props = new Properties();
-            props.put("user", "user1");
-            props.put("password", "user1");
+            props.put("user", username);
+            props.put("password", password);
 
-            /*
-               The connection specifies create=true to cause
-               the database to be created. To remove the database,
-               remove the directory derbyDB and its contents.
-               The directory derbyDB will be created under
-               the directory that the system property
-               derby.system.home points to, or the current
-               directory if derby.system.home is not set.
-             */
-            conn = DriverManager.getConnection(protocol +
+            /* If we are using a J2ME jvm, we need to use a DataSource, otherwise
+             * we can use java.sql.DriverManager to get the connection, or
+             * a Datasource. This example program uses a DataSource with J2ME
+             * but uses DriverManager otherwise.
+             * If we were to use a DataSource for J2SE, we could use
+             * the org.apache.derby.jdbc.EmbeddedDataSource, rather than the
+             * org.apache.derby.jdbc.EmbeddedSimpleDataSource we need to use for J2ME.
+             */
+         
+            if( java2me )
+            {
+                /*
+                   The connection specifies create in the DataSource settings for
+                   the database to be created. To remove the database,
+                   remove the directory derbyDB and its contents.
+                   The directory derbyDB will be created under
+                   the directory that the system property
+                   derby.system.home points to, or the current
+                   directory if derby.system.home is not set.
+                 */
+       
+                ds = new org.apache.derby.jdbc.EmbeddedSimpleDataSource();
+                ds.setDatabaseName("derbyDB");
+                ds.setCreateDatabase("create");
+                conn = ds.getConnection(username, password);
+            }
+            else
+            {
+                /*
+                   The connection specifies create=true in the url to cause
+                   the database to be created. To remove the database,
+                   remove the directory derbyDB and its contents.
+                   The directory derbyDB will be created under
+                   the directory that the system property
+                   derby.system.home points to, or the current
+                   directory if derby.system.home is not set.
+                 */
+          
+                Class.forName(driver).newInstance();
+                System.out.println("Loaded the appropriate driver.");
+            
+                conn = DriverManager.getConnection(protocol +
                     "derbyDB;create=true", props);
-
+            }
             System.out.println("Connected to and created database derbyDB");
 
             conn.setAutoCommit(false);
@@ -184,13 +225,32 @@
 
             if (framework.equals("embedded"))
             {
-                try
+                /* again, with J2ME, we need to use a datasource to get the connection */
+                if( java2me )
                 {
-                    DriverManager.getConnection("jdbc:derby:;shutdown=true");
+                    try
+                    {
+                        ds.setShutdownDatabase( "shutdown" );
+                        conn = ds.getConnection(username, password);
+                    }
+                    catch (SQLException se)
+                    {
+                        if( se.getErrorCode() == 45000 )
+                        {
+                            gotSQLExc = true;
+                        }
+                    }
                 }
-                catch (SQLException se)
-                {
-                    gotSQLExc = true;
+                else                  
+                {   
+                    try
+                    {
+                        DriverManager.getConnection("jdbc:derby:;shutdown=true");
+                    }
+                    catch (SQLException se)
+                    {
+                        gotSQLExc = true;
+                    }
                 }
 
                 if (!gotSQLExc)
@@ -249,4 +309,4 @@
             }
         }
     }
-}
+}
\ No newline at end of file

Modified: db/derby/code/trunk/java/demo/simple/example.html
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/demo/simple/example.html?view=diff&rev=497686&r1=497685&r2=497686
==============================================================================
--- db/derby/code/trunk/java/demo/simple/example.html (original)
+++ db/derby/code/trunk/java/demo/simple/example.html Thu Jan 18 18:16:51 2007
@@ -18,7 +18,7 @@
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
-<meta name="LASTUPDATED" content="06/15/05 12:10:42">
+<meta name="LASTUPDATED" content="07/18/01 06:12:42">
 <link rel="StyleSheet" href="../../csfull.css" type="text/css" media="screen">
 <title>Simple JDBC Application</title>
 
@@ -69,6 +69,7 @@
   <a name="820012"> </a>This example program is a very minimal JDBC application.
JDBC is the primary API for interacting with Apache Derby. This program:
 </p><ul class="Normal">  
   <li class="Normal">runs in either embedded mode (the default) or as a client in a
server environment, depending on the arguments passed to the program.
+  <li class="Normal">runs in J2ME or J2SE Java Virtual Machines.
   <li class="Normal"><a name="817266"> </a>starts up the Derby engine,
if running in embedded mode
   <li class="Normal"><a name="817266"> </a>connects to the Derby Network
Server, if running in client mode
   <li class="Normal"><a name="817275"> </a>creates and connects to a database
@@ -81,7 +82,7 @@
   <li class="Normal"><a name="817282"> </a>shuts down Derby, if running
in embedded mode
 </ul>
 <p class="Body">
-  <a name="817267"> </a>In embedded mode, the application starts up an instance
of Derby within the current Java Virtual Machine and shuts down the instance before it completes.
No network access is involved. Only one application can access a database at a time. 
+  <a name="817267"> </a>In embedded mode, the application starts up an instance
of Derby within the current Java Virtual Machine and shuts down the instance before it completes.
For a J2ME Java Virtual Machine an appropriate DataSource is used, otherwise, the java.sql.Driver
mechanism is used. No network access is involved. Only one application can access a database
at a time. 
 </p>
 <p>In a server environment, the application demonstrates the use of the Derby network
client or the IBM DB2 JDBC Universal Driver by connecting to the Network Server and running
the demo. Note that the client drivers allow multiple instances of the application to run
at the same time.  However, the SQL operations performed by this demo will cause failures
when multiple simultaneous instances of the application are run. Use of a client driver to
connect to the Network Server in this application is intended only to demonstrate this type
of connection. The SimpleApp demo is not suited for simultaneous executions because it creates
and drops the table on which it operates.
 </p>



Mime
View raw message