简体   繁体   中英

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. For that I am using below command on linux terminal .

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. 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. Check Out Java file below.

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.

I notice three differences between the terminal version and the Java version:

  1. The Java version adds an additional argument of dbname ,
  2. --xml -e is passed as a single argument in the Java version but as two arguments in the terminal version
  3. You have quotes around the query in the Java version

Try (I assume the dbname is correct here so I am leaving it):

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". 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM