0

can someone please correct to me this program, i try to connect to MySQL but the process cant stop, if i run this command in command prompt there is cursor waiting and i authenticate without it just pressing enter here is the code:

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         


     try {
        // TODO code application logic here
        Process x=Runtime.getRuntime().exec("cmd /C C:\\xampp\\mysql\\bin\\mysql -u root -p -e \"show slave status\\G\"| findstr \"Last_SQL\"");
        BufferedReader br = new BufferedReader(new InputStreamReader(x.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(x.getErrorStream()));
        // read the output from the command

        String s = null;
        String t=null;
        while ((s = br.readLine()) != null) {

            jTextArea1.append("\n"+s);
        }

   }catch(IOException ex){
         ex.printStackTrace();
   }

}                                        
maxx777
  • 1,320
  • 1
  • 20
  • 37
  • your problem is not quite clear – maxx777 Apr 16 '14 at 23:34
  • ok i want to execute the command that i specified to getRuntime().exec() on java and put the result in JTexArea but there is problem in command i dont know how can i solve it – user3533570 Apr 16 '14 at 23:37
  • try to elaborate the exact problem you are facing. is it hanging up the command line or you are getting some error? – maxx777 Apr 16 '14 at 23:39
  • first thank you for the help, concerning this example there is no error its correct but the process is waiting, normally in command prompt if we execute this command when we press enter there is another cursor that waits for entering the password, in my example i don't have password authentication with root.beg pardon – user3533570 Apr 16 '14 at 23:46
  • it seems you dont have a password set for root user. If it is so, try sending a null string or `""` as the password. – maxx777 Apr 16 '14 at 23:48
  • How can I do it? if i put "" just after -p there is an error – user3533570 Apr 16 '14 at 23:49
  • use `''` or use escape sequences `\"\"` – maxx777 Apr 16 '14 at 23:51
  • see [this](http://stackoverflow.com/questions/19006252/run-a-mysql-command-from-java) – maxx777 Apr 16 '14 at 23:55
  • Im so sorry but Runtime.getRuntime().exec(String [] {"mysql", "-u", "root", "-pmanager", "-e", "show databases"}); cant work in windows 7. there is problem – user3533570 Apr 17 '14 at 00:09
  • we have to specify the path where is the mysqld – user3533570 Apr 17 '14 at 00:13
  • i made a simple change to your code and it was running on my system. `Process x = Runtime.getRuntime().exec("C:\\xampp\\mysql\\bin\\mysql -u root -p -e \"show databases\\G\"| findstr \"Last_SQL\"");` – maxx777 Apr 17 '14 at 00:14
  • this command will show me Last_SQL_Error:(the error if it existes) please replace show databases\G by show slave status\G my command is mysql -u root -p -e "show slave status\G" findstr "Last_SQL_Error" thank you – user3533570 Apr 17 '14 at 10:06
  • You might want to consider using `ProcessBuilder` and redirecting error to standard so that you can read if any errors occur. – Mark Rotteveel Apr 17 '14 at 10:33
  • How can I do it please.best regards – user3533570 Apr 17 '14 at 10:37

0 Answers0