import com.fujitsu.shun.ShunConnection;
import com.fujitsu.shun.ShunPreparedStatement;
import com.fujitsu.shun.ShunResultSet;
import com.fujitsu.shun.common.ShunException;
/*** Set the maximum value of number of hits ***/
public class JavaAPISample9 {
public static void main(String[] args) {
ShunConnection con = null;
ShunPreparedStatement pstmt = null;
ShunResultSet rs = null;
try {
// Search conditional expression
String sQuery = "/document/base/name = 'Hotel'";
// Return expression
String sReturn = "/document/base/name/text(), /document/base/price/text()";
// Sort expression
String sSort = "/document/base/price/text()";
// 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 sort expression
pstmt.setSort(sSort);
// Set maximum number of items to obtain
pstmt.setRequest(1, 5);
// Set the limiter of number of hits
pstmt.setHitCountLimit(5);
// Execute search and create ShunResultSet object
rs = pstmt.executeSearch();
// Obtain data that corresponds to search conditions
// Obtain number of hits
iHitNum = rs.getHitCount();
System.out.println("Number of hits = " + iHitNum);
if(rs.isHitCountLimitOver()) {
System.out.print("The number of hits has exceeded the limiter of number of hits.");
}
// Obtain the data corresponding to the search condition one item at a time
while (rs.next()) {
System.out.println("[Result]" + iDataCounter + "Item No. = " + rs.getString());
iDataCounter++;
}
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();
}
}
}
} |