runtime - get mysqldump error message from java -
i dumped data using java runtime.exec.
now want handle exceptions "mysqldump: got error: 1045: access denied user 'errre'@'localhost' (using password: yes) when trying connect"
when put error username or password or bad condition
but got process.geterrorstream() "warning: using password on command line interface can insecure." , getinputstream() null
in terminal, output this:
mysqldump -uerror -p123456 erp_sys warning: using password on command line interface can insecure. mysqldump: got error: 1045: access denied user 'error'@'localhost' (using password: yes) when trying connect
i did research, still can't find way mysqldump error.anyone has experiences this?
i don't know. works me:
import java.io.bytearrayoutputstream; import java.io.inputstream; import java.io.outputstream; import java.nio.charset.charset; import org.apache.commons.io.ioutils; public class mysqltest { public static void main(string[] args) throws exception { process p = new processbuilder("mysqldump", "-u", "error", "-p123456", "erp_sys").start(); bytearrayoutputstream dmp = new bytearrayoutputstream(); // use fileoutputstream dmp = ... in real cases. thread t1 = copystreamsinbackground(p.getinputstream(), dmp); bytearrayoutputstream err = new bytearrayoutputstream(); thread t2 = copystreamsinbackground(p.geterrorstream(), err); t1.join(); t2.join(); int exitcode = p.waitfor(); if (exitcode != 0) { system.err.println("exit code: " + exitcode); string errors = new string(err.tobytearray(), charset.forname("utf-8")); system.err.println(errors); } else { system.out.println("exit code: " + exitcode); string dumps = new string(dmp.tobytearray(), charset.forname("utf-8")); system.out.println(dumps); } } private static thread copystreamsinbackground(final inputstream is, final outputstream os) { thread t = new thread(new runnable() { @override public void run() { try { ioutils.copy(is, os); os.close(); } catch (exception ex) { ex.printstacktrace(); throw new illegalstateexception(ex); } } }); t.start(); return t; } }
it prints:
exit code: 2 mysqldump: got error: 1045: access denied user 'error'@'localhost' (using password: yes) when trying connect
Comments
Post a Comment