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.2 Deleting Data

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

'I want to delete the data relating to Hotel 9 from the hotels in Adelaide that have a vacancy on 2006/07/18.'

Perform a search using the date (2006/07/18) and location (Adelaide) as conditions, then delete data whose name matches "Hotel 9".

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;

/***     Delete specified data     ***/
public class JavaAPISample7 {

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

    try {
      // Search expression
      String sQuery = "/document/base/city == 'Adelaide' AND /document/information/date == '2006/07/18'";

      // 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);

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

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

      // Execute search and create ShunResultSet object
      rs = pstmt.executeSearch();
      // Obtain record ID of Hotel 9
      while (rs.next()) {
        if (rs.getString().equals("Hotel 9")) {
          rid.add(rs.getRecordID());
        }
      }
      rs.close();
      pstmt.close();

      // If record ID was obtained successfully, delete the corresponding data 
      if (0 < rid.getCount()) {
        rid.deleteByRecordID();
        System.out.println("Deletion complete");
      }
      rid.close();
      con.close();
    }
    // 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

Deletion complete

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006