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