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.2 Obtaining Search Results According to the Number of Data Items

In general, Web applications do not display all the search results on a single page, but limit the number of items shown on each page.

Specify the reply start number (position) and the number of items to return per request (requestCount) in the setRequest method to control the number of items of data to be obtained.

The following diagram shows the process for obtaining search results according to the number of data items.

[Figure 9-4 Flow of the Process for Obtaining Search Results According to the Number of Data Items]

mark1Sample Code

ShunConnection con = new ShunConnection();
String queryExpression = "/document/base/prefecture == 'Sydney'";
String returnExpression = "/document/base/name, /document/base/price";
ShunPreparedStatement pstmt = con.prepareSearch(queryExpression, returnExpression);  (1)
pstmt.setRequest(11,5);                                                              (2)
ShunResultSet rs = pstmt.executeSearch();                                            (3)
System.out.println("Number) = " + rs.getHitCount());                                 (3)
while(rs.next()) {                                                                   (4)
  System.out.println("[Search results] = " + rs.getString());                        (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. If the setRequest method is omitted, the number of items to return per request will be set to the value specified in the AnsMax parameter in the conductor or director environment file.

For the number of items to return per request, specify the number of data items to display on each page.

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.

The number of hits (XML documents that match the search conditions) can be obtained using the getHitCount method. This value can be used to find such things as the number of pages of the search results.

mark2(4) Extract the Results of the Search

Always invoke the next method before extracting the results of the search. The next method returns true if there is still more data that can be extracted and false otherwise.

Different methods can be used to extract XML documents for different objectives. The following table shows the methods that can be used.

[Table 9-3 Methods that Can be Used to Extract XML Documents]

Method

Function

getString

Extracts XML documents as String objects.

getStringArray

Extracts XML documents as a two-dimensional array of String objects.

getStream

Extracts XML documents as InputStream objects.

Note: The getStringArray method is an effective way to extract search results in text format.

The getrecordID method can be used to obtain record IDs (which uniquely identify each data item) in addition to the results of the search. Record IDs are used to extract, delete, and update the corresponding XML documents.

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

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);
Object[] resultData = 
      controller.getResultData(ShunsakuAccessController.SORT_NONE, 11, 5);
controller.close();

con.close();

ShunPreparedStatement Object

public class ShunsakuAccessController {
    :
   private ShunPreparedStatement pstmt;
    :
}

search Method of Shunsaku Access Class

public void search(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.

getResultData Method of Shunsaku Access Class

public Object[] getResultData(String sortString,
                              int position,
                              int requestCount)
                                          throws ShunException {
    :
  ShunResultSet resultSet = getResultSet(sortString, position, requestCount);
    :
  Object[] resultData = parseResultSet(resultSet);                        (4)
    :
  resultSet.close();                                                      (5)
    :
}

public ShunResultSet getResultSet(String sortString,
                                  int position,
                                  int requestCount)
                                  throws ShunException {
  if(!sortString.equals(SORT_NONE)) {
      pstmt.setSort(sortString);
   }
   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