1:load_data_test.sh
#!/bin/sh file=$1 load_data_log=/mnt/load_data_test/load.log load_sql="LOAD DATA LOCAL INFILE '/mnt/load_data_test/${file}' IGNORE INTO TABLE t_test CHARACTER SET utf8 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '' LINES TERMINATED BY ' '" mysql 2>>${load_data_log} -uroot -pxxx --local-infile=1 << EOF use app_info; $load_sql; exit EOF echo "$?" exit
备注:$?如果为1表示脚本执行出错,为0表示脚本执行成功。
2:java代码
public static void main(String[] args) { String sh = System.getProperty("shell"); try{ Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(sh,null,null); InputStream stderr = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(stderr,"UTF-8"); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { System.out.println("执行结果:"+line); } }catch (Exception e){ e.printStackTrace(); } }
3:测试
# java -Dshell='/mnt/load_data_test/load_data_test.sh 00.txt' -jar t03.jar 执行结果:0