getOutputStream() & PrintWriter problems

hi i am trying to start mysql from java i have used getRuntime and process to start the server with the command C/mysql/bin/mysql -u username -p. this works becasuse the enter password prompt is passed back to the java program,using InputStream in = p.getInputStream();, my problem is passing the password back to mysql from java i have tried to use getOutputStream();with PrintWriter , but to no avail i would be greatful of any help on this matter
cheers

try{
Runtime r=null;
r=r.getRuntime();
Process p=r.exec("c:/mysql/bin/mysql -u mark_r -p");
p.waitFor();
for(int i=0;i<3;i++){

InputStream in = p.getInputStream();
BufferedReader d = new BufferedReader(new InputStreamReade(in));

String inputLine;
while((inputLine= d.readLine())!=null){
System.out.println(inputLine);}

OutputStream out = p.getOutputStream();
PrintWriter pp= new PrintWriter(out);
pp.println("spy10902");
pp.close();
}}
catch(Exception ex){System.out.print("Error");}}}
mrowlandsAsked:
Who is Participating?
 
Venci75Connect With a Mentor Commented:
try this:
try{
Runtime r=null;
r=r.getRuntime();
Process p=r.exec("c:/mysql/bin/mysql -u mark_r -p");
// remove waitFor() - check the documentation about this
//p.waitFor();
for(int i=0;i<3;i++){
// send the output first, because you application can block on Inputstream.read()
OutputStream out = p.getOutputStream();
PrintWriter pp= new PrintWriter(out);
pp.println("spy10902");
pp.close();

InputStream in = p.getInputStream();
BufferedReader d = new BufferedReader(new InputStreamReade(in));

String inputLine;
while((inputLine= d.readLine())!=null){
System.out.println(inputLine);}

}}
catch(Exception ex){System.out.print("Error");}}}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Try putting a delay after your statement:

pp.println ( "spy10902" ) ;
Thread.sleep ( 3000 ) ;

Since you're writing 'pp.close () ;' (and after that, terminating the whole program itself) just after the 'pp.println ( "spy10902" ) ;' statement, so you're not being able to give it enough time.... by the time your mysql is receiving it, the stream is already closed and the application has already terminated. Just check it out once.

Mayank.
0
 
77picoCommented:
pp.flush(); before the pp.close(); would be the better way
instead of the thread.sleep(3000);
0
 
CleanupPingCommented:
mrowlands:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to Venci75

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0
All Courses

From novice to tech pro — start learning today.