我的代码是这样的:
public static void load() {
String root = "root";
String pass = "root";
String filepath = "E:\\MySQl\\yehuoneng.sql";
String stmt1 = "cmd /c"+" mysqladmin -u " + root + " -p" + pass+ " create yy";
String stmt2 = " cmd /c mysql -u " + root + " -p " + pass + " tt < "+ filepath;
try {
Runtime.getRuntime().exec(stmt1);
Runtime.getRuntime().exec(stmt2);
System.out.println("数据已从 " + filepath + " 导入到数据库中");
} catch (IOException e) {
e.printStackTrace();
}
}
然后中main方法里调用load方法,没有报错,mysql中没有新建的数据库,并且我自己建了数据库yy然后进行调用该方法也没有任何数据,请问高手,这是怎么回事?哪里出了问题?我在网上也查了很久,也没查出原因

解决方案 »

  1.   

    String stmt2 = " cmd /c mysql -u " + root + " -p " + pass + " tt < "+ filepath;debug中看一下stmt2字符串的实际内容是什么,至少你的-p 后面不应该有空格。
      

  2.   

    stmt2中的实际内容是cmd /c mysql -u root -proot tt < E:\MySQl\yehuoneng.sql,应该没有错把
      

  3.   

    直接在你的命令行中试一下。cmd /c mysql -u root -proot tt < E:\MySQl\yehuoneng.sql估计结果不对。最好把mysql.exe 全路径写出。 比如 "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql"
      

  4.   

    Runtime.getRuntime().exec(...),应该想办法得到它的输出流啊,光捕获异常是没用的。