我将使用java进行数据库备份,code.This代码执行得很好,但我得到的是int processComplete =runtimeProcess.waitFor();这个方法调用将整数作为1返回。因此,最后我得到消息,因为无法将备份创建为sop。
public static void main(String[] args) {
String path = "D:/databasebackup/databasbac.sql";
String username = "root";
String password = "";
String dbname = "rac";
String executeCmd = "<Path to MySQL>/bin/mysqldump -u " + username + " -p" + password + " --add-drop-database -B " + dbname + " -r " + path;
Process runtimeProcess;
try {
// System.out.println(executeCmd);//this out put works in mysql shell
runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });
System.out.println(executeCmd);
// runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
System.out.println("processComplete"+processComplete);
if (processComplete == 0) {
System.out.println("Backup created successfully");
} else {
System.out.println("Could not create the backup");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}发布于 2016-05-19 15:32:33
在通过JDBC从MYSQL备份数据库时,我也遇到了类似的问题。所以我运行了下面的代码。
String path = C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump
String command= "cmd.exe /c "
+ "\"\""+path+"\" "
+ " --user="+UserInputs.getDbUserName()
+ " --password="+UserInputs.getDbPassword()
+ " --host="+UserInputs.getDbConnectionIP()
+ " --protocol=tcp "
+ " --port="+UserInputs.getDbConnectionPort()
+ " --default-character-set=utf8 "
+ " --single-transaction=TRUE "
+ " --routines "
+ " --events "
+ "\""+UserInputs.getDbName()
+"\" "
+ ">"
+ " \""
+ "D:\\MY DATA\\DB_Backup.sql"
+ "\""
+ " \"";
Runtime runtime = Runtime.getRuntime(command);如果密码为空,请删除--密码行。这将创建数据库备份。
如果您在LINUX上运行此替换
cmd.exe /c通过
/bin/sh -c谢谢!
发布于 2015-11-10 16:29:37
尝试使用java备份数据库的代码。对我来说效果很好。
try {
String filename = null;
FileChooser.setVisible(true);
int result = FileChooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
filename = FileChooser.getSelectedFile().toString().concat(".sql");
File file = new File(filename);
if (file.exists()) {
Object option[] = {"Sim", "Nao"};
int opcao = JOptionPane.showOptionDialog(null, "aaa", "bbbb", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, option, option[0]);
if (opcao == JOptionPane.YES_OPTION) {
Runtime backup = Runtime.getRuntime();
backup.exec("C:\\wamp\\bin\\mysql\\mysql5.6.17\\bin\\mysqldump.exe -v -v -v --host=localhost --user=root --port=3306 --protocol=tcp --force --allow-keywords --compress --add-drop-table --result-file=" + filename + " --databases GIVE YOUR DATABSE NAME");
JOptionPane.showMessageDialog(null, "Backup succesfully");
} else {
FileChooserActionPerformed(evt);
}
} else {
Runtime backup = Runtime.getRuntime();
backup.exec("C:\\wamp\\bin\\mysql\\mysql5.6.17\\bin\\mysqldump.exe -v -v -v --host=localhost --user=root --port=3306 --protocol=tcp --force --allow-keywords --compress --add-drop-table --result-file=" + filename + " --databases GIVE YOUR DATABASE NAME");
JOptionPane.showMessageDialog(null, "Backup succesfully");
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e, "Error.!", 2);
}这是我写的DbOperation类。
import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DbOperation {
String url = "jdbc:mysql://localhost:3306/give your database name";
String username = "your username";
String password = "your password";
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
public Connection backupDB(){
try{
con=DriverManager.getConnection(url, username, password);
}catch(SQLException e){
System.out.println(e.getMessage());
}
return con;
}
}发布于 2014-02-14 09:11:26
https://stackoverflow.com/questions/21774894
复制相似问题