This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Exposing an RDBMS as a Data Service

In this tutorial, we will run through the process of service enabling an RDBMS as a data service. 



Step 1: Setting up an RDBMS

Follow the steps given below to set up a MySQL database for this tutorial.

  1. Install the MySQL server.
  2. Download the JDBC driver for MySQL from here and copy it to your <EI_HOME>/lib directory.
  3. Create the following database: trainingdb
  4. Create the following table:

    CREATE TABLE ACCOUNT(AccountID int NOT NULL,Branch varchar(255) NOT NULL, AccountNumber varchar(255),AccountType ENUM('CURRENT', 'SAVINGS') NOT NULL,
    Balance FLOAT,ModifiedDate DATE,PRIMARY KEY (AccountID)); 
  5. Enter the following data into the table:

    INSERT INTO ACCOUNT VALUES (1,"AOB","A00012","CURRENT",231221,'2014-12-02');

Step 2: Adding an RDBMS datasource

Follow the steps given below.

  1. Enter the URL to your MySQL database in the URL field.
  2. Enter the username and password to connect to your MySQL database. By default the username is root and the password is blank. 

Step 3: Defining a query 

Now let's start writing a query for getting data from the datasource. The query will specify the data that should be fetched and the format that should be used to display data when the query is invoked.

  1. Click Add New Query to open the Add New Query screen.
  2. Enter the following values:
    • Query ID: Enter an ID for the query.
    • Datasource: Select the datasource for which you are going to write a query. Select the RDBMS datasource that you created previously.
    • SQL: In this field, enter the SQL statement describing the data that should be retrieved from the RDBMS datasource.

      SELECT AccountID,Branch,AccountNumber,AccountType,Balance,ModifiedDate FROM
      ACCOUNT WHERE AccountID=:AccountID
  3. Add input mappings: Click Generate Input Mapping to automatically create a mapping for the AccountID field in the database.

    Input mappings allow you to add parameters to a query so that you can set the parameter value when executing the query. According to the above definition, you need to provide the account ID as an input in order to retrieve the data corresponding to the account ID.

    Find out more about defining Input Mappings.

  4. Add output mappings: Click Generate Response to automatically create mappings for the fields that should show in the output.

    Output mapping is used to specify how the data that is fetched from your query will be shown in the response. Note that, by default, the output type is XML. Find out more about defining Output Mappings. 

  5. Click Save and then Next to open the Operations screen.

Step 4: Defining an operation to invoke the query

Data service operations are written to invoke queries.

  1. Click Add New Operation to open Add New Operation screen.
  2. Add a name for your operation in the Operation Name field.
  3. In the Query ID field, select the query that you defined previously.
  4. Save the operation. 

Step 5: Finish creating the data service

Once you have defined the operation, click Finish to complete the data service creation process. You will now be taken to the Deployed Services screen, which shows all the data services deployed on the server.


Step 6: Invoking your data service 

You can try the data service you created by using the TryIt tool that is in your product by default. 

  1. Go to the Deployed Services screen.
  2. Click the Try this service link for the AccountDetailsService. The TryIt Tool will open with the data service.
  3. Select the operation you created earlier. You need to provide the account ID as an input. Enter '1'.
  4. Click Send to see the details of the account.

    <Entries xmlns="http://ws.wso2.org/dataservice">
       <Entry>
          <AccountID>1</AccountID>
          <Branch>AOB</Branch>
          <AccountNumber>A00012</AccountNumber>
          <AccountType>CURRENT</AccountType>
          <Balance>231221</Balance>
          <ModifiedDate>2014-12-02+05:30</ModifiedDate>
       </Entry>
    </Entries>