Connecting Java servlets to an mSQL database with Apache JServ
by Developer's Daily
 

Introduction

We were asked recently if it's possible to use Mini SQL (mSQL) with Apache and the new Apache JServ module -- i.e., can you use the Java servlet features of Apache JServ to access the mSQL database engine? I'm glad to say that the answer is a resounding "yes", and we'll show you how to "make it so" in this mSQL/Apache JServ tutorial.
 

The quick answer

Before we start on the solution, let me note that this mSQL/JServ tutorial makes the following assumptions:
    1. You've alread downloaded and installed Apache and Apache JServ.
    2. You've already downloaded and installed mSQL.
    3. You've already downloaded and installed a recent version of the Java JDK.
    4. You don't know how to make them work together.
You might be surprised, but it's actually very easy. All you have to do to make these two programs work together is to tell Apache JServ where the msql-jdbc.jar file is located. You do this by editing the jserv.properties file in Apache's conf directory.

Assuming that you've installed the JAR file in a directory named /usr/local/Hughes, just add this line to the properties file and you'll be ready to go:

Once you've added this line to jserv.properties, just restart the Apache web server and you'll be ready to start running Java servlets that connect to the mSQL database server. Although you'll need to do other things to configure Apache JServ, like set up your servlet zones, adding the mSQL JAR file and database to the equation is very simple.
 

More detailed steps

For my particular intranet setup, here's a few more details about the installation:
  1. First, I installed Apache 1.3.4 and Apache JServ in a directory named /usr/local/lib/apache.
  2. Next, I installed the Hughes' mSQL database server in a directory named /usr/local/Hughes. I started the mSQL server and populated my databases (I don't mean to make this step sound too easy; as you may know, properly designing a database is a fair amount of work.).
  3. Once I verified that these two applications were working independently, I downloaded the mSQL-JDBC driver (i.e., the JAR file) via the web, and copied it into the /usr/local/Hughes directory.
  4. Next, I configured a servlet zone in Apache -- a place where I could install the Java servlets that work with mSQL. Creating servlet zones is beyond the scope of this article; the only point I want to make here is that I made my servlets available through a directory that appears to the server as /msqlserv.
  5. Next, I edited the jserv.properties file, and added the line "wrapper.classpath=/usr/local/Hughes/msql-jdbc.jar" just below the other wrapper.classpath entries. (There were two other entries by default, one for Apache-Jserv.jar and another for the jsdk.jar file.)
  6. At this point, I loaded our servlets into an appropriate servlet directory (this directory corresponds to your servlet zone configuration). As described in an earlier step, this directory is advertised by Apache under the name "/msqlserv".
  7. At this point, I restarted Apache so it would read my configuration file changes. My version of Apache included a nice shell script named apachectl (in Apache's bin directory) that let me stop and start Apache by typing "apachectl stop" and "apachectl start", respectively.
  8. Once the server was up and running, I entered the URL for one of my test servlets.  One of our servlets is named SEF, so I typed in this URL:  http://intranet.devdaily.com/msqlserv/SEF. To my amazement, it worked the very first time. The first time I hit the servlet it was fairly slow, but on subsequent accesses it was very fast.
Since the initial installation I've made plenty of changes, mostly regarding the servlets zones, but the mSQL installation was absolutely painless. If you've ever wanted an easy way to start developing Java servlets that connect to a relational database, I can highly recommend Apache JServ and the mSQL database engine.
 

Other locations on the Developer's Daily web site

We hope you enjoyed this short mSQL tutorial. We invite you to visit the other free educational resources on our web site:


Copyright DevDaily Interactive, Inc.
All Rights Reserved.