import com.fujitsu.shun.ShunConnection;
import com.fujitsu.shun.ShunPreparedKey;
import com.fujitsu.shun.ShunResultSet;
import com.fujitsu.shun.common.ShunException;
/*** ダイレクトアクセスキーを使用してデータを検索します ***/
public class DirectAccessSample {
public static void main(String[] args) {
ShunConnection con = null;
ShunPreparedKey keystmt = null;
ShunResultSet rs = null;
try {
// ダイレクトアクセスキー名
String sCoursKeyName = "key";
// ダイレクトアクセスキー
String key = "0001";
// リターン式
String sCourseReturn = "/";
// ShunConnectionオブジェクトを作成
// 接続先のホスト名:DirSvr1、
// ポート番号:33101、
// Shunsaku File名:shunsakuFile1の場合
con = new ShunConnection("DirSvr1", 33101, "shunsakuFile1");
// 検索用のShunPreparedKeyオブジェクトを作成
keystmt = con.prepareSearchKey(sCoursKeyName, sCourseReturn);
// 検索対象のダイレクトアクセスキーを追加
keystmt.add(key);
// 検索を実行し、ShunResultSetオブジェクトを作成
rs = keystmt.searchByKey();
// ヒット件数取得
int iHitNum = rs.getHitCount();
// データ件数のカウンタ
int iDataCounter = 1;
System.out.println("ヒット件数 = " + iHitNum);
// 検索条件に該当するデータを、1件ずつ取得
while (rs.next()) {
System.out.println("[結果]" + iDataCounter + "件目");
System.out.println("ダイレクトアクセスキー:" + rs.getKey());
System.out.println("データ :");
System.out.println(rs.getString());
iDataCounter++;
}
rs.close();
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(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(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();
}
}
}
} |