Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

getOutputStream() & PrintWriter problems

Posted on 2003-03-13
6
Medium Priority
?
386 Views
Last Modified: 2008-03-17
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");}}}
0
Comment
Question by:mrowlands
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Accepted Solution

by:
Venci75 earned 200 total points
ID: 8126920
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8126995
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
 

Expert Comment

by:77pico
ID: 8139698
pp.flush(); before the pp.close(); would be the better way
instead of the thread.sleep(3000);
0
 

Expert Comment

by:CleanupPing
ID: 9058721
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
 
LVL 35

Expert Comment

by:girionis
ID: 9762606
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

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question