Interstage Shunsaku Data Manager Application Development Guide - Microsoft(R) Windows(R) 2000/ Microsoft(R) Windows Server(TM) 2003 - - UNIX -
Contents Index PreviousNext

Appendix F Sample Java Programs> F.2 Updating Data

F.2.3 Updating Data

The following example shows how the Java APIs are used to update data.

mark1Search Conditions

'Of the hotels in Sydney that have a vacancy on 2006/07/18, I want to update the information relating to Hotel 1.'

Perform a search using the date (2006/07/18) as the search condition, then update data whose name matches "Hotel 1".

mark1An Example of Using the APIs

The following is a sample program using the Java APIs.

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;

/***     Update specified data     ***/
public class JavaAPISample8 {

  public static void main(String[] args) {
    ShunConnection con = null;
    ShunPreparedStatement pstmt = null;
    ShunPreparedRecordID rid = null;
    ShunResultSet rs = null;

    try {
      // Record ID of data to be updated
      String sUpdateRecordID = null;
      // Data to be updated
      String sUpdateData =
        "<document>"
          + "    <base>"
          + "        <name>Hotel 1</name>"
          + "        <city>Sydney</city>"
          + "        <address>Bondi Beach</address>"
          + "        <detail>http://xxxxx.com.au</detail>"
          + "        <price>300</price>"
          + "    </base>"
          + "    <information>"
          + "        <date>2006/07/18</date>"
          + "    </information>"
          + "    <note>En-suite bathroom and toilet, two minutes' walk to train station XX</note>"
          + "</document>";
      // Search expression
      String sQuery = "/document/base/name == 'Hotel 1'";
      // Return expression
      String sReturn = "/document/base/name/text()";

      // Create ShunConnection object
      con = new ShunConnection("DirSvr1", 33101);

      // Specify search expression and create ShunPreparedStatement object
      pstmt = con.prepareSearch(sQuery, sReturn);

      //Set maximum number of items to obtain
      pstmt.setRequest(1, 30);

      // Execute search and create ShunResultSet object
      rs = pstmt.executeSearch();

      // Create ShunPreparedRecordID object
      rid = con.prepareUpdateRecordID();

      //Set maximum number of items to obtain
      pstmt.setRequest(1, 30);

      // Execute search and create ShunResultSet object
      rs = pstmt.executeSearch();

      // Obtain one item of data that corresponds to search conditions
      while (rs.next()) {
       // Set the record ID and data to update for Hotel 1 information
        if (rs.getString().equals("Hotel 1")) {
          rid.add(rs.getRecordID(), sUpdateData);
        }
      }
      // Update the data if the data was set successfully 
      if (0 < rid.getCount()) {
        rid.updateByRecordID();
        System.out.println("Update complete");
      }
    }
    // Processing to perform if an error occurs while the application is running
    catch (ShunException ex) {
            int errorLevel = ex.getErrLevel();
      switch( errorLevel ) {
        case ShunException.SHUN_ERROR :
          System.out.println("Error level  :SHUN_ERROR");
          break;
        case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
          System.out.println("Error level  :SHUN_ERROR_CONNECTION_TERMINATED");
          break;
      }
      System.out.println("Error message:" + ex.getMessage());
      ex.printStackTrace();
    }
    catch (Exception ex) {
      System.out.println("Error message:" + 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("Error level  :SHUN_ERROR");
          break;
        case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
          System.out.println("Error level  :SHUN_ERROR_CONNECTION_TERMINATED");
          break;
      }
        System.out.println("Error message:" + 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("Error level  :SHUN_ERROR");
          break;
        case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
          System.out.println("Error level  :SHUN_ERROR_CONNECTION_TERMINATED");
          break;
      }
        System.out.println("Error message:" + 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("Error level  :SHUN_ERROR");
          break;
        case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
          System.out.println("Error level  :SHUN_ERROR_CONNECTION_TERMINATED");
          break;
      }
        System.out.println("Error message:" + 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("Error level  :SHUN_ERROR");
          break;
        case ShunException.SHUN_ERROR_CONNECTION_TERMINATED :
          System.out.println("Error level  :SHUN_ERROR_CONNECTION_TERMINATED");
          break;
      }
        System.out.println("Error message:" + ex.getMessage());
        ex.printStackTrace();
      }
    }
  }
}

mark1Execution Results

Update complete

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006