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.3 Advanced Applications

F.3.2 Searching Data Using Direct Access Key

The following is a sample program to search data with the direct access key using the Java APIs.

mark1Data to be Searched

<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>

mark1Defined Direct Access Key

key /course/id/text()

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.ShunPreparedKey;
import com.fujitsu.shun.ShunResultSet;
import com.fujitsu.shun.common.ShunException;

/*** Search data using direct access key ***/
public class DirectAccessSample {

        public static void main(String[] args) {
                ShunConnection con = null;
                ShunPreparedKey keystmt = null;
                ShunResultSet rs = null;
                try {
                         // Direct access key name 
                         String sCoursKeyName = "key";
                         // Direct access key
                         String key = "0001";
                         // Return expression
                         String sCourseReturn = "/";

                         // Create ShunConnection object
                         // Host name for the connection destination:DirSvr1,
                         // Port number:33101,
                         // If the Shunsaku File name is shunsakuFile1 
                         con = new ShunConnection("DirSvr1", 33101, "shunsakuFile1");

                         // Create ShunPreparedKey object for searching data
                         keystmt = con.prepareSearchKey(sCoursKeyName, sCourseReturn);

                         // Add the direct acess key to be searched
                         keystmt.add(key);

                         // Execute search and create ShunResultSet object
                         rs = keystmt.searchByKey();

                         // Obtain number of hits
                         int iHitNum = rs.getHitCount();
                         // Data counter
                         int iDataCounter = 1;

                         System.out.println("Number of hits       = " + iHitNum);
                         // Obtain the data corresponding to the search conditions one item at a time
                         while (rs.next()) {
                                 System.out.println("[Result] Item No." + iDataCounter);
                                 System.out.println("Direct access key:" + rs.getKey());
                                 System.out.println("Data                :");
                                 System.out.println(rs.getString());
                                 iDataCounter++;
                         }

                         rs.close();
                         keystmt.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();
                  }
                  // Recovery procedure in the event of an error 
                  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(keystmt != null) keystmt.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

Number of hits       = 1
[Result] Item No.1
Direct access key:0001
Data                :
<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>

Contents Index PreviousNext

All Rights Reserved, Copyright(C) FUJITSU LIMITED 2006