January 23, 2021
Hot Topics:

Exposing a Database as a Web Service

  • By Deepal Jayasinghe
  • Send Email »
  • More Articles »

The service description file, or the services.xml for your service, will be a very simple one. You can download that from the Download section. The services.xml file will look like the following:

<service name="DBSampleService"
   <description>Exposing a DB as a Web Service</description>
   <parameter name="ServiceClass">

Deploying the Service

Before you deploy the service, you need to create a service archive file using your compiled classes and services.xml. You can use any available tools, or you can just create a zip file from the compiled code and services.xml and rename that as dbsample.aar. I have created a service archive file from the compiled code so that you can download that and just try it out.

Because you need to have the DB connection jar, first you have to copy the mysql-connector jar file in the class path or to <TOMCATHOME>/webappes/axis2/WEB-INF/lib. Next, you can copy your service archive file into <TOMCATHOME>/webappes/axis2/WEB-INF/services directory. Then start Tomcat (or your application server).

Now, type http://localhost:8080/axis2/services/DBSampleService?wsdl in your browser (the port may vary depending on the application server configurations); then, you will be able to see the WSDL file for your sample DB service. This is simply an indicator that your service is up and running. If you do not get the WSDL file, something has gone wrong with your database or database driver.

Invoking the Service

In this sample, you are not going to write a Java client to invoke the service; if you want, you can try that out. However, you will focus more on invoking the service just using the browser or REST manner.

List all the People in the DB

To see all the people in the database, you can invoke the "listAllPeople" method in your service. Just type the following in the browser and see what you are getting. (This is how you invoke the listAllPeople method in the REST manner.)


You will get something like the following, which is simply all the people in the DB.

   <ns:return type="dbsample.Person">
         No 59, Flower Road, Colombo, Sir Lanka
   <ns:return type="dbsample.Person">
      <ax21:address>San Jose, CA</ax21:address>
   <ns:return type="dbsample.Person">
      <ax21:address> Colombo, Sri Lanka</ax21:address>
      <ax21:name>Samisa Abeysinghe</ax21:name>

List the Names of All the People in the DB

Getting a list of names of the people is almost equal to the method invocation above. To get all the names, just type the following in the browser and see what you get:


Then, you will see something like the following, which is exactly all the names of the people in the DB.

   <ns:return>Samisa Abeysinghe</ns:return>

Getting the Name and Age for a Given Person

Now, you will the name and age of a given person, so will be a matter of giving the person ID and the Web Service gives you the name and age of the person represented by the id. Invoking that service is just a matter or typing the following in the browser:


Then, you get the following output:

   <ns:return type="dbsample.NameAge">

Page 2 of 3

This article was originally published on March 21, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date