ダイレクトアクセスキーを使用してデータを更新する場合の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.common.ShunException; /*** ダイレクトアクセスキーを使用してデータを更新します ***/ public class DirectAccessSample2 { public static void main(String[] args) { ShunConnection con = null; ShunPreparedKey keystmt = null; try { //ダイレクトアクセスキー名 String sCoursKeyName = "key"; //ダイレクトアクセスキー String key = "0001"; //更新データ String updateCourseData = "<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>31</current-auditors>" + "</course>"; // ShunConnectionオブジェクトを作成 // 接続先のホスト名:DirSvr1、 // ポート番号:23101、 // Shunsaku File名:shunsakuFile1の場合 con = new ShunConnection("DirSvr1", 23101, "shunsakuFile1"); //更新用のShunPreparedKeyオブジェクトを作成 keystmt = con.prepareUpdateKey(sCoursKeyName); //更新データを追加 keystmt.add(key, updateCourseData); //コース情報の更新 keystmt.updateByKey(); System.out.println("更新終了"); 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(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(); } } } } |
実行結果
更新終了