[英]MySQL restoration from Java stops suddenly without any message and hangs
我正在尝试从mysqldump生成的文件中还原MySQL数据库。 我使用包含恢复计划的每个查询的ArrayList进行操作,并使用Statement执行它们中的每个查询。
但有时,它会在过程的某个点停止(在不同的执行过程中可能会有所不同)。 它不显示任何错误消息; 它只是挂起(发生这种情况时,我需要重新启动Mysql服务)。
这是恢复的代码:
ArrayList<String> sql;
int res;
FileSQLCommandManager fichero = null;
try {
if (pass == null)
conectar(conn);
else
conectar(conn, pass);
Statement st = null;
st = conn.createStatement();
st.executeUpdate("SET FOREIGN_KEY_CHECKS=0");
PreparedStatement stConstraints = null;
String cadenaSQL = null;
String cadenaSQLConstraints = null;
String cadenaConstraints;
ResultSet rs;
boolean ejecutar = false;
fichero = new FileSQLCommandManager(fic);
fichero.open();
sql = fichero.read();
cadenaSQL = "";
for (int i = 0; i < sql.size(); i++) {
cadenaSQL = sql.get(i);
ejecutar = true;
if (ejecutar) {
st = null;
st = conn.createStatement();
res = st.executeUpdate(cadenaSQL);
if (res == Statement.EXECUTE_FAILED) {
System.out.println("HA FALLADO LA CONSULTA " + cadenaSQL);
}
}
}
st.executeUpdate("SET FOREIGN_KEY_CHECKS=1");
st.close();
fichero.close();
commit();
desconectar();
fichero = null;
return true;
} catch (Exception ex) {
ex.printStackTrace();
rollback();
desconectar();
return false;
}
}
FileSQLCommandManager是一个填充ArrayList的类。 这样工作,ArrayList内容就可以了。 它会在任何查询的executeUpdate上停止(并非总是如此,有时它在使用相同的SQL文件时不会出现问题)。
首先,我禁用外键检查,因为它可以删除带有引用的表(重新创建表的顺序由SQL转储设置)。
有什么提示吗?
谢谢; 我对此很生气:(
当一个简单的mysql < db_backup.dump
将为您还原整个内容时,为什么要经历所有这些工作呢?
这真的可以恢复
String comando = "C:\\MySQL\\bin\\mysql.exe --host=localhost --port=3306 --user=root --password=123 < D:\\back.sql";
File f = new File("restore.bat");
FileOutputStream fos = new FileOutputStream(f);
fos.write(comando.getBytes());
fos.close();
Process run = Runtime.getRuntime().exec("cmd /C start restore.bat ");
和备份
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.forLanguageTag("ru"));
java.util.Date currentDate = new java.util.Date();
Process p = null;
try {
Runtime runtime = Runtime.getRuntime();
p = runtime.exec("C:\\MySQL\\bin\\mysqldump.exe --default-character-set=utf8 -uroot -p123 -c -B shch2 -r " + "D:/" + dateFormat.format(currentDate) + "_backup" + ".sql");
//使用您的dbpass和dbname更改dbpass和dbname int processComplete = p.waitFor();
if (processComplete == 0) {
System.out.println("Backup created successfully!");
} else {
System.out.println("Could not create the backup");
}
} catch (Exception e) {
e.printStackTrace();
}
但是您需要传达mysql.exe和mysqldump.exe的确切路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.