データを追加する場合のJava APIの使用例を示します。
「神奈川のホテル情報(ホテル9の情報)を1件追加したい。」
追加したいデータを組み立て、データを追加します。
以下にJava APIを使用したプログラミング例を示します。
import com.fujitsu.shun.ShunConnection;
import com.fujitsu.shun.ShunPreparedStatement;
import com.fujitsu.shun.common.ShunException;
/*** 指定されたデータを追加します ***/
public class JavaAPISample6 {
public static void main(String[] args) {
ShunConnection con = null;
ShunPreparedStatement pstmt = null;
try {
// ShunConnectionオブジェクトを作成
con = new ShunConnection("DirSvr1", 23101);
// データ追加用にShunPreparedStatementオブジェクトを作成
pstmt = con.prepareInsert();
// 追加するデータを作成
String addData =
"<document>"
+ " <base>"
+ " <name>ホテル9</name>"
+ " <prefecture>新横浜</prefecture>"
+ " <address>神奈川県横浜市神奈川区</address>"
+ " <detail>http://xxxxx.co.jp</detail>"
+ " <price>6000</price>"
+ " </base>"
+ " <information>"
+ " <date>2006年07月18日</date>"
+ " </information>"
+ "<note>バス付 トイレ付 禁煙ルーム選択可 地下鉄 △△駅徒歩05分</note>"
+ "</document>";
// データを追加
pstmt.add(addData);
// データ追加を実行
pstmt.executeInsert();
System.out.println("追加終了");
pstmt.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 (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 (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();
}
}
}
} |
追加終了