blob: 2a0d079abcc36eaaaf6dc79a900f89399ad0969f [file] [log] [blame]
<!--
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>