ダイレクトアクセスキーを使用してデータを検索する場合のJava APIの使用例を示します。
検索するデータ
<course> <id>0001</id> <name>Business negotiation</name> <instructor> <first-name>Max</first-name> <last-name>cameron</last-name> </instructor> <capacity>40</capacity> <current-auditors>30</current-auditors> </course> |
定義したダイレクトアクセスキー
key /course/id/text()
APIの使用例
以下にJava APIを使用したプログラミング例を示します。
import com.fujitsu.shun.ShunConnection; import com.fujitsu.shun.ShunPreparedKey; import com.fujitsu.shun.ShunResultSet; import com.fujitsu.shun.common.ShunException; /*** ダイレクトアクセスキーを使用してデータを検索します ***/ public class DirectAccessSample { public static void main(String[] args) { ShunConnection con = null; ShunPreparedKey keystmt = null; ShunResultSet rs = null; try { // ダイレクトアクセスキー名 String sCoursKeyName = "key"; // ダイレクトアクセスキー String key = "0001"; // リターン式 String sCourseReturn = "/"; // ShunConnectionオブジェクトを作成 // 接続先のホスト名:DirSvr1、 // ポート番号:23101、 // Shunsaku File名:shunsakuFile1の場合 con = new ShunConnection("DirSvr1", 23101, "shunsakuFile1"); // 検索用のShunPreparedKeyオブジェクトを作成 keystmt = con.prepareSearchKey(sCoursKeyName, sCourseReturn); // 検索対象のダイレクトアクセスキーを追加 keystmt.add(key); // 検索を実行し、ShunResultSetオブジェクトを作成 rs = keystmt.searchByKey(); // ヒット件数取得 int iHitNum = rs.getHitCount(); // データ件数のカウンタ int iDataCounter = 1; System.out.println("ヒット件数 = " + iHitNum); // 検索条件に該当するデータを、1件ずつ取得 while (rs.next()) { System.out.println("[結果]" + iDataCounter + "件目"); System.out.println("ダイレクトアクセスキー:" + rs.getKey()); System.out.println("データ :"); System.out.println(rs.getString()); iDataCounter++; } rs.close(); keystmt.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(keystmt != null) keystmt.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 [結果]1件目 ダイレクトアクセスキー:0001 データ : <course> <id>0001</id> <name>Business negotiation</name> <instructor> <first-name>Max</first-name> <last-name>cameron</last-name> </instructor> <capacity>40</capacity> <current-auditors>30</current-auditors> </course> |