Share on Google+Share on Google+

Database driven webservices

Create database driven webservice.

WebService with database Mysql - A database driven webservice


User Registration WebServices Example

In this tutorial we will develop a simple user registration Webservices. The user registration/account registration form will be presented to the user. Once user fills in the form and clicks on the "OK" button,  the serverside JSP will call the webservice to register the user.

This webservices will expose the insert user operation which will be used by the JSP client to register the user. We will use the NetBeans 6.1 IDE to develop and test the application.

The MySQL database is used to save the user registration data. You can modify the code to use any database of your choice. The existing webservices can also be modified to use the Hibernate or any other ORM technologies. You can also use the Entity beans to persist the data into database.

Software required to develop and run this example:
  • JDK 1.6
  • NetBeans 6.1
  • MySQL Database 5 or above
Let's get started with the development of the applicaton

MySql Database Configuration In NetBeans
Let's configure MySQL database in teh NetBeans IDE and then create the required table into database.

       Step 1:

  • Click on the service tab in NetBeans  as shown below in Fig 1.

                                     Fig. 1

     Step 2:
  • Right Click on the Databases
  • Select New Connection as shown below in Fig 2.

                                  Fig. 2.

    Step 3:

  • It opens a dialog box for the mysql configuration.
  • Type the driver name, url , user name and password as shown below in Fig. 3.

                                                              Fig. 3
    Step 4:

  • Click on the Ok button .
  • Now expand the Newly created database connection.
  • It shows the all the tables of the database test as shown below in Fig 4.

                                     Fig. 4

    Step 5:

  • Create a table named login.
  • Right Click on  the Tables and select Create table
    as shown below in Fig 5

                                         Fig. 5
    Step 6:

  • It opens a dialog box for giving the fields name of the table 
  • Now give the field name and data type as shown below in Fig 6.

                                                        Fig. 6

    Step 7:

  • Click on the Ok 
  • It creates the table login in the test database.

    Creating the WebService program for Account creation

    Step 8:

  • Open the netbeans 6.1
  • Creat a new web project as shown below in Fig 7.

                                                     Fig. 7

    Step 9:

  • Type the project Name as MyAccount
  • Click on the next button as shown below in Fig 8.

                                                      Fig. 8

    Step 10:

  • Select the server as Glassfish 
  • Click on the Next and then finish button as shown below in Fig 9.

                                                                 Fig. 9
    Step 11:

  • It creates a Web Project  named MyAccount.

    Creating the WebService

    Step 12:

  • Right Click on the project MyAccount 
  • Select New-->WebService  as shown below in Fig 10.

                                                           Fig. 10

    Step 13:

  • Type the name of the WebService as myaccount with the  package as mypack.
  • Click on the Finish button as shown below in Fig 11.

                                                       Fig. 11

    Step 14:

  • It creates a WebService  application  in design view
  • Click on the Add operation as shown below in Fig 12.

                                                       Fig. 12
    Step 15:

  • In the dialog box type  all  parameter names. 
  • Also select the appropriate data type.
  • Click on Ok as shown below in Fig 13.

                                                       Fig. 13
    Step 16:

  • It creates a WebService  application 
  • Click on the source tab as shown below in the Fig 14.

                                                       Fig. 14
    Step 17:

  • Now  create the database source
  • Right Click in the source code of 
  • Select the Enterprise Resources-->Use Database as shown below in Fig 15.

                                                       Fig. 15
    Step 18:

  • In the choose database select the Add button  as shown below in Fig 16.

                                                   Fig. 16
          Step 19:
  • It opens a Add Data Source Reference.
  • Type the Reference Name as data1
  • For Project Data  Sources Click on the Add button as shown below in Fig 17.

                                              Fig. 17

    Step 20:

  • In the Crea Data Source  type thye jndi name as jndi1
  • In the database connection select  the newly created database connction for the mysql.
    as shown below in Fig 18.

                                                        Fig. 18

    Step 21:

  • Click on the Ok button
  • It creates the database connection gives the dialog box  as shown below.
  • Click on the Ok button as shown below in Fig 19.

                                                                Fig. 19

    Step 22:

  • It creates the datasource data1 with the resource name as data1 in the code
  • Edit the code and give the database connection, statement  for the mysql connectivity 
    as shown below.

package mypack;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.sql.DataSource;

public class myaccount {

  @Resource(name = "data1")
  private DataSource data1;

