从Java运行mysql命令 [英] run a mysql command from java

查看:179
本文介绍了从Java运行mysql命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi,我有一个JAVA项目,我想在其中显示MySQL数据库. 我写这段代码:

hi I have a JAVA project in which I want to display the MySQL databases. i write this code:

try {
     String []command={"mysql -u root -pmanager","show databases"};
        Process p= Runtime.getRuntime().exec("mysql -u root -pmanager");
        Process p1= Runtime.getRuntime().exec("show databases");
        if (p.waitFor()==0){System.out.println("backup done...");}
        else{System.out.println("!!!");}
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

但是它给了我这个错误:

but it give me this error:

Cannot run program "show": CreateProcess error=2, The system cannot find the file specified

应该怎么办? 谢谢...

what it should be do? thanks...

推荐答案

更好地在exec方法中传递String对象,例如:

Better to pass String object in exec method like:

Runtime.getRuntime().exec(  new String [] {"mysql", "-u", "root", "-pmanager", "-e", "show databases"} )

这篇关于从Java运行mysql命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