検索操作において、検索した結果をある特定の要素をキーとしてソートして取得したい場合があります。
日付と場所を検索条件式に指定して、条件に一致するホテルの件数と部分情報を取得します。
データを取得する際に、宿泊費の高い順に取り出す例を用いて、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 JavaAPISample4 {
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 = "/document/base/name, /document/base/price";
// ソート式
String sSort = "val(/document/base/price/text()) DESC";
// ヒット件数
int iHitNum = 0;
// データ件数のカウンタ
int iDataCounter = 1;
// ShunConnectionオブジェクトを作成
con = new ShunConnection("DirSvr1", 23101);
// 検索式を指定し、ShunPreparedStatementオブジェクトを作成
pstmt = con.prepareSearch(sQuery, sReturn);
//返信要求件数を設定
pstmt.setRequest(1, 30);
// ソート式を指定
pstmt.setSort(sSort);
//検索を実行し、ShunResultSetオブジェクトを作成
rs = pstmt.executeSearch();
// 検索条件に該当するデータを、1件ずつ取得
while (rs.next()) {
System.out.println("[結果]" + 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("エラーレベル :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();
}
}
}
} |
[結果]1件目 = <document><name>ホテル1</name><price>9000</price></document> [結果]2件目 = <document><name>ホテル3</name><price>7500</price></document> [結果]3件目 = <document><name>ホテル2</name><price>6000</price></document> |