Debian Codegraph @

java/demo/simple/example.html

raw symbols SCIP
1 <!--
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 -->
17 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
18 <html lang="en">
19 <head>
20 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
21 <meta name="LASTUPDATED" content="2007-11-20 13:04:00 UTC">
22 <link rel="StyleSheet" href="../csfull.css" type="text/css" media="screen">
23 <title>Simple JDBC Application</title>
24 </head>
25 <body>
26 <h1 class="Title">
27 Simple JDBC Application
28 </h1>
29
30 <!-- ########## TABLE OF CONTENTS ########## -->
31 <ul class="ChapterTOC">
32 <li class="ChapterTOC"><a href="#overview">Overview</a>
33 <li class="ChapterTOC"><a href="#included">What's Included?</a>
34 <li class="ChapterTOC"><a href="#runembedded">How to run this sample application in an embedded environment</a>
35 <li class="ChapterTOC"><a href="#runclientserver">How to run this sample application in a client/server environment</a>
36 <ul class="SubList">
37 <li class="ChapterToc"><a href="#server">Starting the Network Server</a>
38 <li class="ChapterToc"><a href="#derbyclient">Using the Derby client driver</a>
39 </ul>
40 </ul>
41
42 <!-- ########## OVERVIEW ########## -->
43 <h2 class="Heading2"><a id="overview" name="overview">Overview</a>
44 </h2>
45 <p>
46 This example program is a minimal JDBC application written in Java. JDBC is
47 the primary API for interacting with Apache Derby using Java. This program:
48 </p>
49 <ol class="Normal">
50 <li class="Normal">Runs in either embedded mode (the default) or as a client in a client/server environment, depending on the arguments passed to the program
51 <li class="Normal">Starts up the Derby engine, if running in embedded mode
52 <li class="Normal">Connects to the Derby Network Server, if running in client mode
53 <li class="Normal">Creates and connects to a database
54 <li class="Normal">Creates a table
55 <li class="Normal">Inserts data
56 <li class="Normal">Updates data
57 <li class="Normal">Selects data
58 <li class="Normal">Drops a table
59 <li class="Normal">Disconnects
60 <li class="Normal">Shuts down the database, if running in embedded mode
61 </ol>
62 <p>
63 This program is intended to run in Virtual Machines for the Java Platform
64 supporting Java SE 6 or newer.
65 </p>
66 <p>
67 In embedded mode, the application starts up an instance of Derby within the
68 current Java Virtual Machine (JVM) and shuts down the instance before it
69 completes. No network access is involved. Only one JVM can access a database
70 at a time.
71 </p>
72 <p>In a client/server environment, the application demonstrates the
73 use of the Derby network client driver by
74 connecting to the Network Server and running the demo. Note that the client
75 drivers allow multiple instances of the application to run at the same time.
76 However, the SQL operations performed by this demo will cause failures when
77 multiple simultaneous instances of the application are run. Use of a client
78 driver to connect to the Network Server in this application is intended only to
79 demonstrate this type of connection. The SimpleApp demo is not suited for
80 simultaneous executions because it creates and drops the table on which it
81 operates.
82 </p>
83 <p>
84 <em>Note that in this document, file paths and environment variables are
85 referenced using UNIX notation unless noted otherwise. This means that if, for
86 example, you are using a Windows platform, you must substitute file separators,
87 path separators and environment variable references to the Windows-specific
88 notation, for example:</em>
89 </p>
90 <table summary="This table provides UNIX and Windows examples of file and
91 path separators and environment variable references." class="simple">
92 <tr>
93 <th class="heading" id="c1">Element</td>
94 <th class="heading" id="c2">Example, UNIX</td>
95 <th class="heading" id="c3">Example, Windows</td>
96 </tr>
97 <tr>
98 <td class="listItem" headers="c1">File separator</td>
99 <td class="listItem" headers="c2"><em class="fileName">/home/path/file</em></td>
100 <td class="listItem" headers="c3"><em class="fileName">c:\path\file</em></td>
101 </tr>
102 <tr>
103 <td class="listItem" headers="c1">(CLASS)PATH separator</td>
104 <td class="listItem" headers="c2"><var class="envVar">derby.jar:derbytools.jar</var></td>
105 <td class="listItem" headers="c3"><var class="envVar">derby.jar;derbytools.jar</var></td>
106 </tr>
107 <tr>
108 <td class="listItem" headers="c1">Environment Variable reference</td>
109 <td class="listItem" headers="c2"><var class="envVar">$DERBY_HOME</var></td>
110 <td class="listItem" headers="c3"><var class="envVar">%DERBY_HOME%</var></td>
111 </tr>
112 </table>
113 <p>
114 <em>Also note that this document refers to the JVM launch command as
115 <var class="command">java</var>, and that it is being assumed that the JVM
116 installation's launcher is available via the system's
117 <em class="envVar">PATH</em>. Refer to the documentation of your JVM /
118 runtime environment for details on how to run a Java program using that JVM.
119 </em>
120 </p>
121 <h3>Modifying the application code</h3>
122 <p>
123 If you decide to modify the demo application code, or use it as a model for
124 your own JDBC application, be aware that any change may affect the behavior
125 of the program and the results from running it.
126 For example, the data verification code is very specific to the data this
127 demo inserts into the database, and will not work without modification with
128 other databases.
129 </p>
130 <p>
131 For more information about how to use Derby, refer to the included
132 documentation and the
133 <a href="http://db.apache.org/derby" target="_new">Apache Derby web site</a>.
134 </p>
135 <p>
136 If you are relatively new to databases or JDBC, the
137 <a href="http://docs.oracle.com/javase/tutorial/jdbc/basics/index.html" target="_new">Oracle
138 JDBC basics tutorial</a> may be a good starting point.
139 </p>
140
141
142 <!-- ########## WHAT'S INCLUDED ########## -->
143 <h2 class="Heading2"><a id="included" name="included">What's Included?</a>
144 </h2>
145 <p>
146 Before running this demo, you should see the following files and directories
147 in the <em class="fileName">demo/programs/simple/</em> directory:
148 </p>
149 <ul class="Normal">
150 <li class="Normal"><em class="fileName">example.html</em>
151 <p class="BodyRelative">
152 This file.
153 </p>
154 <li class="Normal"><em class="fileName">
155 <a href="SimpleApp.java" target="_top">SimpleApp.java</a></em>
156 <p class="BodyRelative">
157 Source code for the example program.
158 <em class="Emphasis">Examine this file to see how the application behaves
159 in the various environments</em>.
160 </p>
161 <li class="Normal"><em class="fileName">
162 <a href="derby.properties" target="_top">derby.properties</a></em>
163 <p class="BodyRelative">
164 Properties file for the Derby system. This demo program runs with default
165 settings, but you can use this file to tune Derby if you want to.
166 </p>
167 <li class="Normal"><em class="fileName">SimpleApp.class</em>
168 <p class="BodyRelative">
169 The compiled class file, runnable by Java SE JVMs.
170 </p>
171 </ul>
172
173 <p>
174 After running the demo, you will see some new files and directories. These
175 will be located in the directory that the system property
176 <var class="property">derby.system.home</var>
177 points to, or the current directory (<var class="property">user.dir</var>) if
178 <var class="property">derby.system.home</var> is not set for the embedded or
179 Network Server JVM. If you are following the instructions on this page, you
180 will find the new files in the directory
181 <em class="fileName">demo/programs/simple/</em>, relative to this Derby
182 installation. New files are:
183 </p>
184 <ul class="Normal">
185 <li class="Normal"><em class="fileName">derbyDB</em> (directory)
186 <p class="BodyRelative">
187 The directory that makes up the <em class="fileName">derbyDB</em> database.
188 You must not modify anything in this directory, or you will corrupt the
189 database. The directory was created when the application connected with
190 Derby, using the attribute <em class="Emphasis">create=true</em> in the
191 database connection URL. The database name, <em class="Emphasis">derbyDB</em>,
192 was also set in the database connection URL.
193 </p>
194 <ul class="Normal">
195 <li class="Normal"><em class="fileName">derbyDB/log</em> (directory)
196 <p class="BodyRelative">
197 The directory that holds the database transaction logs for the
198 <em class="fileName">derbyDB</em> database.
199 </p>
200 <li class="Normal"><em class="fileName">derbyDB/seg0</em> (directory)
201 <p class="BodyRelative">
202 The directory that holds the data for the <em class="fileName">derbyDB</em>
203 database.
204 </p>
205 <li class="Normal"><em class="fileName">derbyDB/service.properties</em>
206 <p class="BodyRelative">
207 An internal file that holds boot-time configuration parameters for the
208 <em class="fileName">derbyDB</em> database; do not edit.
209 </p>
210 </ul>
211 <li class="Normal"><em class="fileName">derby.log</em>
212 <p class="BodyRelative">
213 The log file with Derby progress and error messages.
214 </p>
215 </ul>
216 <p>
217 To remove the effects of running the demo program, simply delete the database
218 directory (<em class="fileName">derbyDB</em>) and
219 <em class="fileName">derby.log</em>. Note that if you are running the demo in
220 a client/server environment you most likely need to shut down the Derby
221 Network Server before being able to delete the database directory.
222
223 <!-- ########## HOW TO RUN - EMBEDDED ########## -->
224 <h2 class="Heading2"><a id="runembedded" name="runembedded">How to run this sample application in an embedded environment</a>
225 </h2>
226 <p>
227 The Derby embedded driver is a JDBC driver included in binary distributions of
228 Apache Derby, in the directory <em class="fileName">$DERBY_HOME/lib/</em>.
229 The embedded driver should be used when you want to run Derby in the same
230 JVM as your application, for example when no direct network access to the
231 database system is needed.
232 </p>
233 <table class="listing">
234 <tr>
235 <td class="listItem">Class name:</td>
236 <td class="listItem"><em class="javaObject">org.apache.derby.jdbc.EmbeddedDriver</em></td>
237 </tr>
238 <tr>
239 <td class="listItem">Library:</td>
240 <td class="listItem"><em class="fileName">derby.jar</em></td>
241 </tr>
242 </table>
243 <p>&nbsp;</p>
244 <ol class="decimal">
245 <li class="Normal">Open a command window.
246 <li class="Normal">If you haven't set it already on a system-wide basis, set
247 the <var class="envVar">DERBY_HOME</var> environment variable to the location
248 of this Derby installation. This is not strictly required to run the demo, but
249 this environment variable will be used later on this page to refer to the
250 required Derby resources, files, etc. Examples:
251 <p class="BodyRelative">UNIX (ksh/bash)</p>
252 <p class="commandLine">export DERBY_HOME=/home/user/derby/db-derby-10.x.y.z-bin</p>
253 <p class="BodyRelative">Windows:</p>
254 <p class="commandLine">set DERBY_HOME=c:\programs\derby\db-derby-10.x.y.z-bin</p>
255 <li class="Normal">Change directories to the <em class="fileName">$DERBY_HOME/demo/programs/simple</em> directory.
256 <li class="Normal">In the command window, set the CLASSPATH to include the
257 current directory (the location of <em class="fileName">SimpleApp.class</em>)
258 and Derby's embedded driver library (<em class="fileName">derby.jar</em>).
259 (You may skip this step and provide the classpath as an option to the JVM
260 launch command instead, refer to your JVM's documentation for details).
261 <p class="BodyRelative">This may be done as follows:</p>
262 <p class="BodyRelative">UNIX (ksh/bash):</p>
263 <p class="commandLine">export CLASSPATH=.:${DERBY_HOME}/lib/derby.jar</p>
264 <p class="BodyRelative">WINDOWS:</p>
265 <p class="commandLine">set CLASSPATH=.;%DERBY_HOME%\lib\derby.jar</p>
266 <li class="Normal">(Optional) Run Derby's Sysinfo utility for testing the
267 classpath for an embedded environment. You should provide the arguments
268 <em class="Emphasis">embedded</em> (to indicate an embedded environment) and
269 <em class="Emphasis">SimpleApp.class</em> to test for the presence of the
270 <em class="javaObject">SimpleApp</em> class.
271 <p class="BodyRelative">
272 You run the utility like this:
273 </p>
274 <p class="CommandLine">
275 java org.apache.derby.tools.sysinfo -cp <em class="Emphasis">arguments</em>
276 </p>
277 <p class="BodyRelative">
278 So for the arguments you need here, run it like this (all on one line):
279 </p>
280 <p class="CommandLine">
281 java org.apache.derby.tools.sysinfo -cp embedded SimpleApp.class
282 </p>
283 <p class="BodyRelative">
284 If your environment is set up correctly, the utility shows output indicating
285 success. It looks like this:
286 </p>
287 <pre class="Output">
288 FOUND IN CLASS PATH:
289 Derby embedded engine library (derby.jar)
290 /home/user/derby/db-derby-10.x.y.z-bin/lib/derby.jar
291
292
293 user-specified class (SimpleApp)
294 /home/user/derby/db-derby-10.x.y.z-bin/demo/programs/simple
295
296
297 SUCCESS: All Derby related classes found in class path.
298 </pre>
299 <p class="BodyRelative">
300 If something is missing from your classpath environment, the utility
301 indicates what is missing. For example, if you neglected to add the
302 directory containing the SimpleApp class to your classpath, the utility
303 would indicate as such:
304 </p>
305 <pre class="Output">
306 FOUND IN CLASS PATH:
307
308 Derby embedded engine library (derby.jar)
309 /home/user/derby/db-derby-10.x.y.z-bin/lib/derby.jar
310
311
312
313 NOT FOUND IN CLASS PATH:
314
315 user-specified class (SimpleApp)
316 (SimpleApp not found.)
317 </pre>
318 <li class="Normal">Once you have your environment set up correctly, execute
319 the application from the same directory (<em class="fileName">demo/programs/simple</em>):
320 <p class="CommandLine">java SimpleApp</p>
321 The demo program's default framework is embedded, so there is no need to
322 specify this explicitly.
323 <p class="BodyRelative">A successful run produces the following output:</p>
324 <pre class="Output">
325 SimpleApp starting in embedded mode
326 Connected to and created database derbyDB
327 Created table location
328 Inserted 1956 Webster
329 Inserted 1910 Union
330 Updated 1956 Webster to 180 Grand
331 Updated 180 Grand to 300 Lakeshore
332 Verified the rows
333 Dropped table location
334 Committed the transaction
335 Derby shut down normally
336 SimpleApp finished
337 </pre>
338 <p class="BodyRelative">
339 If any error messages appear, and you are unable to resolve the error(s),
340 ask for help on the derby-user
341 <a href="http://db.apache.org/derby/derby_mail.html" target="_new">mailing list</a>.
342 </p>
343 </ol>
344
345 <!-- ########## HOW TO RUN - CLIENT/SERVER ########## -->
346 <h2 class="Heading2"><a id="runclientserver" name="runclientserver">How to run this sample application in a client/server environment</a>
347 </h2>
348 <p>You will need to set up both the client process and the server
349 process to run the demo application as a client connecting to the Network server.
350 The Network Server needs to be running before you can connect using a client
351 driver. This demo includes support for the Derby client driver.
352 </p>
353 <p>You must start the Network Server before trying to run the
354 demo application in one of the client modes.
355
356 <!-- ########## HOW TO RUN - SERVER ########## -->
357 <h3><a id="server" name="server">Starting the Network Server</a></h3>
358 <ol class="decimal">
359 <li class="Normal">Open a command window for the server.
360 <li class="Normal">If you haven't set it already on a system-wide basis, set
361 the <var class="envVar">DERBY_HOME</var> environment variable to the location of this Derby installation.
362 This is not strictly required to run the demo, but this environment variable
363 will be used later on this page to refer to the required Derby resources,
364 files, etc.
365 <p class="BodyRelative">Examples:</p>
366 <p class="BodyRelative">UNIX (ksh/bash):</p>
367 <p class="commandLine">export DERBY_HOME=/home/user/derby/db-derby-10.x.y.z-bin</p>
368 <p class="BodyRelative">Windows:</p>
369 <p class="commandLine">set DERBY_HOME=c:\programs\derby\db-derby-10.x.y.z-bin</p>
370 <li class="Normal">Change directories to the <em class="fileName">$DERBY_HOME/demo/programs/simple</em> directory.
371 <li class="Normal">In the command window for the server, start the Network Server
372 by running the executable jar file <em class="fileName">derbyrun.jar</em>.
373 <p class="BodyRelative">Examples:</p>
374 <p class="BodyRelative">UNIX (ksh/bash):
375 <p class="commandLine">java -jar $DERBY_HOME/lib/derbyrun.jar server start</p>
376 <p class="BodyRelative">Windows:</p>
377 <p class="commandLine">java -jar %DERBY_HOME%\lib\derbyrun.jar server start</p>
378 <p class="BodyRelative">When the server starts, the server console output will
379 resemble something like this:
380 </p>
381 <pre class="Output">
382 Security manager installed using the Basic server security policy.
383 Apache Derby Network Server - &lt;version&gt; - (&lt;svnrevision&gt;) started and ready to accept connections on port 1527 at &lt;timestamp&gt;
384 </pre>
385 <p class="BodyRelative">(When you are done with the demo, you may shut down the network server for example by
386 passing the arguments <var class="command">server shutdown</var> to
387 <em class="fileName">derbyrun.jar</em> in a new command window.)
388 </p>
389 <table cellSpacing=2 cellPadding=3 bgColor=silver border=0>
390 <tbody>
391 <tr vAlign=top>
392 <td class=BoxTable>
393 <h3 class=BoxHead>A note on running the Network Server</h3>
394 <p class=CellBodySmall>You may start the server in a number of other ways,
395 including:</p>
396 <ul class="boxed">
397 <li>Invoking the Network Server's main class from the command line.
398 <p class="CommandLine">java -cp $DERBY_HOME/lib/derbynet.jar org.apache.derby.drda.NetworkServerControl start</p>
399 <li>Accessing the <em class="java">NetworkServerControl</em> API from a Java program.
400 <li>Running a so-called "embedded server" by setting the property
401 <var class="property">derby.drda.startNetworkServer=true</var> before loading the
402 embedded driver (with <em class="fileName">derbynet.jar</em> in the classpath).
403 <li>Running the script <em class=fileName>$DERBY_HOME/bin/startNetworkServer</em>
404 (<em class=fileName>%DERBY_HOME%\bin\startNetworkServer.bat</em> on Windows).
405 </ul>
406 <p class="CellBody">
407 Note that unless you set the system property
408 <var class="property">derby.system.home</var>, Derby's default database
409 location is the working directory of the Network Server JVM.
410 </p>
411 </td>
412 </tr>
413 </tbody>
414 </table>
415 </ol>
416
417 <!-- ########## HOW TO RUN - DERBY CLIENT ########## -->
418 <h3><a id="derbyclient" name="derbyclient">Using the Derby client driver (<var class="command">derbyclient</var>)</a></h3>
419 <p>
420 The Derby client driver is a JDBC driver included in binary distributions of
421 Apache Derby, in the directory <em class="fileName">$DERBY_HOME/lib/</em>.
422 It is recommended that you use this driver to connect to the
423 Derby Network Server, as this client driver is developed and maintained by
424 the Apache Derby development community.
425 </p>
426 <table class="listing">
427 <tr><td class="listItem">Class name:</td><td class="listItem"><em class="javaObject">org.apache.derby.jdbc.ClientDriver</em></td></tr>
428 <tr><td class="listItem">Library:</td><td class="listItem"><em class="fileName">derbyclient.jar</em></td></tr>
429 </table>
430 <p>&nbsp;</p>
431 <ol class="decimal">
432 <li class="Normal">Open a command window
433 <li class="Normal">If you haven't set it already on a system-wide basis, set
434 the <var class="envVar">DERBY_HOME</var> environment variable to the location
435 of this Derby installation. This is not strictly required to run the demo, but
436 this environment variable will be used later on this page to refer to the
437 required Derby resources, files, etc. Examples:
438 <p class="BodyRelative">UNIX (ksh/bash):</p>
439 <p class="commandLine">export DERBY_HOME=/home/user/derby/db-derby-10.x.y.z-bin</p>
440 <p class="BodyRelative">Windows:</p>
441 <p class="commandLine">set DERBY_HOME=c:\programs\derby\db-derby-10.x.y.z-bin</p>
442 <li class="Normal">Change directories to the <em class="fileName">$DERBY_HOME/demo/programs/simple</em> directory.
443 <li class="Normal">In the client command window, set the CLASSPATH to include the
444 current directory (the location of <em class="fileName">SimpleApp.class</em>)
445 and Derby's client driver library (<em class="fileName">derbyclient.jar</em>).
446 (You may skip this step and provide the classpath as an option to the JVM
447 launch command instead, refer to your JVM's documentation for details).
448 <p class="BodyRelative">This may be done as follows:</p>
449 <p class="BodyRelative">UNIX (ksh/bash):</p>
450 <p class="commandLine">export CLASSPATH=.:${DERBY_HOME}/lib/derbyclient.jar</p>
451 <p class="BodyRelative">WINDOWS:</p>
452 <p class="commandLine">set CLASSPATH=.;%DERBY_HOME%\lib\derbyclient.jar</p>
453 <li class="Normal">(Optional) Run Derby's Sysinfo utility for testing the
454 classpath in your environment. You should provide the arguments
455 <em class="Emphasis">client</em> (to indicate a client environment) and
456 <em class="Emphasis">SimpleApp.class</em> to test for the presence of the
457 <em class="javaObject">SimpleApp</em> class.
458 <p class="BodyRelative">
459 You run the utility like this:
460 </p>
461 <p class="CommandLine">
462 java org.apache.derby.tools.sysinfo -cp <em class="Emphasis">arguments</em>
463 </p>
464 <p class="BodyRelative">
465 So for the arguments you need here, run it like this (all on one line):
466 </p>
467 <p class="CommandLine">
468 java org.apache.derby.tools.sysinfo -cp client SimpleApp.class
469 </p>
470 <p class="BodyRelative">
471 If your environment is set up correctly, the utility shows output indicating
472 success. It looks like this:
473 </p>
474 <pre class="Output">
475 FOUND IN CLASS PATH:
476
477 Derby Client libraries (derbyclient.jar)
478 /home/user/derby/db-derby-10.x.y.z-bin/lib/derbyclient.jar
479
480
481 user-specified class (SimpleApp)
482 /home/user/derby/db-derby-10.x.y.z-bin/demo/programs/simple
483
484
485 SUCCESS: All Derby related classes found in class path.
486 </pre>
487 <li class="Normal">Start the Derby Network Server in a different command
488 window, as described <a href="#server">here</a>
489 <li class="Normal">Start SimpleApp in Derby client mode:
490 <p class="commandLine">java SimpleApp derbyclient</p>
491 <p class="BodyRelative">The <var class="command">derbyclient</var> argument
492 tells the program to use the Derby client driver instead of its default
493 driver (the embedded driver).
494 </p>
495 <p class="BodyRelative">A successful run produces the following output:</p>
496 <pre class="Output">
497 SimpleApp starting in derbyclient mode
498 Connected to and created database derbyDB
499 Created table location
500 Inserted 1956 Webster
501 Inserted 1910 Union
502 Updated 1956 Webster to 180 Grand
503 Updated 180 Grand to 300 Lakeshore
504 Verified the rows
505 Dropped table location
506 Committed the transaction
507 SimpleApp finished
508 </pre>
509 <p class="BodyRelative">
510 If any error messages appear, and you are unable to resolve the error(s),
511 ask for help on the derby-user
512 <a href="http://db.apache.org/derby/derby_mail.html" target="_new">mailing list</a>.
513 </p>
514 </ol>
515
516 <!-- ########## FOOTER (VERSION INFO) ########## -->
517 <hr>
518 <p class=NavBarVersion>Last updated for Apache Derby Version 10.11</p>
519
520 </body>
521 </html>