| Interstage Shunsaku Data Manager Application Development Guide - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX - |
Contents
Index
![]()
|
| Appendix F Sample Java Programs | > F.1 Searching Data |
Part of the data obtained as search results is sometimes used as additional conditions to perform the next search operation. In this case, it is common to obtain only part of the data as search results instead of the entire data.
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, as well as to get partial information.
Search Conditions'I want to know the names and rates of up to 30 hotels that have a vacancy on 2006/07/18.
Specify the date (2006/07/18) and the location (Sydney) as search conditions. Also specify the hotel name and rate for the search result, and execute the search.
An Example of Using the APIsThe 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 and the data that correspond to the specified search conditions ***/
public class JavaAPISample2 {
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";
// 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);
// Execute search and create ShunResultSet object
rs = pstmt.executeSearch();
// Obtain number of hits
iHitNum = rs.getHitCount();
System.out.println("Number of hits = " + iHitNum);
// 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();
}
}
}
} |
Execution Results
Number of hits = 3 [Result] Item No. 1 = <document><name>Hotel 1</name><price>350</price></document> [Result] Item No. 2 = <document><name>Hotel 2</name><price>150</price></document> [Result] Item No. 3 = <document><name>Hotel 3</name><price>250</price></document> |
Contents
Index
![]()
|