Interstage Shunsaku Data Manager Application Development Guide - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX -
Contents Index PreviousNext

Appendix F Sample Java Programs> F.1 Searching Data

F.1.4 Obtaining XML Documents that Match Search Conditions after they have been Sorted

When performing a search, the user sometimes prefers to obtain the search results after they have been sorted according to a specific element.

Specify the date and location in the search conditional expression and obtain the number of hotels that match the conditions and partial information about those hotels.

To show how the Java APIs are used, the following example sorts the extracted data in descending order according to the accommodation rate.

mark1Search Conditions

'I want to know the hotels in Sydney that have a vacancy on 2006/07/18, and I want to sort the hotels in descending order according to the accommodation rate.'

Specify the date (2006/07/18) and the location (Sydney) as search conditions. Also specify the accommodation rate in the sort condition and then execute the search.

mark1An Example of Using the APIs

The following is a sample program using the Java APIs.

import com.fujitsu.shun.ShunConnection;
import com.fujitsu.shun.ShunPreparedStatement;
import com.fujitsu.shun.ShunResultSet;
import com.fujitsu.shun.common.ShunException;

/***     Obtain data that corresponds to specified search conditions after the data has been sorted     ***/
public class JavaAPISample4 {

  public static void main(String[] args) {
    ShunConnection con = null;
    ShunPreparedStatement pstmt = null;
    ShunResultSet rs = null;

    try {
      // Search conditional expression 
      String sQuery = "/document/base/city == 'Sydney' AND /document/information/date == '2006/07/18'";
      // Return expression
      String sReturn = "/document/base/name, /document/base/price";
      // Sort expression
      String sSort = "val(/document/base/price/text()) DESC";
      // Number of hits
      int iHitNum = 0;
      // Data counter
      int iDataCounter = 1;

      // Create ShunConnection object
      con = new ShunConnection("DirSvr1", 33101);

      // Specify search expression and create ShunPreparedStatement object
      pstmt = con.prepareSearch(sQuery, sReturn);

      //Specify number of items to return per request
      pstmt.setRequest(1, 30);

      // Specify sort expression
      pstmt.setSort(sSort);

      // Execute search and create ShunResultSet object
      rs = pstmt.executeSearch();

      // Obtain the data corresponding to the search conditions one item at a time 
      while (rs.next()) {
        System.out.println("[Result] Item No. " + iDataCounter + " = " + rs.getString());
        iDataCounter++;
      }
      rs.close();
      pstmt.close();
      con.close();
    }
    catch (ShunException ex) {
            int errorLevel = ex.getErrLevel();
      switch( errorLevel ) {
        case ShunException.SHUN_ERROR :
          System.out.println("Error level  :SHUN_ERROR");
          break;
        case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
          System.out.println("Error level  :SHUN_ERROR_CONNECTION_TERMINATED");
          break;
      }
      System.out.println("Error message:" + ex.getMessage());
      ex.printStackTrace();
    }
    catch (Exception ex) {
      System.out.println("Error message:" + ex.getMessage());
      ex.printStackTrace();
    }
    finally {
      try {
        if (rs != null)
          rs.close();
      }
      catch (ShunException ex) {
              int errorLevel = ex.getErrLevel();
      switch( errorLevel ) {
        case ShunException.SHUN_ERROR :
          System.out.println("Error level  :SHUN_ERROR");
          break;
        case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
          System.out.println("Error level  :SHUN_ERROR_CONNECTION_TERMINATED");
          break;
      }
        System.out.println("Error message:" + ex.getMessage());
        ex.printStackTrace();
      }
      try {
        if (pstmt != null)
          pstmt.close();
      }
      catch (ShunException ex) {
              int errorLevel = ex.getErrLevel();
      switch( errorLevel ) {
        case ShunException.SHUN_ERROR :
          System.out.println("Error level  :SHUN_ERROR");
          break;
        case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
          System.out.println("Error level  :SHUN_ERROR_CONNECTION_TERMINATED");
          break;
      }
        System.out.println("Error message:" + ex.getMessage());
        ex.printStackTrace();
      }
      try {
        if (con != null)
          con.close();
      }
      catch (ShunException ex) {
              int errorLevel = ex.getErrLevel();
      switch( errorLevel ) {
        case ShunException.SHUN_ERROR :
          System.out.println("Error level  :SHUN_ERROR");
          break;
        case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
          System.out.println("Error level  :SHUN_ERROR_CONNECTION_TERMINATED");
          break;
      }
        System.out.println("Error message:" + ex.getMessage());
        ex.printStackTrace();
      }
    }
  }
}

mark1Execution Results

[Result] Item No. 1 = <document><name>Hotel 1</name><price>350</price></document>
[Result] Item No. 2 = <document><name>Hotel 3</name><price>250</price></document>
[Result] Item No. 3 = <document><name>Hotel 2</name><price>150</price></document>

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006