| 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 |
When performing a search, the user sometimes prefers to obtain the search results after the value of a specific element has been aggregated.
To show how the Java APIs are used, the following example specifies the location in the search conditional expression and extracts the cheapest accommodation rate, the most expensive accommodation rate, and the average accommodation rate.
Search Conditions'Among the hotels with a vacancy on 2006/07/18, I want to use aggregation to obtain the cheapest hotel, the most expensive hotel and the average hotel rate according to district.'
Specify the date (2006/07/18) as the search condition and then apply aggregation expressions (min, max and avg) to the results to obtain aggregated results.
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 data corresponding to the specified search conditions after it has been aggregated ***/
public class JavaAPISample5 {
public static void main(String[] args) {
ShunConnection con = null;
ShunPreparedStatement pstmt = null;
ShunResultSet rs = null;
try {
// Search conditional expression
String sQuery = "/document/information/date == '2006/07/18'";
// Return expression
String sReturn = "/document/base/city/text(), min(/document/base/price/text()), max(/document/base/price/text()), avg(/document/base/price/text())";
// Sort expression
String sSort = "/document/base/city/text()";
// Create ShunConnection object
con = new ShunConnection("DirSvr1", 33101);
// Specify search expression and create ShunPreparedStatement object
pstmt = con.prepareSearch(sQuery, sReturn);
// Specify sort expression
pstmt.setSort(sSort);
// Set maximum number of items to obtain
pstmt.setRequest(1, 30);
// Execute search and create ShunResultSet object
rs = pstmt.executeSearch();
// Obtain data that corresponds to search conditions
int counter = 0;
String[][] result;
while (rs.next()) {
counter++;
System.out.println("[Result] Item No. " + counter );
result = rs.getStringArray();
System.out.println(" District :" + result[0][0]);
System.out.println(" Cheapest rate:" + result[1][0]);
System.out.println(" Most expensive rate:" + result[2][0]);
System.out.println(" Average rate:" + result[3][0]);
}
rs.close();
pstmt.close();
con.close();
}
// Processing to perform if an error occurs while the application is running
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
[Result] Item No. 1: District: Adelaide Cheapest rate: 100 Most expensive rate: 300 Average rate: 200 [Result] Item No. 2: District: Sydney Cheapest rate: 150 Most expensive rate: 350 Average rate: 250 |
Contents
Index
![]()
|