Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 76125 invoked from network); 26 Apr 2006 23:02:11 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 26 Apr 2006 23:02:11 -0000 Received: (qmail 40397 invoked by uid 500); 26 Apr 2006 23:02:10 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 40373 invoked by uid 500); 26 Apr 2006 23:02:10 -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 40362 invoked by uid 99); 26 Apr 2006 23:02:10 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Apr 2006 16:02:10 -0700 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 [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Wed, 26 Apr 2006 16:02:08 -0700 Received: (qmail 75857 invoked by uid 65534); 26 Apr 2006 23:01:47 -0000 Message-ID: <20060426230147.75855.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r397327 - in /db/derby/docs/trunk: ./ src/workingwithderby/ Date: Wed, 26 Apr 2006 23:01:45 -0000 To: derby-commits@db.apache.org From: jta@apache.org X-Mailer: svnmailer-1.0.8 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: jta Date: Wed Apr 26 16:01:44 2006 New Revision: 397327 URL: http://svn.apache.org/viewcvs?rev=397327&view=rev Log: DERBY-913 : Add chapters 3 and 4, update Ch 1 and 2, and update build.xml so the Working With Derby doc builds properly on case sensative systems like Linux. Committed patch by Stanley Bradbury . Added: db/derby/docs/trunk/src/workingwithderby/rwwdactivity3.dita (with props) db/derby/docs/trunk/src/workingwithderby/twwdactivity2.dita (with props) db/derby/docs/trunk/src/workingwithderby/twwdactivity3_Setup.dita (with props) Modified: db/derby/docs/trunk/build.xml db/derby/docs/trunk/src/workingwithderby/WorkingWithDerby.ditamap db/derby/docs/trunk/src/workingwithderby/cwwdactivities.dita db/derby/docs/trunk/src/workingwithderby/rwwdcopyright.dita db/derby/docs/trunk/src/workingwithderby/twwdIntro.dita db/derby/docs/trunk/src/workingwithderby/twwdactivity1.dita db/derby/docs/trunk/src/workingwithderby/twwdactivity1_Setup.dita Modified: db/derby/docs/trunk/build.xml URL: http://svn.apache.org/viewcvs/db/derby/docs/trunk/build.xml?rev=397327&r1=397326&r2=397327&view=diff ============================================================================== --- db/derby/docs/trunk/build.xml (original) +++ db/derby/docs/trunk/build.xml Wed Apr 26 16:01:44 2006 @@ -36,7 +36,7 @@ - + @@ -113,7 +113,7 @@ - + @@ -173,7 +173,7 @@ - + Modified: db/derby/docs/trunk/src/workingwithderby/WorkingWithDerby.ditamap URL: http://svn.apache.org/viewcvs/db/derby/docs/trunk/src/workingwithderby/WorkingWithDerby.ditamap?rev=397327&r1=397326&r2=397327&view=diff ============================================================================== --- db/derby/docs/trunk/src/workingwithderby/WorkingWithDerby.ditamap (original) +++ db/derby/docs/trunk/src/workingwithderby/WorkingWithDerby.ditamap Wed Apr 26 16:01:44 2006 @@ -1,6 +1,6 @@ + "map.dtd"> - + Apache Software Foundation -Working With Derby - partial (Intro and Activity 1) - +Working With Derby - (partial: Chap 1 - 4) + Apache Derby @@ -34,5 +34,10 @@ + + + + + Modified: db/derby/docs/trunk/src/workingwithderby/cwwdactivities.dita URL: http://svn.apache.org/viewcvs/db/derby/docs/trunk/src/workingwithderby/cwwdactivities.dita?rev=397327&r1=397326&r2=397327&view=diff ============================================================================== --- db/derby/docs/trunk/src/workingwithderby/cwwdactivities.dita (original) +++ db/derby/docs/trunk/src/workingwithderby/cwwdactivities.dita Wed Apr 26 16:01:44 2006 @@ -1,6 +1,6 @@ + "concept.dtd"> + +The WwdEmbedded program +This section describes the WwdEmbedded program, +highlighting details specific to accessing a Derby database from a JDBC program. + +WwdEmbedded.javacode descriptionorg.apache.derby.jdbc.EmbeddedDriver +DatabaseshutdownJDBC +SQLException + + +
The WwdEmbedded.java program is described here. +Most of the code related to the database activities performed is included +but you may find it helpful to open the program file and follow along in a +text viewer or editor. The SECTION NAMES referred to in this +text can be found in the comments within the program code and serve as cross-reference +points between this document and the java program. The program utilizes routines +from the WwdUtils class. The utility class code is not described +here but is available for review in the file WwdUtils.java
+
Initialize the program INITIALIZATION SECTION: +The initial lines of code identify the Java libraries used in the program +then set up the Java class WwdEmbedded and the main method +signature. Refer to a standard Java programming guide for information on these +program constructs.import java.sql.*; +public class WwdEmbedded +{ + public static void main(String[] args) + {
+
Define key variables and ObjectsDEFINE VARIABLES +SECTION: The initial lines of the main method define +the variables and Objects used in the program. This example uses variables +to store the information needed to connect to the Derby database. Using variables +for this information makes it easy to adapt the program to other configurations +and other databases.
    +
  • driver - stores the name of the Derby embedded driver.
  • +
  • dbName - stores the name of the database.
  • +
  • connectionURL - stores the Derby connection URL that +will be used to access the database.
  • +
  • createString - stores the SQL CREATE statement for +the WISH_LIST table .
  • +
String driver = "org.apache.derby.jdbc.EmbeddedDriver"; +String dbName="jdbcDemoDB"; +String connectionURL = "jdbc:derby:" + dbName + ";create=true"; +String createString = "CREATE TABLE WISH_LIST " + + "(WISH_ID INT NOT NULL GENERATED ALWAYS AS IDENTITY " + ... + + " WISH_ITEM VARCHAR(32) NOT NULL) " ;
+
Start the Derby engineLOAD DRIVER SECTION: +Loading the Derby embedded JDBC driver starts the Derby database engine. The try and catch block +(the Java error handling construct) catches the exceptions that may occur. +A problem here is generally due to an incorrect CLASSPATH setting. String driver = "org.apache.derby.jdbc.EmbeddedDriver"; +... +try{ + Class.forName(driver); +} catch(java.lang.ClassNotFoundException e) { +... +}
+
Boot the databaseBOOT DATABASE SECTION: +The DriverManager class loads the database using the Derby connection URL +stored in the variable connectionURL. This URL includes the +parameter ;create=true so the database will be created +if it does not already exist. The primary try and catch +block begins here. This construct handles errors for the database access code +.String connectionURL = "jdbc:derby:" + dbName + ";create=true"; +... +try { + conn = DriverManager.getConnection(connectionURL); +... <most of the program code is contained here> +} catch (Throwable e) { +... +}
+
Set up to execute SQLINITIAL SQL SECTION: +Program objects needed to perform subsequent SQL operations are initialized +here and a check is made to see if the required data table exists.

The statement +object s is initialized. If the utility method WwdUtils.wwdChk4Table does +not find the WISH_LIST table it is create by executing +the SQL stored in the variable createString via this statement +object.

s = conn.createStatement(); +if (! WwdUtils.wwdChk4Table(conn)) +{ + System.out.println (" . . . . creating table WISH_LIST"); + s.execute(createString); +}The insert statement used to add data to the table is bound to +the prepared statement object psInsert. The prepared statement +uses the ? parameter to represent the data that will be inserted +by the user. The actual value that is inserted is set later +in the code prior to executing the SQL. This is the most efficient way to +execute SQL statements that will be used multiple times.psInsert = conn.prepareStatement + ("insert into WISH_LIST(WISH_ITEM) values (?)"); +
+
Interact with the DatabaseADD / DISPLAY RECORD +SECTION: This section uses the utility method WwdUtils.getWishItem to +gather information from the User. It then utilizes the objects setup previously +to insert the data into the WISH_LIST table and then display +all records. A standard do loop causes the program to repeat +this series of steps until exit is entered. The data +related activities performed in this section are:

The setString method +sets the substitution parameter of the psInsert object to +the value entered by the User then executeUpdate is called +to perform the database insert.psInsert.setString(1,answer); +psInsert.executeUpdate(); +

The statement object s is used to select +all the records in the WISH_LIST table and store them in +the ResultSet myWishes.myWishes = s.executeQuery("select ENTRY_DATE, WISH_ITEM + from WISH_LIST order by ENTRY_DATE"); +The while loop reads each record in turn by calling +the next method. The getTimestamp and getString methods +return specific fields in the record in the proper format. The fields are +displayed using rudimentary formatting.while (myWishes.next()) +{ + System.out.println("On " + myWishes.getTimestamp(1) + + " I wished for " + myWishes.getString(2)); +}Close the ResultSet to release the memory being used.myWishes.close(); +

+
Shutdown the DatabaseDATABASE SHUTDOWN SECTION: +When an application starts the Derby engine it should shutdown all databases +prior to exiting. The attribute ;shutdown=true in the Derby +connection URL performs the shutdown. The shutdown process cleans up records +in the transaction log to insure a faster startup the next time the database +is booted.

This section verifies that the embedded driver is being used +then issues the shutdown command and catches the shutdown exception to confirm +the database shutdown cleanly. The shutdown status is displayed before the +program exits.

if (driver.equals("org.apache.derby.jdbc.EmbeddedDriver")) { + boolean gotSQLExc = false; + try { + DriverManager.getConnection("jdbc:derby:;shutdown=true"); + } catch (SQLException se) { + if ( se.getSQLState().equals("XJ015") ) { + gotSQLExc = true; + } + } + if (!gotSQLExc) { + System.out.println("Database did not shut down normally"); + } else { + System.out.println("Database shut down normally"); + } +}The XJ015 error is the +only exception thrown by Derby that signifies an operation succeeded. All +other exceptions indicate an operation failed.
+
The errorPrint and SQLExceptionPrint methodsDERBY +EXCEPTION REPORTING CLASSES: The two methods at the end of the file, errorPrint and SQLExceptionPrint, +are generic exception reporting routines that can be used with any JDBC program. +This type of exception handling is required because often multiple exceptions +(SQLException) are chained together then thrown. A while loop +is used to report on each error in the chain. These classes are used by calling +the errorPrint method from the catch block +of the code that accesses the database. // Beginning of the primary catch block: uses errorPrint method +} catch (Throwable e) { + /* Catch all exceptions and pass them to + ** the exception reporting method */ + System.out.println(" . . . exception thrown:"); + errorPrint(e); +}The errorPrint routine prints a stack trace +for all exceptions except a SQLException. All SQLExceptions +are passed to the SQLExceptionPrint method.static void errorPrint(Throwable e) { + if (e instanceof SQLException) + SQLExceptionPrint((SQLException)e); + else { + System.out.println("A non SQL error occured."); + e.printStackTrace(); + } +} // END errorPrint The SQLException method +iterates through each of the exceptions on the stack. For each error the codes, +message then stacktrace are printed. // Iterates through a stack of SQLExceptions +static void SQLExceptionPrint(SQLException sqle) { + while (sqle != null) { + System.out.println("\n---SQLException Caught---\n"); + System.out.println("SQLState: " + (sqle).getSQLState()); + System.out.println("Severity: " + (sqle).getErrorCode()); + System.out.println("Message: " + (sqle).getMessage()); + sqle.printStackTrace(); + sqle = sqle.getNextException(); + } +} // END SQLExceptionPrint If you wish to see the output produced +by this method enter a wish list item with more than 32 characters like: I +wish to see a JAVA program fail.
+
+
Propchange: db/derby/docs/trunk/src/workingwithderby/rwwdactivity3.dita ------------------------------------------------------------------------------ svn:eol-style = native Modified: db/derby/docs/trunk/src/workingwithderby/rwwdcopyright.dita URL: http://svn.apache.org/viewcvs/db/derby/docs/trunk/src/workingwithderby/rwwdcopyright.dita?rev=397327&r1=397326&r2=397327&view=diff ============================================================================== --- db/derby/docs/trunk/src/workingwithderby/rwwdcopyright.dita (original) +++ db/derby/docs/trunk/src/workingwithderby/rwwdcopyright.dita Wed Apr 26 16:01:44 2006 @@ -1,6 +1,6 @@ + "reference.dtd"> + +Activity 2: Run SQL using the Client Driver +This activity uses Derby within a Client-Server configuration by +using the Network Server. The ij tool is the client application +that connects to the Derby Network Server. A database called seconddb is +created and some basic SQL commands are executed. + +Network ServerClient Driver + + + +This activity assumes that you know how to open a command shell, change +directory (cd) to the DERBYDBS directory +and set the DERBY_HOME environment variable. +Two command windows (labelled Shell-1 and Shell-2) +are used in this activity. Shell-1 is used to start the Derby +Network Server and display Network Server messages. Shell-2 is +used to establish a client connection to the Network Server using ij and +then perform some basic SQL operations. + +Open a command window that we'll call Shell-1. +Change directory (cd) to the DERBYDBS directory +and set the DERBY_HOME environment variable. +Start the Network Server.
+
On Windows platforms:
+
java -jar %DERBY_HOME%\lib\derbynet.jar start +Server is ready to accept connections on port 1527.
+
+
On UNIX Korn Shell platforms:
+
java -jar $DERBY_HOME/lib/derbynet.jar start +Server is ready to accept connections on port 1527.
+
+
A Network Server startup message is displayed +in the Shell-1 command window.
+Open another command window that we'll call Shell-2. +Change directory (cd) to the DERBYDBS directory +and set the DERBY_HOME environment variable. +Start ij.
+
On Windows platforms:
+
java -jar %DERBY_HOME%\lib\derbyrun.jar ij +ij version 10.2 +
+
+
On UNIX Korn Shell platforms:
+
java -jar $DERBY_HOME/lib/derbyrun.jar ij +ij version 10.2
+
All subsequent commands are entered from +the network client, and are therefore entered in the Shell-2 command +window.
+Create and open a connection to the database using the client driver. +CONNECT 'jdbc:derby://localhost:1527/seconddb;create=true'; +A client connection URL contains a hostname +and a port number: //localhost:1527/. +Create a table with two columns (ID and NAME) +using SQL.CREATE TABLE SECONDTABLE + (ID INT PRIMARY KEY, + NAME VARCHAR(14)); +0 rows inserted/updated/deleted + +Insert three records into the table.INSERT INTO SECONDTABLE VALUES + (100,'ONE HUNDRED'),(200,'TWO HUNDRED'),(300,'THREE HUNDRED'); +3 rows inserted/updated/deleted + +Select all of the records in the table.SELECT * FROM SECONDTABLE; + ID |NAME + ------------------------ + 100 |ONE HUNDRED + 200 |TWO HUNDRED + 300 |THREE HUNDRED + +3 rows selected +Select a subset of records from the table by qualifying the command. +ij> SELECT * FROM SECONDTABLE WHERE ID=200; + ID |NAME + ------------------------ + 200 |TWO HUNDRED + +1 row selected +Exit ij.exit; + +Shut down the Derby Network Server.
+
On Windows platforms:
+
java -jar %DERBY_HOME%\lib\derbynet.jar shutdown +Shutdown successful. +
+
+
On UNIX Korn Shell platforms:
+
java -jar $DERBY_HOME/lib/derbynet.jar shutdown +Shutdown successful. +
+
The server shutdown confirmation appears +in both command windows.
+
+Activity notes

The client connection URL contains network +information (a hostname and portnumber) not found in the URL for an embedded +connection. This information tells the client driver the "location" of the +Network Server. The client driver sends requests to and receives responses +from the Network Server.

In this activity the Derby database engine +is embedded in the Network Server and returns data to the ij client +(a client/server configuration). In contrast, establishing a connection using +an embedded URL (one without //localhost:1527/) would have +caused the Derby engine to be embedded in the ij application +(an embedded configuration).

Network Server start up and shutdown messages +are written to the derby.log log file along with the standard +database engine messages. For example:Server is ready to accept connections on port 1527. + ...( database engine messages not shown )... +Shutdown successful.

Though +not demonstrated here, multiple client programs can connect to Network Server +and access the database simultaneously in this configuration.

+
+
Propchange: db/derby/docs/trunk/src/workingwithderby/twwdactivity2.dita ------------------------------------------------------------------------------ svn:eol-style = native Added: db/derby/docs/trunk/src/workingwithderby/twwdactivity3_Setup.dita URL: http://svn.apache.org/viewcvs/db/derby/docs/trunk/src/workingwithderby/twwdactivity3_Setup.dita?rev=397327&view=auto ============================================================================== --- db/derby/docs/trunk/src/workingwithderby/twwdactivity3_Setup.dita (added) +++ db/derby/docs/trunk/src/workingwithderby/twwdactivity3_Setup.dita Wed Apr 26 16:01:44 2006 @@ -0,0 +1,91 @@ + + + + +Activity 3: Run a JDBC program using the Embedded driver +This activity loads the Derby database engine using a simple Java +JDBC program. JDBC is the Java Database Connectivity API and is also the native +API for Derby. The program uses the embedded driver to create (if needed) +and then connect to the jdbcDemoDB database. You can then +populate a table within the database with text. The program demonstrates some +basic JDBC processing along with related error handling. + +WwdEmbedded.javarunning +CLASSPATH + + +This activity assumes that you have opened a command window, navigated +to the DERBYDBS directory, and set the DERBY_HOME environment +variable. +The CLASSPATH environment variable is used by +Java to locate the binary files (jarfiles and classfiles) +needed to run Derby and other Java applications. Before performing this activity, +you need to set the CLASSPATH and compile the WwdEmbedded.java Java +program. + +Copy the program files into the DERBYDBS directory +and set the CLASSPATH:
+
On Windows platforms:
+
copy %DERBY_HOME%\demo\workingwithderby\* . +set CLASSPATH=%DERBY_HOME%\lib\derby.jar;. +
+
+
On UNIX Korn Shell platforms:
+
cp $DERBY_HOME/demo/workingwithderby/* . +export CLASSPATH=$DERBY_HOME/lib/derby.jar:. +
+
Include the dot (.) at the end +of each command so that your current working directory is included in the CLASSPATH and +the files are copied to the correct location.
+Compile the WwdEmbedded.java program:javac WwdEmbedded.java +Only a command prompt will be displayed +if the compilation is successful. The binary file WwdEmbedded.class will +be created. If an error is displayed please verify that the Java development +kit is properly installed. +Run the program:The WwdEmbedded.java program +populates a table with wish list items. It is a simple Java program that prompts +the User for text input (up to 32 characters), stores the text entered in +a database table and then lists the items stored in the table. The program +will continue to ask for wish list items until the word exit is +entered or a problem is encountered. Some basic information on program progress +is displayed at the beginning and the end of the program.java WwdEmbedded +org.apache.derby.jdbc.EmbeddedDriver loaded. +Connected to database jdbcDemoDB + . . . . creating table WISH_LIST +Enter wish-list item (enter exit to end): +a peppermint stick + _________________________________________________________ +On 2006-09-21 15:11:50.412 I wished for a peppermint stick + ________________________________________________________ +Enter wish-list item (enter exit to end): +an all expenses paid vacation + ________________________________________________________ +On 2006-09-21 15:11:50.412 I wished for a peppermint stick +On 2006-09-21 15:12:47.024 I wished for an all expenses paid vacation + ________________________________________________________ +Enter wish-list item (enter exit to end): +exit +Closed connection +Database shut down normally +Working With Derby JDBC program ending. + +
+
+
Propchange: db/derby/docs/trunk/src/workingwithderby/twwdactivity3_Setup.dita ------------------------------------------------------------------------------ svn:eol-style = native