JAVA实现数据库备份与还原[MYSQL为例]
数据库数据的备份与还原最好是使用数据库自带的客户端软件来实现,JAVA可以通过调用mysqldump、mysql客户端软件分别进行mysql数据库的备份还原,当然前提是JAVA服务器必须安装mysql客户端软件,但是也可以通过java telnet登陆数据库服务器实现备份,关于telnet的相关资料请Google. MYSQL支持的备份方式: 1.使用SELECT INTO ...OUTFILE,例如 SQL代码 1 SELECT * INTO OUTFILE "D:\\test.sql" fields terminatedby ',' lines terminatedby '' from category ; 生成的文件会存放在数据库服务器上面 SELECT INTO只备份了数据库表中的数据:如下为category备份后的文件内容 XML/HTML代码 2 1,ELECTRONICS,\N 3 2,TELEVISIONS,1 4 3,TUBE,2 5 4,LCD,2 6 5,PLASMA,2 7 6,PORTABLE ELECTRONICS,1 8 7,MP3 PLAYERS,6 9 8,FLASH,7 10 9,CD PLAYERS,6 11 10,2 WAY RADIOS,6 2.使用BACKUP TABLE,例如 SQL代码 12 BACKUPTABLE test_tableto 'D:\backup'; BACKUP只针对MyISAM表格,且在MYSQL 5.1的参考手册中“注释:本语句不理想”; BACKUP语句效果为拷贝数据库表中的.frm文件到数据库服务器目标目录 3.使用mysqldump程序或mysqlhotcopy脚本 MYSQL 5.1的参考手册中关于mysqldump的说明:“可用来转储数据库或搜集数据库进行备份或将数据转移到另一个SQL服务器(不一定是一个MySQL服务器)。转储包含创建表和/或装载表的SQL语句。“ JAVA可以运用Process类的exec(String str)方法调用系统命令,所以需要在服务器上安装mysqldump程序已完成备份,可以对远程的数据库服务器进行备份(配置hostname参数),备份语句具体参数参考MYSQL参考手册 MYSQL备份命令行: SQL代码 13 mysqldump -hhostname -uusername -ppassword databasename > ‘backupfile’ JAVA代码如下: Java代码 14 public final String BACKUP_COMMAND ="mysqldump"; 15 public final String ENCODING ="utf8"; 16 17 public boolean backup(String file) { 18 boolean isSuccess =true; 19 try { 20 Runtime rt = Runtime(); 21 String backupStr =BackupStr(); 22 logger.infoT(backupStr); 23 Process process = rt.exec(backupStr); 24 25 BufferedReader br =new BufferedReader(new InputStream(), ENCODING)); 26 27 String inStr =""; 28 StringBuffer sb =new StringBuffer(""); 29 while ((inStr = br.readLine()) !=null) { 30 sb.append(inStr).append(""); 31 } 32 String outStr = sb.toString(); 33 34 OutputStreamWriter writer =new OutputStreamWriter(new FileOutputStream(file), ENCODING); 35 writer.write(outStr); 36 writer.flush(); 37 38 br.close(); 39 writer.close(); 40 }catch (Exception e) { 41 e.printStackTrace(); 42 isSuccess =false; 43 } 44 return isSuccess; 45 } 46 47 private String getBackupStr() { 48 String backupStr = BACKUP_COMMAND +" -u" + db.getUserName() +" -p" + db.getPassword() +" -h" + db.getHost() +" --set-charset=" + db.getEncoding() +" " + db.getName(); 49 return backupStr; 50 } 51 MYSQL还原命令行: SQL代码 52 mysql -hhostname -uusername -ppassword databasename < ‘backupfile’ JAVA代码如下: Java代码 53 public final String REVERT_COMMAND ="mysql"; 54 55 public boolean revert(String file) { 56 try { 57 Runtime rt = Runtime(); 58 String revertStr =RevertStr(); 59 Process process = rt.exec(revertStr); 60 61 String inStr; 62 StringBuffer sb =new StringBuffer(""); 63 BufferedReader br =new BufferedReader(new InputStreamReader( 64 new FileInputStream(file), ENCODING)); 65 while ((inStr = br.readLine()) !=null) { 66 sb.append(inStr).append(""); 67 } 68 String outStr = sb.toString(); 69 70 OutputStreamWriter writer =new OutputStream(), ENCODING); 71 writer.write(outStr); 72 writer.flush(); 73 br.close(); 74 writer.close(); 75 }catch (Exception e) { 76 e.printStackTrace(); 77 return false; 78 } 79 return true; 80 } 81 82 private String getRevertStr() { 83 String backupStr = REVERT_COMMAND +" -u" + db.getUserName() +" -p" + db.getPassword() +" -h" + db.getHost() +" " + db.getName(); 84 return backupStr; 85 } mysqlhotcopy程序,MYSQL 5.1的参考手册的说明:"如果你在服务器上进行备份,并且表均为MyISAM表,它运行在Unix和NetWare中" 实现原理和mysqldump一样,这里暂不详细说明 附上mysqldump备份后的文件的内容:DROP TAB XML/HTML代码 86 CREATE TABLE `category` ( 87 `category_id` int(11) NOT NULL auto_increment, 88 `name` varchar(20) NOT NULL, 89 `parent` int(11) default NULL, 90 PRIMARY KEY (`category_id`) 91 )ENGINE=MyISAM DEFAULTCHARSET=latin1 ROW_FORMAT=REDUNDANT; 92 93 -- 94 -- Dumping data for table `category` 95 -- 96 97 98 /*!40000 ALTER TABLE `category` DISABLE KEYS */; 99 LOCK TABLES `category` WRITE; 100 INSERT INTO `category` VALUES (1,'ELECTRONICS',NULL),(2,'TELEVISIONS',1),(3,'TUBE',2),(4,'LCD',2),(5,'PLASMA',2),(6,'PORTABLE ELECTRONICS',1),(7,'MP3 PLAYERS',6),(8,'FLASH',7),(9,'CD PLAYERS',6),(10,'2 WAY RADIOS',6); 101 UNLOCK TABLES; 102 /*!40000 ALTER TABLE `category` ENABLE KEYS */; |
数据库的备份import java.io.IOException;
import javax.swing.JOptionPane;
public class JavaOutputOracle {
public static void main(String[] args) {
Runtime rt = Runtime();
Process processexp = null;// 创建一个本机进程
String exp = "exp scott/tiger@oracle file=d:/gengen.dmp";
try {
processexp = rt.exec(exp);
if (processexp.waitFor() != 0) {
JOptionPane.showMessageDialog(null, "数据库备份失败");
} else {
Process processexp = null;// 创建一个本机进程
String exp = "exp scott/tiger@oracle file=d:/gengen.dmp";
try {
processexp = rt.exec(exp);
if (processexp.waitFor() != 0) {
JOptionPane.showMessageDialog(null, "数据库备份失败");
} else {
JOptionPane.showMessageDialog(null, "数据库备份成功");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
数据的恢复:
import java.io.IOException;
import javax.swing.JOptionPane;
java库public class OracleInput{
public static void main(String[] args) {
public static void main(String[] args) {
Runtime rt = Runtime();
Process processexp = null;
String imp = "imp scott/tiger@oracle file=d:/gengen.dmp";
try {
processexp = rt.exec(imp);
if (processexp.waitFor() != 0) {
JOptionPane.showMessageDialog(null, "没有备份的数据库信息文件");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Process processexp = null;
String imp = "imp scott/tiger@oracle file=d:/gengen.dmp";
try {
processexp = rt.exec(imp);
if (processexp.waitFor() != 0) {
JOptionPane.showMessageDialog(null, "没有备份的数据库信息文件");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论