简体   繁体   English

从Java执行时,MYSQL命令给出错误。 相同的命令在Linux Terminal上可以正常工作。 可能是什么原因?

[英]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版本之间的三个区别:

  1. The Java version adds an additional argument of dbname , Java版本添加了dbname的附加参数,
  2. --xml -e is passed as a single argument in the Java version but as two arguments in the terminal version --xml -e在Java版本中作为单个参数传递,而在终端版本中作为两个参数传递
  3. You have quotes around the query in the Java version 您在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.

相关问题 java 命令在 linux 中抛出错误,但在 windows 中运行良好 - java command throwing an error in linux although works fine in windows 除了这个命令(合并)之外,ffmpeg大部分时间在java中工作正常,这个命令在终端中工作正常 - ffmpeg works fine most of the time in java except for this command (merging), and this command works fine directly in terminal mysqldump从java运行时返回代码6,但同样的命令从命令行运行正常 - mysqldump returns code 6 when run from java, but the same command works fine from command line 通过java运行linux命令。 - Run linux command through java. 为什么Java Processbuilder不能在其自己的目录中运行PMCMD(informatica命令),但可以在命令提示符下正常运行 - Why PMCMD (informatica command) is not run by Java Processbuilder from its own directory while it works fine with command prompt 从Java在终端中执行Linux命令 - Executing a Linux command in Terminal from Java 从 src/main/resources 读取会在命令行上给出 java.nio.file.InvalidPathException (但在 Eclipse 中工作正常) - Reading from src/main/resources gives java.nio.file.InvalidPathException on command line (but works fine in Eclipse) 我有不同版本的javac和java。 我正在尝试在终端上运行.java文件。 但是它可以在日食中工作 - I have different versions of javac and java. I'm trying to run my .java file on terminal. But it works in eclipse mySQL查询提供了Java中的语法错误,但在mySQL工作台中工作正常 - mySQL query gives syntax error in java, but works fine in mySQL workbench 从终端执行的命令,使用ProcessBuilder从Java失败 - Command executed from terminal, failed from Java using ProcessBuilder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM