ヒット件数の上限値を設定する場合のJava APIの使用例を示します。
APIの使用例
以下に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 JavaAPISample9 { public static void main(String[] args) { ShunConnection con = null; ShunPreparedStatement pstmt = null; ShunResultSet rs = null; try { // 検索条件式 String sQuery = "/document/base/name = 'ホテル'"; // リターン式 String sReturn = "/document/base/name/text(), /document/base/price/text()"; // ソート式 String sSort = "/document/base/price/text()"; // ヒット件数 int iHitNum = 0; // データ件数のカウンタ int iDataCounter = 1; // ShunConnectionオブジェクトを作成 con = new ShunConnection("DirSvr1", 23101); // 検索式を指定し、ShunPreparedStatementオブジェクトを作成 pstmt = con.prepareSearch(sQuery, sReturn); // ソート式を指定 pstmt.setSort(sSort); // 最大取得件数を設定 pstmt.setRequest(1, 5); // ヒット件数リミッタを設定 pstmt.setHitCountLimit(5); // 検索を実行し、ShunResultSetオブジェクトを作成 rs = pstmt.executeSearch(); // 検索条件に該当するデータを取得 // ヒット件数取得 iHitNum = rs.getHitCount(); System.out.println("ヒット件数 = " + iHitNum); if(rs.isHitCountLimitOver()) { System.out.print("ヒット件数リミッタをオーバーしました。"); } // 検索条件に該当するデータを、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(); } } } } |
ヒット件数 = 8 ヒット件数リミッタをオーバーしました。 |