首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何确定从Runtime.exec()调用的mysqldump是否失败

问如何确定从Runtime.exec()调用的mysqldump是否失败
EN

Stack Overflow用户
提问于 2010-02-19 07:25:16
回答 1查看 712关注 0票数 0

如果mysql中不存在数据库名,如何查找错误。数据库名类似于演示。我提供以下示例代码

代码语言:javascript
复制
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)
{
//.....
}

即使mysql中不包含数据库名,此代码也将成功运行。

EN

回答 1

Stack Overflow用户

发布于 2010-02-19 07:37:21

以下是两种错误:

mysqldump: Got错误: 1045:在尝试连接时拒绝用户'root'@'localhost‘(使用密码:否)的访问

和

mysqldump: Got错误: 1049:当选择数据库时未知数据库'database1name‘

基于此,您可以执行以下操作

代码语言:javascript
复制
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));
}

如果您不想使用IOUtils (来自公野),您可以使用while(..)方法并在那里进行上述检查。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2294683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档