Java中各个表的mySQL转储

时间:2014-07-22 19:24:58

标签: java mysql mysqldump

我正在尝试连接到mySQL数据库并转储存储在数组中的一系列表。当我运行我的代码时,我得到以下错误。任何人都可以帮我找出它找不到的文件吗?它是找不到的表还是本地机器上的目标文件夹?另外,我如何知道/如何检查我是否正确连接到数据库?

Connection dbConn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    long companyId = 1002;
    String query = "select mobile_no, creation_dt from subscriber where company_id=?";
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String dbUrl = "jdbc:mysql://"+ dbHost + ":" + dbPort + "/" + dbName + "?autoReconnect=true";

        System.out.println(dbUrl);

        dbConn = DriverManager.getConnection(dbUrl, dbUserId, dbPassword);
        stmt = dbConn.prepareStatement(query);
        stmt.setLong(1, companyId);
        rs = stmt.executeQuery();

        java.util.Date date = new java.util.Date();


        System.out.println("STARTING DUMP");
        int i =0;
        dbTableName = dbTables.split(",");
        while(dbTableName[i] != null) {
            bPath = "\\" + dbHost + "." + dbTableName[i] + "." + new Timestamp(date.getTime()) + ".sql";
            System.out.println(bPath);
            Runtime.getRuntime().exec("mysqldump -u " + dbUserId + " -p " + dbPassword + " " + dbName + " " + dbTableName[i] + " --result-file=" + bPath);
            i++;
            //System.out.println(i);
        }

    } catch (Exception e) {
        System.err.println("Exception while reading info from database.");
        e.printStackTrace();
    } finally {
        try {
            if (rs != null) rs.close();
        } catch (Exception e) {
            System.err.println("Exception while closing result set!! - " + e.getMessage());
        }
        try {
            if (stmt != null) stmt.close();
        } catch (Exception e) {
            System.err.println("Exception while closing statment!! - " + e.getMessage());
        }

        try {
            if (dbConn != null) {
                dbConn.close();
            }
        } catch (Exception e) {
            System.err.println("Exception while closing db connection!! - " + e.getMessage());
        }
    }

错误:

Exception while reading info from database.
java.io.IOException: Cannot run program "mysqldump": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
    at java.lang.Runtime.exec(Runtime.java:620)
    at java.lang.Runtime.exec(Runtime.java:450)
    at java.lang.Runtime.exec(Runtime.java:347)
    at Backup.main(Backup.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
    at java.lang.ProcessImpl.start(ProcessImpl.java:137)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)

1 个答案:

答案 0 :(得分:0)

所以问题结果不是我的代码,而是服务器上的权限的一系列问题。花了一段时间才弄清楚。