简体   繁体   English

无法从java中的mysql导出csv文件

[英]Not able to export csv file from mysql in java

I have a requirement to export mysql table data to be downloaded by user in CSV format. 我要求导出用户以CSV格式下载的mysql表数据。

To achieve this I am trying java runtime exec function and executing "mysql -e". 为了实现这一点,我正在尝试java运行时exec函数并执行“mysql -e”。 Unfortunately, I am stuck half way. 不幸的是,我被困在一半。 I am only able to display sql output to console but not able to route it to a file. 我只能将sql输出显示到控制台但不能将其路由到文件。

WORKING CODE 工作代码

(I am able to see records in eclipse console) (我能在eclipse控制台中看到记录)

try {
        Process p = Runtime.getRuntime().exec(new String[]
                {


                "mysql","-h","localhost","-u","admin","-pxyz","myDB","-e", "\"select concat(billing_amount,',') as 'Billing Amount,', concat(amount_paid ,',') as 'Amount Paid,' from invoice\""

                }
                );
         BufferedReader in = new BufferedReader(
                 new InputStreamReader(p.getInputStream()));
         String line = null;
         while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        //process.waitFor();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

But when I am trying to export data to a file using "> abc.txt " , the file is not created, instead I see the mySQL --help option in eclipse console. 但是当我尝试使用“> abc.txt”将数据导出到文件时,不会创建该文件,而是在eclipse控制台中看到mySQL --help选项。 What could be wrong here? 这可能有什么问题?

CODE NOT WORKING 代码不工作

try {
        Process p = Runtime.getRuntime().exec(new String[]
                {


                "mysql","-h","localhost","-u","admin","-pxyz","myDB","-e", "\"select concat(billing_amount,',') as 'Billing Amount,', concat(amount_paid ,',') as 'Amount Paid,' from invoice\"", "> abc.txt"

                }
                );
         BufferedReader in = new BufferedReader(
                 new InputStreamReader(p.getInputStream()));
         String line = null;
         while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
        //process.waitFor();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I/O redirection is handled by the shell, not by each program. I / O重定向由shell处理,而不是由每个程序处理。 Hence, mysql does not know how to handle > abc.txt . 因此, mysql不知道如何处理> abc.txt Executing a shell and passing the full command as a single argument like this would work in an Unix system: 执行shell并将完整命令作为单个参数传递,就像在Unix系统中一样:

Process p = Runtime.getRuntime().exec(new String[] {
     "sh", "-c", "mysql -h localhost -u admin -pxyz myDB -e \"select concat(billing_amount,',') as 'Billing Amount,', concat(amount_paid ,',') as 'Amount Paid,' from invoice\" > abc.txt"
});

But the better option, that does not assume an Unix shell, is to use a ProcessBuilder and the redirectOutput(File) method. 但是,不假设Unix shell的更好选择是使用ProcessBuilderredirectOutput(File)方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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