Java中单个表的mySQL转储
我试图连接到mySQL数据库并转储存储在数组中的一系列表。当我运行我的代码时,我得到下面的错误。有人能帮我找出它找不到的文件吗?它是找不到的表还是本地计算机上的目标文件夹?另外,我如何知道/如何检查我是否正确连接到数据库Java中单个表的mySQL转储,java,mysql,mysqldump,Java,Mysql,Mysqldump,我试图连接到mySQL数据库并转储存储在数组中的一系列表。当我运行我的代码时,我得到下面的错误。有人能帮我找出它找不到的文件吗?它是找不到的表还是本地计算机上的目标文件夹?另外,我如何知道/如何检查我是否正确连接到数据库 Connection dbConn = null; PreparedStatement stmt = null; ResultSet rs = null; long companyId = 1002; String query = "select
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)
所以问题不是我的代码,而是服务器上的一系列权限问题。花了一段时间才弄明白。错误消息说系统找不到指定的文件,所以我尝试指定mysqldump的完整路径。因此我将其更改为:mysqldump-u root-p nielsoft lbs subscriber-result file=C:/Users/Michael/Documents/SQL Backup/********/subscriber.SQL我仍然会遇到相同的错误。我指的是mysqldump的路径,不是文件的路径,尽管您可能也需要这样做。在装有MySQL 5.5的Win 7机器上,完整路径是C:\Program Files x86\MySQL\MySQL Workbench 5.2 CE\mysqldump.exe。我不确定您是否需要.exe扩展名,但这不会有什么坏处。