[英]MYSQL command gives error while being executed from java. Same command works fine on Linux Terminal. What can be the reason?
I want to get the foreign key and primary key information from information_schema in XML format. 我想以XML格式从information_schema获取外键和主键信息。 For that I am using below command on linux terminal . 为此,我在linux终端上使用以下命令。
mysql --user=root --password=nikunj@cloud --xml -e "select CONSTRAINT_NAME,TABLE_NAME,COLUMN_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME from information_schema.key_column_usage where table_schema='bakerydb';"
This gives result in XML format. 这将以XML格式给出结果。 Check the output. 检查输出。
<?xml version="1.0"?>
<resultset statement="select CONSTRAINT_NAME,TABLE_NAME,COLUMN_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME from information_schema.key_column_usage where table_schema='bakerydb'" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<field name="CONSTRAINT_NAME">PRIMARY</field>
<field name="TABLE_NAME">baked_goods</field>
<field name="COLUMN_NAME">id</field>
<field name="REFERENCED_TABLE_NAME" xsi:nil="true" />
<field name="REFERENCED_COLUMN_NAME" xsi:nil="true" />
</row>
...
</resultset>
But while I run the same command on linux * terminal * using Java file It gives error. 但是,当我使用Java文件在linux * terminal *上运行相同的命令时,它给出了错误。 Check Out Java file below. 在下面签出Java文件。
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class foreignKeydetailXML {
static final String USER = "root";
static final String PASS = "nikunj@cloud";
static final String DATABASE = "bakerydb";
//cli command= mysql -u root -pnikunj@cloud --xml -e
//"select CONSTRAINT_NAME,TABLE_NAME,COLUMN_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME from information_schema.key_column_usage where table_schema='bakerydb';"
static String queryLine ="select CONSTRAINT_NAME,TABLE_NAME,COLUMN_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME from information_schema.key_column_usage where table_schema=\'" + DATABASE + "\';";
public static String executeScript (String dbname, String dbuser,String dbpassword, boolean verbose) {
String output = null;
try {
String[] cmd = new String[]{"mysql",dbname,"--user=" + dbuser,"--password=" + dbpassword,"--xml -e","\"" + queryLine + "\"" };
System.err.println(cmd[0] + " " + cmd[1] + " " + cmd[2] + " " + cmd[3] + " " + cmd[4] + " " + cmd[5]);
System.out.println(cmd);
Process proc = Runtime.getRuntime().exec(cmd);
if (verbose) {
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
// read the output
String line;
while ((line = bufferedreader.readLine()) != null) {
System.out.println(line);
}
// check for failure
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " +
proc.exitValue());
}
}
catch (InterruptedException e) {
System.err.println(e);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return output;
}
public static void main(String argv[])
{
//executeScript (String dbname, String dbuser,String dbpassword, String scriptpath, boolean verbose)
executeScript("information_schema", USER,PASS,true);
}
}
What can be the reason? 可能是什么原因? It returns exit value 2. Previously with bit change in code, It was giving list of MYSQL options. 它返回退出值2。以前在代码中进行了位更改,它给出了MYSQL选项的列表。
I notice three differences between the terminal version and the Java version: 我注意到终端版本和Java版本之间的三个区别:
dbname
, Java版本添加了dbname
的附加参数, --xml -e
is passed as a single argument in the Java version but as two arguments in the terminal version --xml -e
在Java版本中作为单个参数传递,而在终端版本中作为两个参数传递 Try (I assume the dbname
is correct here so I am leaving it): 尝试(我假设dbname
在这里正确,所以我将其保留):
String[] cmd = new String[] {
"mysql",
dbname,
"--user=" + dbuser,
"--password=" + dbpassword,
"--xml",
"-e",
queryLine};
Also, it could be simply that it cannot find the command "mysql". 同样,很可能是找不到命令“ mysql”。 Verify the path
environment variable is set properly for your Java process, specify the full path to the command, or specify a working directory using the Runtime.exec(String[], String[], File)
overload . 验证是否为Java进程正确设置了path
环境变量,使用Runtime.exec(String[], String[], File)
重载来指定命令的完整路径,或指定工作目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.