java调用sqlplus执行批量脚本获得输出结果和调用.bat文件
2018-03-05 10:42
966 查看
//FileOutputStream fos = null;
InputStream in = null;
Process p = null;
//BufferedReader br=null;
InputStreamReader isr=null;
StringBuffer sb = new StringBuffer();
sb.append(sqlPlus+"\\sqlplus ");
//sb.append(username);
if(username.toUpperCase().contains("SYS")){
String[] s=username.split(" ");
sb.append(s[0]);
sb.append("/");
sb.append(password+" ");
sb.append(s[1]+" "+s[2]);
}else{
sb.append(username);
sb.append("/");
sb.append(password);
}
//sb.append("/");
//sb.append(password);
sb.append("@");
//host示例:127.0.0.1:1521/orcl
sb.append(host);
sb.append(" @");
sb.append(sqlScriptPath);
String cmd = sb.toString();
Runtime rt = Runtime.getRuntime();
p = rt.exec(cmd);
in = p.getInputStream();
/*String logPath="D:\\sql.txt";
File file = new File(logPath);
if (!file.exists()) {
file.createNewFile();
}
fos = new FileOutputStream(logPath,false);
//预防脚本名为中文,在log里乱码
OutputStreamWriter oStreamWriter =new OutputStreamWriter(fos,"GBK");
String s="\r\n------------------------------"+sqlScriptPath+"-----------------------------";
oStreamWriter.write(s);
oStreamWriter.flush();
text.append("正在执行文件:"+sqlScriptPath);*/
isr= new InputStreamReader(in, Charset.forName("GBK"));
char[] b = new char[4096];
int n=-1;
while((n = isr.read(b)) != -1){
String str = new String(b, 0, n);
int i = str.indexOf("SP2-0310");
int j = str.indexOf("SQL>");
//fos.write(str.getBytes(), 0, n);
System.out.println(str);
//text.append(str+"\r\n");
if(str.contains("ORA")){
p.destroy();
throw new MyException("执行该脚本失败");
}
if (i != -1) {
p.destroy();
System.out.println("执行中断:" +sqlScriptPath+ str+"\n");
}
if (j != -1) {
p.destroy();
System.out.println("成功执行"+sqlScriptPath+"\n");
}
}
/*byte[] b = new byte[4096];
int br = 0;
while ((br = in.read(b)) != -1) {
String str = new String(b, 0, br);
String s=new String(str.getBytes("US-ASCII"),"gbk");
int i = str.indexOf("SP2-0310");
int j = str.indexOf("SQL>");
//fos.write(b, 0, br);
System.out.println(s);
if (i != -1) {
p.destroy();
System.out.println("执行中断:" +sqlScriptPath+ str);
}
if (j != -1) {
p.destroy();
System.out.println("成功执行"+sqlScriptPath);
}
}*/
p.waitFor();
//fos.flush();
//fos.close();
isr.close();
//br.close();
in.close();
p.destroy();
try {
/*if (br != null) {
br.close();
}*/
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
java调用.bat文件:
Runtime.getRuntime().exec("rundll32 url.dll FileProtocolHandler file://" + path);
InputStream in = null;
Process p = null;
//BufferedReader br=null;
InputStreamReader isr=null;
StringBuffer sb = new StringBuffer();
sb.append(sqlPlus+"\\sqlplus ");
//sb.append(username);
if(username.toUpperCase().contains("SYS")){
String[] s=username.split(" ");
sb.append(s[0]);
sb.append("/");
sb.append(password+" ");
sb.append(s[1]+" "+s[2]);
}else{
sb.append(username);
sb.append("/");
sb.append(password);
}
//sb.append("/");
//sb.append(password);
sb.append("@");
//host示例:127.0.0.1:1521/orcl
sb.append(host);
sb.append(" @");
sb.append(sqlScriptPath);
String cmd = sb.toString();
Runtime rt = Runtime.getRuntime();
p = rt.exec(cmd);
in = p.getInputStream();
/*String logPath="D:\\sql.txt";
File file = new File(logPath);
if (!file.exists()) {
file.createNewFile();
}
fos = new FileOutputStream(logPath,false);
//预防脚本名为中文,在log里乱码
OutputStreamWriter oStreamWriter =new OutputStreamWriter(fos,"GBK");
String s="\r\n------------------------------"+sqlScriptPath+"-----------------------------";
oStreamWriter.write(s);
oStreamWriter.flush();
text.append("正在执行文件:"+sqlScriptPath);*/
isr= new InputStreamReader(in, Charset.forName("GBK"));
char[] b = new char[4096];
int n=-1;
while((n = isr.read(b)) != -1){
String str = new String(b, 0, n);
int i = str.indexOf("SP2-0310");
int j = str.indexOf("SQL>");
//fos.write(str.getBytes(), 0, n);
System.out.println(str);
//text.append(str+"\r\n");
if(str.contains("ORA")){
p.destroy();
throw new MyException("执行该脚本失败");
}
if (i != -1) {
p.destroy();
System.out.println("执行中断:" +sqlScriptPath+ str+"\n");
}
if (j != -1) {
p.destroy();
System.out.println("成功执行"+sqlScriptPath+"\n");
}
}
/*byte[] b = new byte[4096];
int br = 0;
while ((br = in.read(b)) != -1) {
String str = new String(b, 0, br);
String s=new String(str.getBytes("US-ASCII"),"gbk");
int i = str.indexOf("SP2-0310");
int j = str.indexOf("SQL>");
//fos.write(b, 0, br);
System.out.println(s);
if (i != -1) {
p.destroy();
System.out.println("执行中断:" +sqlScriptPath+ str);
}
if (j != -1) {
p.destroy();
System.out.println("成功执行"+sqlScriptPath);
}
}*/
p.waitFor();
//fos.flush();
//fos.close();
isr.close();
//br.close();
in.close();
p.destroy();
try {
/*if (br != null) {
br.close();
}*/
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
java调用.bat文件:
Runtime.getRuntime().exec("rundll32 url.dll FileProtocolHandler file://" + path);
相关文章推荐
- sqlplus执行脚本,并把结果输出到文件
- 在shell中调用sqlplus输出sql执行结果到指定文件
- java通过cmd命令调用sqlplus执行sql脚本
- Java代码中调用shell脚本和python脚本并获得输出结果(分为小数据量和大数据量)
- 怎么用批处理*.bat 执行文件目录下面的SQL语句脚本(批量的)。。。@echo off是什么意思?
- Java掉执行sql脚本的.bat文件
- vbs 批量修改文件,bat 批处理文件调用执行vbs,并在cmd窗口打印返回值(vbs运行结果)
- java调用.bat,.cmd或执行cmd命令并将输出结果显示的代码
- 怎样把sqlplus执行的查询结果重定向输出到一个文件里面去?
- 如何在脚本中执行SQL语句并获得结果输出?
- vbs 批量修改文件,bat 批处理文件调用执行vbs,并在cmd窗口打印返回值(vbs运行结果)
- java调用cmd并执行批处理(bat)文件
- Java 执行 SQL 脚本文件
- java中调用其他文件(可执行程序,word,浏览器,bat……等)
- 从Linux程序中执行shell(程序、脚本)并获得输出结果(转)
- java调用DOS并获取执行中bat的输出
- 从Linux程序中执行shell(程序、脚本)并获得输出结果(转)
- Java 执行 SQL 脚本文件
- java调用shell脚本,并获得结果集
- 运用ANT地SQL Task来完成自己地Java执行SQL脚本文件地功能