開発関連の覚書
chmod 4755 batch.class
#!/bin/bash # LANG=ja_JP.sjis echo ${BATCH_PATH} if [ "${BATCH_PATH}" == "" ] ; then export BATCH_PATH=/home/java/test/ fi argv[0]="/home/java/tmp/tmp.sql" argv[1]="/home/java/tmp/batch.o" argv[2]="connect to sampledb user db2inst1 using db2inst1;" argv[3]="LOAD from ./test.csv of DEL INSERT INTO test_table set integrity pending cascade deferred;" argv[4]="terminate;" max=${#argv[@]} n=0 until [ $n -eq $max ] do c=$c${argv[$n]}' ' n=$(($n + 1)) done java -cp /home/java/batch.class ${c} echo 'ret=' $? if [ "$?" = "8" ] ; then exit 1 fi exit 0
#include#include main() { if ( execlp("db2", "db2", "-tvf", "./tmp.sql",NULL ) == -1 ) { perror("exec error"); exit(1); } else { exit(0); } }
import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Iterator; /** * バッチ処理サンプル */ public class Batch { public Batch() { } public static void main(String argv[]) { ArrayListp_argList = new ArrayList (); String p_line = ""; for(int i=2; i 0 ) { p_argList.add(p_line); p_line=""; } } try { // ファイルオブジェクトを生成する FileOutputStream fileOutputStream = new FileOutputStream( argv[0] ); OutputStreamWriter outputStreamWriter = new OutputStreamWriter( fileOutputStream); BufferedWriter bufferedWriter = new BufferedWriter( outputStreamWriter); Iterator p_ite = p_argList.iterator(); while(p_ite.hasNext()) { p_line = (String)p_ite.next(); bufferedWriter.write(p_line); bufferedWriter.newLine(); } // ファイルオブジェクトを破棄する bufferedWriter.close(); outputStreamWriter.close(); fileOutputStream.close(); // 一連のファイル処理はIOExceptionが発生する } catch (IOException ex) { ex.printStackTrace(); } if ( !shellExec(argv[1]) ) { System.exit(8); } System.exit(0); } /** * パラメータで指定されたシェルを実行する */ private static boolean shellExec(String x_name) { try { String[] p_command = { x_name }; Process p_proc = Runtime.getRuntime().exec(p_command); int p_status = p_proc.waitFor(); if (p_status != 0) { System.out.println("error = " + p_status); if (p_status==2) { // ペンディング状態は無視 return true; } return false; } } catch (java.io.IOException e) { return false; } catch (InterruptedException e) { return false; } return true; } }
./test.sh if [ $? != 0 ] ; then echo "test.shでエラーが発生しました。" exit 1 fi
JAVA_DATE=$(java jp/co/test/JavaDate)
public class JavaDate { public JavaDate() { } public static void main(String[] argv) { // 日付文字列を返します System.out.println("20070101"); } }