データを更新する場合の、Java APIの使用例を示します。
「2006年7月18日に神奈川で宿泊可能なホテルのうち、ホテル1の情報を更新したい。」
年月日(2006年7月18日)を条件に検索を行い、ホテル名『ホテル1』と一致したデータを更新します。
以下に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; /*** 指定されたデータを更新します ***/ public class JavaAPISample8 { public static void main(String[] args) { ShunConnection con = null; ShunPreparedStatement pstmt = null; ShunPreparedRecordID rid = null; ShunResultSet rs = null; try { // 更新データのレコードID String sUpdateRecordID = null; // 更新するデータ String sUpdateData = "<document>" + " <base>" + " <name>ホテル1</name>" + " <prefecture>大阪</prefecture>" + " <address>大阪府大阪市中央区</address>" + " <detail>http://xxxxx.co.jp</detail>" + " <price>8000</price>" + " </base>" + " <information>" + " <date>2006年07月18日</date>" + " </information>" + " <note>バス付 トイレ付 地下鉄 △△駅徒歩02分</note>" + "</document>"; // 検索式 String sQuery = "/document/base/name == 'ホテル1'"; // リターン式 String sReturn = "/document/base/name/text()"; // ShunConnectionオブジェクトを作成 con = new ShunConnection("DirSvr1", 23101); // 検索式を指定し、ShunPreparedStatementオブジェクトを作成 pstmt = con.prepareSearch(sQuery, sReturn); //最大取得件数を設定 pstmt.setRequest(1, 30); // 検索を実行し、ShunResultSetオブジェクトを作成 rs = pstmt.executeSearch(); // ShunPreparedRecordIDオブジェクトを作成 rid = con.prepareUpdateRecordID(); //最大取得件数を設定 pstmt.setRequest(1, 30); // 検索を実行し、ShunResultSetオブジェクトを作成 rs = pstmt.executeSearch(); // 検索条件に該当するデータ1件を取得 while (rs.next()) { // ホテル1の情報について、レコードID、更新データを設定 if (rs.getString().equals("ホテル1")) { rid.add(rs.getRecordID(), sUpdateData); } } // データ設定に成功している場合、データを更新 if (0 < rid.getCount()) { rid.updateByRecordID(); System.out.println("更新終了"); } } // アプリケーション実行時にエラーが発生した場合の処理 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(); } } } } |
更新終了