1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Derby Network Server Simple sample program</title>
</head>
<body>
<h2>Simple Derby Network Server Example</h2>
<h2>Overview</h2>
<p>The primary purpose of this example is to demonstrate how to obtain an embedded
connection and client connections using the Network Server to the same
database. This program shows how to use either the DriverManager or a
DataSource to obtain client connections. </p>
<p>In order for a database to be consistent, only one JVM is allowed to access
it at a time. The embedded driver is loaded when the Network Server is started.
Hence, the JVM that starts the Network Server can get an embedded connection to
the same database that Network Server is accessing to serve the clients from
other JVMs. This solution allows you to take advantage of the performance
benefits of the embedded driver as well as allow for client connections from other
JVMs to connect to the same database.</p>
<p>In this example, the following two programs show how to set up the server and
client programs when interacting with the Network Server.</p>
<ul>
<li><tt>SimpleNetworkServerSample.java</tt>:
This program starts the Derby Network Server and waits for clients to
connect.</li>
<li><tt>SimpleNetworkClientSample.java</tt>:
This is the client program that interacts with the Derby Network Server
from another JVM.</li>
</ul>
<h2>SimpleNetworkServerSample program</h2>
<p>In particular, this program:</p>
<ol>
<li>Starts the Derby Network Server using a property and loads the
embedded driver</li>
<li>Checks if the Derby Network Server is up and running</li>
<li>Creates the database <tt>NSSimpleDB</tt> if not already created </li>
<li>Obtains an embedded database connection</li>
<li>Tests the database connection by executing a sample query</li>
<li>Allows client connections to connect to the server until the user decides to stop the server and exit the program</li>
<li>Closes the connection</li>
<li>Shuts down the Derby Network Server before exiting the program</li>
</ol>
<p>The following files are needed in the
<tt><i>DERBY_INSTALL</i>\demo\programs\nserverdemo\</span></tt> directory in
order to run this sample program:</p>
<ul>
<li>Source file: <br><tt>SimpleNetworkServerSample.java</tt></li>
<li>Compiled class file: <br><tt>SimpleNetworkServerSample.class</tt></li>
</ul>
<h3>How to run the SimpleNetworkServerSample program</h3>
<p>To run this simple Derby Network Server sample program:</p>
<ol>
<li>Open a command prompt and change directories to the <tt><i>DERBY_INSTALL</i>\demo\programs\nserverdemo\</tt>
directory, where <tt><i>DERBY_INSTALL</i></tt> is the directory
where you installed Derby.</li>
<li>Set the CLASSPATH to include the following jar files in order to run this
program.</li>
<ul><li>The current directory (".")</li>
<li><tt>derbynet.jar</tt>: <BR>The Network Server jar file. It must be in your CLASSPATH since we start the Network Server in this program.</li>
<li><tt>derby.jar</tt>: <BR>The Derby database engine jar file.</li>
</ul>
<li>Test the CLASSPATH settings by running the following java command:
<pre>java org.apache.derby.tools.sysinfo</pre>
<p>This command will show the Derby jar files that are in the CLASSPATH. </p></li>
<li>Once you have set up your environment correctly, execute the application from the <tt><i>DERBY_INSTALL</i>\demo\programs\nserverdemo</tt> directory:
<pre>java SimpleNetworkServerSample</pre>
</li></ol>
<p>You should receive output similar to the following if the program runs successfully:</p>
<pre>
Starting Network Server
Testing if Network Server is up and running!
Derby Network Server now running
Got an embedded connection.
Testing embedded connection by executing a sample query
number of rows in sys.systables = 23
While my app is busy with embedded work, ij might connect like this:
$ java -Dij.user=me -Dij.password=pw -Dij.protocol=jdbc:derby://localhost:1527/ org.apache.derby.tools.ij
ij> connect 'NSSimpleDB';
Clients can continue to connect:
Press [Enter] to stop Server
</pre>
<p><b>Do not press Enter at this time.</b> Leave the server running while you run the
<tt>SimpleNetworkClientSample</tt> program.</p>
<p>Running this program will also create new directories and files:</p>
<ul>
<li><span>NSSimpleDB</span><br>
This directory makes up the <span>NSSimpleDB</span> database.
</li>
<li><tt>derby.log</tt><br>
This log file contains Derby progress and error messages.
</li></ul>
<h2>Example of a client connecting to the Network Server</h2>
<h3>SimpleNetworkClientSample program</h3>
<p>This program:</p>
<ol>
<li>Obtains a client connection using the DriverManager</li>
<li>Obtains a client connection using a DataSource</li>
<li>Tests the database connections by executing a sample query</li>
<li>Closes the connections and then exits the program</li>
</ol>
<p>The following files should be installed in the <tt><i>DERBY_INSTALL</i>\demo\programs\nserverdemo\</tt> directory in order to run this sample program:</p>
<ul>
<li>Source file: <br><tt>SimpleNetworkClientSample.java</tt></li>
<li>Compiled class file: <br><tt>SimpleNetworkClientSample.class</tt></li>
</ul>
<h3>How to run the SimpleNetworkClientSample program</h3>
<p>To connect to the Network Server:</p>
<ol>
<li>Open another command prompt and change directories to the
<tt><i>DERBY_INSTALL</i>\demo\programs\nserverdemo</tt> directory, where
<tt><i>DERBY_INSTALL</i></tt> is the directory where you installed Derby.</li>
<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
the Derby Client Network JDBC Driver.</li>
</ul></li>
<li>Once you have set up your environment correctly, execute the
application from the <tt><i>DERBY_INSTALL</i>\demo\programs\nserverdemo\</tt> directory:
<pre><tt>java SimpleNetworkClientSample [<i>driverType</i>]</tt></pre>
<p>where the possible values for <i>driverType</i> are:</p>
<ul>
<li>derbyClient (default)</li>
</ul>
</li>
</ol>
<p>You should receive output similar to the following if the program runs
successfully:</p>
<tt>
Starting Sample client program <br>
Got a client connection via the DriverManager.<br>
connection from datasource; getDriverName = Apache Derby Network Client JDBC Driver<br>
Got a client connection via a DataSource.<br>
Testing the connection obtained via DriverManager by executing a sample query <br>
number of rows in sys.systables = 23<br>
Testing the connection obtained via a DataSource by executing a sample query <br>
number of rows in sys.systables = 23<br>
Goodbye!<br>
</tt>
<p>After running the program, return to the command prompt where you ran the
<tt>SimpleNetworkServerSample</tt> program and press Enter.</p>
</body>
</html>
|