| Interstage Shunsaku Data Manager Application Development Guide - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX - | 
			Contents
			Index
			![]()  
		 | 
	
| Part 2 Developing Applications by APIs | > Chapter 9 Developing Java Applications | > 9.3 How to Use Java APIs | > 9.3.2 Data Searches | 
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.

Sample 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(); | 
(1) Create a ShunPreparedStatement ObjectCreate 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.
(2) Set the Reply Start Number and the Number of Items to Return per RequestSpecify 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.
(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.
(4) Obtain the number of XML documents that match search conditionsUse the getHitCount method to obtain the number of XML documents that match the search conditions.
(5) Release the ShunResultSet Object and the ShunPreparedStatement ObjectAlways use the close methods of the ShunResultSet object and the ShunPreparedStatement object to release these objects when they are no longer required.
Sample Codes for StudioThe 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.
ShunConnection con = new ShunConnection(); ShunsakuAccessController controller = new ShunsakuAccessController(con); controller.search(key); int hitCount = controller.getHitCount(); controller.close(); con.close();  | 
public class ShunsakuAccessController {
    :
   private ShunPreparedStatement pstmt;
    :
} | 
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)
    :
} | 
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)
} | 
public void close() throws ShunException {
    :
   pstmt.close();                                               (5)
    :
} | 
			Contents
			Index
			![]()  
		 |