検索操作において、対象となるデータが大量に存在する場合、条件式に一致するデータすべてを参照することは非現実的です。このため、大量のデータが存在する場合は、条件式に一致するデータ件数を取得することが必要です。
日付と場所を検索条件式に指定して、条件に一致するホテルの件数を取得する例を用いて、Java APIの使用例を示します。
「2006年7月18日に大阪で宿泊可能なホテルは何件ありますか?」
年月日(2006年7月18日)および場所(大阪)を条件に指定し、検索を実行します。
以下にJava APIを使用したプログラミング例を示します。
import com.fujitsu.shun.ShunConnection;
import com.fujitsu.shun.ShunPreparedStatement;
import com.fujitsu.shun.ShunResultSet;
import com.fujitsu.shun.common.ShunException;
/*** 指定された検索条件に該当するレコード件数を取得します ***/
public class JavaAPISample1 {
public static void main(String[] args) {
ShunConnection con = null;
ShunPreparedStatement pstmt = null;
ShunResultSet rs = null;
try {
// 検索条件式
String sQuery = "/document/base/prefecture == '大阪' AND /document/information/date == '2006年07月18日'"; // リターン式
String sReturn = "/";
// ヒット件数
int iHitNum = 0;
// ShunConnectionオブジェクトを作成
con = new ShunConnection("DirSvr1", 23101);
// 検索式を指定し、ShunPreparedStatementオブジェクトを作成
pstmt = con.prepareSearch(sQuery, sReturn);
//返信要求件数を設定
pstmt.setRequest(1, 0);
//検索を実行し、ShunResultSetオブジェクトを作成
rs = pstmt.executeSearch();
// ヒット件数取得
iHitNum = rs.getHitCount();
System.out.println("ヒット件数 = " + iHitNum);
rs.close();
pstmt.close();
con.close();
}
catch (ShunException ex) {
int errorLevel = ex.getErrLevel();
switch( errorLevel ) {
case ShunException.SHUN_ERROR :
System.out.println("エラーレベル :SHUN_ERROR");
break;
case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED");
break;
}
System.out.println("エラーメッセージ:" + ex.getMessage());
ex.printStackTrace();
}
catch (Exception ex) {
System.out.println("エラーメッセージ:" + 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("エラーレベル :SHUN_ERROR");
break;
case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED");
break;
}
System.out.println("エラーメッセージ:" + 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("エラーレベル :SHUN_ERROR");
break;
case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED");
break;
}
System.out.println("エラーメッセージ:" + 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("エラーレベル :SHUN_ERROR");
break;
case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
System.out.println("エラーレベル :SHUN_ERROR_CONNECTION_TERMINATED");
break;
}
System.out.println("エラーメッセージ:" + ex.getMessage());
ex.printStackTrace();
}
}
}
} |
ヒット件数 = 3