JohnRaja
JohnRaja

Reputation: 2357

How to determine if mysqldump called from Runtime.exec() has failed

How to find the error if the database name not exist in mysql. Database name like demo. I provide this following example code

String dumpCommand = "C:/Program Files/MySQL/MySQL Server 5.0/bin/mysqldump -h"+hostName+user+demo(//database name);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(dumpCommand);                     
InputStream in = proc.getInputStream();              
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String line =null;

 while((line=br.readLine())!=null)
{
//.....
}

This code will run successfully even database name not contains in mysql

Upvotes: 0

Views: 725

Answers (2)

Rickard von Essen
Rickard von Essen

Reputation: 4278

After:

Process proc = rt.exec(dumpCommand);

Check the return value, with something like:

if (proc.waitFor() != 0) 
  return;

Upvotes: 1

Bozho
Bozho

Reputation: 597106

Here are two kinds of errors:

mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: NO) when trying to connect

and

mysqldump: Got error: 1049: Unknown database 'database1name' when selecting the database

Based on that you can do the following

String output = IOUtils.toString(inputStream)
int errIdx = output.indexOf("mysqldump: Got error: ");
if (errIdx != -1) {
   throw new RuntimeException("Failed to perform dump. The error message is: " 
        + output.substring(idx));
}

If you don't want to use IOUtils (from commons-io) you can use your while(..) approach and do the above checks there.

Upvotes: 0

Related Questions