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.1 Obtaining the Number of XML Documents that Match Search Conditions

If there is a large amount of data to be searched, a large number of items may match the search conditions. In this case, it is not practical to return all the data items for the user to view. In such cases, it is useful to find out first how many data items match the specified condition.

The example below shows how to use the Java APIs to find out how many hotels match the date and location specified as the search condition.

mark1Search Conditions

'How many hotels in Sydney have a vacancy on 2006/07/18?'Execute a search using the date (2006/07/18) and the location (Sydney) as search conditions.

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 the number of records that correspond to the specified search conditions     ***/
public class JavaAPISample1 {

  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 = "/";

      // Number of hits
      int iHitNum = 0;

      // 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, 0);

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

      // Obtain number of hits
      iHitNum = rs.getHitCount();
      System.out.println("Number of hits = " + iHitNum);
      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();
    }
    // Recovery procedure in the event of an error
    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

Number of hits = 3

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006