検索操作において、“F.1.1 検索条件に一致するXML文書の件数を求める”および“F.1.2 検索条件に一致するXML文書を指定した形式で取得する”といった操作を経て、絞り込んだ条件を基に、データすべてを取得することになります。
“F.1.2 検索条件に一致するXML文書を指定した形式で取得する”の操作により得られたホテル名を基に、該当するホテルのデータをすべて取得する例を用いて、Java APIの使用例を示します。
「2006年7月18日に大阪で宿泊可能なホテルのうち、あるホテルの詳細情報を取得したい。」
年月日(2006年7月18日)および、場所(大阪)を条件に指定し、検索を実行します。また、ホテルに対応するレコードIDを用いて、詳細情報を取得します。
以下にJava APIを使用したプログラミング例を示します。
import com.fujitsu.shun.ShunConnection; import com.fujitsu.shun.ShunPreparedRecordID; import com.fujitsu.shun.ShunPreparedStatement; import com.fujitsu.shun.ShunResultSet; import com.fujitsu.shun.common.ShunException; /*** 指定されたレコードIDに該当するレコード情報をすべて取得します ***/ public class JavaAPISample3 { public static void main(String[] args) { ShunConnection con = null; ShunPreparedStatement pstmt = null; ShunPreparedRecordID rid = null; ShunResultSet rs = null; try { // 検索条件式 String sQuery = "/document/base/prefecture == '大阪' AND /document/information/date == '2006年07月18日'"; // リターン式 String sReturn = "/document/base/name/text()"; // レコード情報 String sRecordID = ""; // ヒット件数 int iHitNum = 0; // ShunConnectionオブジェクトを作成 con = new ShunConnection("DirSvr1", 23101); // 検索式を指定し、ShunPreparedStatementオブジェクトを作成 pstmt = con.prepareSearch(sQuery, sReturn); //返信要求件数を設定 pstmt.setRequest(1, 30); //検索を実行し、ShunResultSetオブジェクトを作成 rs = pstmt.executeSearch(); // ヒット件数取得 iHitNum = rs.getHitCount(); System.out.println("ヒット件数 = " + iHitNum); // ShunPreparedRecordIDオブジェクトを作成 rid = con.prepareSearchRecordID(); // 検索条件に該当するデータ1件を取得 while (rs.next()) { // ホテル1の情報について、レコードIDを設定 if (rs.getString().equals("ホテル1")) { rid.add(rs.getRecordID()); } } rs.close(); pstmt.close(); // 該当レコードのID取得に成功している場合、詳細データを参照 if (0 < rid.getCount()) { // 指定されたレコードIDでShunResultSetオブジェクトを作成 rs = rid.searchByRecordID(); while (rs.next()) { // レコードID指定でデータを取得 System.out.println("[詳細] = " + rs.getString()); } rs.close(); } rid.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 (rid != null) rid.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 [詳細] = <document> <base> <name>ホテル1</name> <prefecture>大阪</prefecture> <address>大阪府大阪市中央区</address> <detail>http://xxxxx.co.jp</detail> <price>9000</price> </base> <information> <date>2006年07月18日</date> </information> <note>バス付 トイレ付 地下鉄 △△駅徒歩02分</note> </document> |