Derby Network Server Simple sample program

## Simple Derby Network Server Example

## Overview

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.

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.

In this example, the following two programs show how to setup the server and client programs when interacting with the Network Server.

SimpleNetworkServerSample.java: This program starts the Derby Network Server as well as the embedded driver and waits for clients to connect.

SimpleNetworkClientSample.java: This is the client program that interacts with the Derby Network Server from another JVM.

### SimpleNetworkServerSample program

In particular, this program:

- starts the Derby Network Server using a property and also loads the embedded driver
- checks if the Derby Network Server is up and running
- creates the database 'NSSimpleDB' if not already created
- obtains an embedded database connection
- tests the database connection by executing a sample query
- allows for client connections to connect to the server until the user decides to stop the server and exit the program
- closes the connection
- shuts down the Derby Network Server before exiting the program.

The following files should be installed in the `DERBY_INSTALL\demo\programs\nserverdemo\` directory in order to run this sample program:

- Source file:

- `SimpleNetworkServerSample.java`

- Compiled class file:

- `SimpleNetworkServerSample.class`

## How to run the SimpleNetworkServerSample program

To run this simple Derby Network Server sample program:

1. Open a command prompt and change directories to the `DERBY_INSTALL\demo\programs\nserverdemo\` directory, where `DERBY_INSTALL` is the directory where you installed Derby.
2. Set the CLASSPATH to include the following jar files in order to run this program.

- the current directory (".")
- `derbynet.jar`:
  The Network Server jar file. It must be in your CLASSPATH since we start the Network Server in this program.
- `derby.jar`:
  The Derby database engine jar file.

3. Test the CLASSPATH settings by running the following java command:

       java org.apache.derby.tools.sysinfo

   This command will show the Derby jar files that are in the CLASSPATH.

4. Once you have set up your environment correctly, execute the application from the `DERBY_INSTALL\demo\programs\nserverdemo` directory:

       java SimpleNetworkServerSample

You should receive output similar to the following if the program runs successfully:

`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 = 16
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
`

Running this program will also create new directories and files:

- NSSimpleDB
  This directory makes up the NSSimpleDB database.
- `derby.log`
  This log file contains Derby progress and error messages.

## Example of a Client connecting to the Network Server

### SimpleNetworkClientSample program

This program:

- loads the Derby Network Client JDBC Driver
- obtains a client connection using the Driver Manager
- obtains a client connection using a DataSource
- tests the database connections by executing a sample query
- closes the connections and then exits the program.

The following files should be installed in the `DERBY_INSTALL\demo\programs\nserverdemo\` directory in order to run this sample program:

- Source file:
  `SimpleNetworkClientSample.java`
- Compiled class file:
  `SimpleNetworkClientSample.class`

## How to run the SimpleNetworkClientSample program

To connect to the Network Server that has been started as part of the SimpleNetworkServerSample program:

1. Open a command prompt and change directories to the `DERBY_INSTALL\demo\programs\nserverdemo` directory, where `DERBY_INSTALL` is the directory where you installed Derby.
2. Clients of Derby Network Server only need the following jar files in the CLASSPATH in order to connect to the Network Server. Set the CLASSPATH to include the following jar files:
   - the current directory (".")
   - `derbyclient.jar`
     This jar file must be in your CLASSPATH to use the Derby Client Network JDBC Driver.
3. Once you have set up your environment correctly, execute the application from the `DERBY_INSTALL\demo\programs\nserverdemo\` directory:

       java SimpleNetworkClientSample [driverType]

   where the possible values for driverType are:

   - derbyClient (default)

You should receive output similar to the following if the program runs successfully:

`Starting Sample client program
Got a client connection via the DriverManager.
connection from datasource; getDriverName = Apache Derby Network Client JDBC Driver
Got a client connection via a DataSource.
Testing the connection obtained via DriverManager by executing a sample query
number of rows in sys.systables = 16
Testing the connection obtained via a DataSource by executing a sample query
number of rows in sys.systables = 16
Goodbye!
`