gpt4 book ai didi

java - 从 Java 运行导入数据库 DOS 命令

转载 作者:行者123 更新时间:2023-12-01 14:41:16 25 4
gpt4 key购买 nike

我正在尝试从 Java 程序运行数据库导入命令,如下所示:

public class Test {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String[] str = {"imp ASKUL/askul@ASKDB file=askdbinstall.dmp log=askul.log fromuser=askul touser=ASKUL full=N ignore=Y grants=Y indexes=Y;"};
Process pro;
try {
pro = Runtime.getRuntime().exec(str);
} catch (Exception e) {
System.out.println(e);
}
}
}

错误输出是:

java.io.IOException: Cannot run program "imp ASKUL/askul@ASKDB file=askdbinstall.dmp log=askul.log fromuser=askul touser=ASKUL full=N ignore=Y grants=Y indexes=Y;": CreateProcess error=2, The system cannot find the file specified

文件askdbinstall.dmp 存在,因为如果我在CMD 中粘贴相同的命令,它会导入数据库转储,效果很好。我的错误是什么?

添加:根据 Reimius 的建议,我也尝试过这个:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
public class Tes {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
String [] cmd = {"imp", "ASKUL/askul@ASKDB file=askdbinstall.dmp",
"log=askul.log", "fromuser=askul", "touser=ASKUL",
"full=N ignore=Y grants=Y indexes=Y;"};
Process process = Runtime.getRuntime().exec(cmd);
InputStream in = process.getInputStream();
InputStreamReader ins=new InputStreamReader(in);
BufferedReader br = new BufferedReader(ins);
String data = null;
while ((data = br.readLine()) != null) {
System.out.println(data);
}
} catch (Exception e) {
System.out.println(e);
}
}
}

输出

run:
BUILD SUCCESSFUL (total time: 3 seconds)

没有进行导入。

最佳答案

您的导入命令String 被视为单个命令。尝试分解 token 。另请检查 Process#getErrorStream 输出的内容:

String[] str = {"imp", "ASKUL/askul@ASKDB file=askdbinstall.dmp", 
"log=askul.log", "fromuser=askul", "touser=ASKUL",
"full=N ignore=Y grants=Y indexes=Y;"};

process = Runtime.getRuntime().exec(str);
BufferedReader in =
new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.err.println(line);
}

旁白:ProcessBuilder 使参数传递的使用更加容易。

关于java - 从 Java 运行导入数据库 DOS 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15927411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com