Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 43258 invoked from network); 19 Jan 2007 02:17:58 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 19 Jan 2007 02:17:58 -0000 Received: (qmail 61827 invoked by uid 500); 19 Jan 2007 02:18:05 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 61803 invoked by uid 500); 19 Jan 2007 02:18:05 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 61792 invoked by uid 99); 19 Jan 2007 02:18:04 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Jan 2007 18:18:04 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 18 Jan 2007 18:17:56 -0800 Received: by eris.apache.org (Postfix, from userid 65534) id 4EEEF1A981A; Thu, 18 Jan 2007 18:16:52 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r497686 - in /db/derby/code/trunk/java/demo/simple: SimpleApp.java example.html Date: Fri, 19 Jan 2007 02:16:52 -0000 To: derby-commits@db.apache.org From: myrnavl@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070119021652.4EEEF1A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 @@ - + Simple JDBC Application @@ -69,6 +69,7 @@ This example program is a very minimal JDBC application. JDBC is the primary API for interacting with Apache Derby. This program:

  • runs in either embedded mode (the default) or as a client in a server environment, depending on the arguments passed to the program. +
  • runs in J2ME or J2SE Java Virtual Machines.
  • starts up the Derby engine, if running in embedded mode
  • connects to the Derby Network Server, if running in client mode
  • creates and connects to a database @@ -81,7 +82,7 @@
  • shuts down Derby, if running in embedded mode

- 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. + 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.

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.