Appendix B. Setting up the Application Database – Apache Derby

To set up some kind of database running on your development environment, we use Apache Derby database. Apache Derby is a light weight in memory database, which is easy to setup and takes less resources, and is also perfect for testing out new concepts and trying out things that we are doing right now.

To download Apache Derby, hit over the Apache Derby website at http://db.apache.org/derby/derby_downloads.html, and download the latest release. Once the downloaded ZIP file is extracted, we will have some important folder named bin and lib folder as shown:

Setting up the Application Database – Apache Derby

The lib folder contains the jar that needs to be included in our program when we connect to the Derby database. The bin contains programs like startNetworkServer.bat and stopNetworkServer.bat for database as follows:

Setting up the Application Database – Apache Derby

After downloading and extracting the JAR file, the next step is to set environment variable. Derby recommends a couple of environment variables that need to be set.

  • DERBY_HOME: The DERBY_HOME environment variable needs to be set to the location where we have extracted our distribution containing the bin and lib folder.
  • Path: The second variable that we need to set is path environment variable. We need to set DERBY_HOME/bin to path environment variable:
    Setting up the Application Database – Apache Derby

Now, open a command prompt, and hit over to location where we have Apache Derby as follows:

Setting up the Application Database – Apache Derby

Here in the preceding screenshot, we have changed the directory to the bin directory in Apache Derby, which contains batch files and script files. For Windows OS, we need these batch files while running.

Apache Derby operates in two modes:

  • The Network-Server mode
  • The Embedded mode

First, we need to start derby as Network Server mode which is similar to all the databases on one machine, and the other machine on the network that can connect to it. Embedded mode is something specific to derby.

To start derby Network Server mode, we need to run startNetworkServer.bat as follows:

Setting up the Application Database – Apache Derby

In the preceding screenshot, we can see that Apache Derby Network-Server started and ready to accept connection on port 1527, which we can test by using a client to connect to the server. Derby actually comes with a client called ij.bat, which can be used to connect to the server to execute queries.

So, we need to have a second command line prompt, which hits same bin directory, and run ij.bat to client, and executes the query to connect to the server connect jdbc:derby://localhost:1527/db;create=true'; as follows:

Setting up the Application Database – Apache Derby

We can create a table using create query for table employee (as shown in the following screenshot with two column as ID and NAME), and insert values using insert query, and also print data to console using select query, as follows:

Setting up the Application Database – Apache Derby
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.239.231