我目前正在使用Mssql2000。如何使用java应用程序备份此数据库?有可能吗?
发布于 2013-11-21 12:10:25
只需使用jdbc运行备份数据库脚本,例如..。
BACKUP DATABASE 'databaseOfNames' TO DISK = 'c:\myFile' 所以你会做一些类似..。
try (
// Step 1: Allocate a database "Connection" object
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:8888/ebookshop", "myuser", "password"); // MySQL
// Step 2: Allocate a "Statement" object in the Connection
Statement stmt = conn.createStatement();
) {
String strSelect = "BACKUP DATABASE 'databaseOfNames' TO DISK = 'c:\myFile' ";
ResultSet rset = stmt.execute(strSelect);
}应该注意的是,这使用了新的JDK7试用资源代码,因此它将自动关闭资源。
发布于 2013-11-21 13:06:21
只有在本地访问DB时才使用下面的代码。
public boolean backupDB(String dbName, String path) {
String executeCmd = "BACKUP DATABASE '"+ dbName +"' TO DISK = '"+ path +"' ";
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println("Backup created successfully");
return true;
} else {
System.out.println("Could not create the backup");
}
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}有关SQL备份的更多信息,请参阅此URL。
https://stackoverflow.com/questions/20120525
复制相似问题