mysql - How to give input in cmd commands from Java -
i new console problems. creating application executes few command lines. have commands
mysql -u root -p
which opening mysql console. here, how can input password? did:-
cd c:\\program files (x86)\\mysql\\mysql server 5.5\\bin && mysql -u root -p && root && create database testingdb; && quit && mysql –u root –p testingdb < mysql_dump.testingdb.sql
here, first input root password , sec mysql_dump.testingdb.sql
sql file located in mysql_dump
package. not working , thread opened though cmd console window close.
how java work in situation?
running command
processbuilder provides total api starting commands.
processbuilder processbuilder = new processbuilder("mysql", "-u", "root", "-p"); processbuilder.directory(new file("mydir")); pb.redirecterrorstream(true); process proc = pb.start();
pipe command you can write strings bufferedwriter wraps confusingly named "output" stream process object returned processbuilder.start()
outputstream stdin = proc.getoutputstream(); outputstreamwriter osr = new outputstreamwriter(stdin); buffererwriter bw = new buffererwriter(osr); bw.write("hello\n");
a word of warning you need careful when using process consume standard output/error streams otherwise process can lock. robust implementation should create 2 threads read stdout , stderr asynchronously , perhaps 3rd feed process.
the parent process uses these streams (#getinputstream(), geterrorstream()) feed input , output subprocess. because native platforms provide limited buffer size standard input , output streams, failure promptly write input stream or read output stream of subprocess may cause subprocess block, , deadlock.
this guide when runtime.exec() won't has info need.
java mysql cmd
No comments:
Post a Comment