  @WebMethod(operationName = "insert")
  public String insert(@WebParam(name = "uname"String uname,
@WebParam(name = "fname"String fname, 
@WebParam(name = "lname"String lname, 
(name = "location"String location, 

(name = "phone"String phone, 
@WebParam(name = "credit"String credit, 
(name = "dl"String dl) {
  try {
  Connection con = 
  PreparedStatement st = 
("insert into login values(?,?,?,?,?,?,?)");
  st.setString(1, uname);
  st.setString(2, fname);
  st.setString(3, lname);
  st.setString(4, location);
  int ph = Integer.parseInt(phone);
  st.setInt(5, ph);
  int cr = Integer.parseInt(credit);
  st.setInt(6, cr);
  int d1 = Integer.parseInt(dl);
  st.setInt(7, d1);
  catch (Exception e) {
  return "record inserted";

        Step 23:
  • Now deploy the project 
  • Right click on the project 
  • Select Undeploy and Deploy as shown below in Fig 20.
  • It builds and deploys the MyAccount Web project on the glassfish server.

                                          Fig. 20
    Step 24:
  • After deploying now we can test the Web Service created
  • Right click on the myaccount webservice 
  • Select  Test Web Service  as shown below in Fig 21.
                              Fig. 21

Step 25:
  • It runs the deployed webservice in the firefox browser.
  • Type the value and click on the insert button as shown below in Fig 22.

                                                Fig. 22

Step 26:
  • After clicking on the insert button it insert the values into the login table 
  • It open new window and displays the Metrhod parameters, SOAP request and response
    as shown below in Fig 23.

                                                   Fig. 23
Step 27:

Check the data in the database table
  • In the service tab select the mysql connection
  • In its table node right click on the table login
  • Select the view data as shown below in Fig 24.

  •                                 Fig. 24

Step 27:
  • It opens a new window 
  • Then displas the data as shown below in Fig. 25.

                                                       Fig. 25

Making the client of the WebService

Step 28:
  • Create a new Web Project 
  • Type the name as  MyAccountClient as shown below in Fig.26.

                                             Fig. 26
Step 29:
  • Click on the next button 
  • Select the server as glassfish as shown below in Fig. 27.

                                             Fig. 27
       Step 30:
  • Click on the finish button
  • It creates a web rpoject with a index.jsp file
  • In the index.jsp  design it with form and all its fileds 
  • Give the form action as Client.jsp as shown below in Fig 28.

    Create WebService Client

    Step 1:

  • Right Click on the MyAccountClient  
  • Select WebService Client  as shown below in Fig.28.

                                                    Fig. 28
    Step 2:

  • In the dialog box click on the browse button
    as shown below in Fig 29.

                                                                    Fig. 29
    Step 3:

  • Select the myaccount webservice.
  • Click on the OK button as shown below in Fig 30.

                                           Fig. 30
    Step 4:

  • Now project is containing the WSDL file url.
  • Click on the Ok button as shown below in Fig 31.

                                                            Fig. 31
    Step 5:

  • Right Click  in the Client.jsp
  • Select Web Service Client Resources-->Call WebService Operation
    as shown below in Fig 32.

                                                 Fig. 32
    Step 5:

  • Select the insert operation as shown below in Fig .33.

                                              Fig. 33
    Step 6:

  • After insert opoeration the code becomes like as shown below in Fig 34.

                                                        Fig. 34
    Step 7:

  • Deploy the MyAccountClient  
  • Right Click and select Undeplo and Deploy as shown below in Fig.35.

                                       Fig. 35
    Step 8:

  • After deployment run the project.
  • Right Click and select the run as shown below in Fig 36.

                                       Fig. 36
    Step 9:

  • It runs the index.jsp in the browser  as shown below in Fig 37.
  • Put the data and click on the ok button 

                              Fig. 37
    Step 10:

  • After this click values get inserted in the database table 
  • The message comes as record inserted as shown below in Fig.38

                            Fig. 38
    Step 11:
  • See the data inserted in the table.
  • In the service tab select the connection created for mysql.
  • In the table node select the login
  • Right Click and select the view data as shown below in Fig. 39.

                                  Fig. 39
    Step 12:

  • It displays the record inserted as shown below in Fig 40.

                                                      Fig. 40
Download the example code of the Webservices discussed in this tutorial.


Posted on: November 11, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Database driven webservices   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 22, 2012

affiche les donnèe de basse de donnèe select *from client
July 5, 2012
wsdl and databases

this is really helpful!!!! thank you a lot!!!!
October 8, 2012
Displays record inserted but mysql table empty

Hi thats a very nice tutorial, the web service part works however am having problems with the client part, after clicking on the ok button, it tells me record inserted, but when i check in the login table there is a new row, but the field are empty. No data is there! Am using netbeans7.0 and glassfish v3.0 java EE 5 and mysql 5.2. Please help.
July 18, 2013

every thing is wrong....can't do anything...
June 21, 2014

I did same as u did in tutorial but I am not able to see the values in the table.In output it showed the values are inserted perfectly
Naren Joshi
August 7, 2014

is this SOAP based or Restful?
August 8, 2014

The records are not being updated in the database What may be the solution?
August 8, 2014
Posting in database

The values are not updated in the database
Ram Kumar
March 22, 2012
EJB Methods And Enterprise Resources menu options

I think what you are looking can be found by: - right clicking in the editor - clicking insert code... - there is then an option for call enterprise bean as well as the other options that used to be in the ENterprise Resources menu Hope this helps.