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

Part 2 Developing Applications by APIs> Chapter 9 Developing Java Applications> 9.3 How to Use Java APIs> 9.3.2 Data Searches

9.3.2.1 Obtaining the Number of XML Documents that Match Search Conditions

Sometimes it is desirable to obtain the number of items that match specific search conditions before extracting the actual data.

To obtain only the number of matching items, specify 0 as the number of items to return per request (requestCount) for the setRequest method.

The following diagram shows the process for obtaining the number of XML documents that match specified conditions.

[Figure 9-3 Flow of the Process for Obtaining the Number of XML Documents that Match Conditions]

mark1Sample Code

ShunConnection con = new ShunConnection();

String queryExpression = "/document/base/prefecture == 'Sydney'";
String returnExpression = "/";
ShunPreparedStatement pstmt = con.prepareSearch(queryExpression, returnExpression); (1)
pstmt.setRequest(1,0);                                                              (2)
ShunResultSet rs = pstmt.executeSearch();                                           (3)
System.out.println("[Number of hits] = " + rs.getHitCount());                       (4)
rs.close();                                                                         (5)
pstmt.close();                                                                      (5)

con.close();

mark2(1) Create a ShunPreparedStatement Object

Create a ShunPreparedStatement object by specifying a search expression and a return expression as parameters of the prepareSearch method.

Refer to Appendix B Format of Search, Return and Sort Expressions for more information on search expressions and return expressions.

mark2(2) Set the Reply Start Number and the Number of Items to Return per Request

Specify the reply start number and the number of items to return per request in the setRequest method. To obtain only the number of matching items, specify 0 as the number of items to return per request.

mark2(3) Execute the Search (create a ShunResultSet Object)

Execute the search using the executeSearch method. A ShunResultSet object will be created to hold the results of the search.

mark2(4) Obtain the number of XML documents that match search conditions

Use the getHitCount method to obtain the number of XML documents that match the search conditions.

mark2(5) Release the ShunResultSet Object and the ShunPreparedStatement Object

Always use the close methods of the ShunResultSet object and the ShunPreparedStatement object to release these objects when they are no longer required.

mark1Sample Codes for Studio

The following is sample code for a business class that uses a Shunsaku access class created with the Studio interactive wizard, and a prototype Shunsaku access class that is used by the business class.

Business Class

ShunConnection con = new ShunConnection();

ShunsakuAccessController controller = new ShunsakuAccessController(con);
controller.search(key);
int hitCount = controller.getHitCount();
controller.close();

con.close();

ShunPreparedStatement Object

public class ShunsakuAccessController {
    :
   private ShunPreparedStatement pstmt;
    :
}

search Method of Shunsaku Access Class

public void search(java.lang.String keys) throws ShunException {
    :
   queryString = "/document/base/prefecture == ' " + keys + "'";                     (a)
   returnString = "/";                                                               (b)
    :
   getPreparedStatement(queryString, returnString);
    :
}

private void getPreparedStatement(String queryString, String returnString) {
    :
   pstmt = con.preparedSearch(queryString, returnString);                            (1)
    :
}
(a) Search Expression
This expression is defined using the interactive wizard. The interactive wizard can also be used to create complex search expressions.
(b) Return Expression
"/" is specified here to extract the entire XML document.

getHitCount Method of Shunsaku Access Class

public int getHitCount() throws ShunException {
     :
   resultSet = getResultSet(ShunsakuAccessController.SORT_NONE, 1, 0);
     :
   hitCount = resultSet.getHitCount();                                         (4)
     :
   resultSet.close();                                                          (5)
}

public ShunResultSet getResultSet(String sortString, int position, int requestCount)
                                                     throws ShunException {
     :
   pstmt.setRequest(position, requestCount);                                   (2)
     :
   return pstmt.executeSearch();                                               (3)
}

close Method of Shunsaku Access Class

public void close() throws ShunException {
    :
   pstmt.close();                                               (5)
    :
}

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006