日期:2014-05-16  浏览次数:20433 次

使用dbunit备份还原数据库

<!-- pom.xml 引入dbunit -->
<dependency>
<groupId>dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.1</version>
</dependency>


JAVA代码:

package com.sinba.common.config.backup.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;

import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.dbunit.ext.mysql.MySqlDataTypeFactory;
import org.dbunit.ext.mssql.MsSqlDataTypeFactory;

?


public class DBXMLHelper{

?/**?
?? * 获取连接?
?? * @return?
?? * @throws DatabaseUnitException?
? */?
? private IDatabaseConnection getConnection(){
??
?? Properties hibernateProperties=new Properties();
?? InputStream in = DBXMLHelper.class.getResourceAsStream("/hibernate.properties");
??
?? IDatabaseConnection connection=null;
??
?? try {
???
???hibernateProperties.load(in);
???//处理连接字符串、密码
???String driverClassName=hibernateProperties.getProperty("dataSource.driverClassName");
???String username=hibernateProperties.getProperty("dataSource.username");
???String password=hibernateProperties.getProperty("dataSource.password");
???String serverName=hibernateProperties.getProperty("dataSource.serverName");
???String databaseName=hibernateProperties.getProperty("dataSource.databaseName");
???String url=hibernateProperties.getProperty("dataSource.url")
???.replace("${dataSource.serverName}",serverName)
???.replace("${dataSource.databaseName}",databaseName);
???
???Class.forName(driverClassName);
???Connection conn=DriverManager.getConnection(url,username,password);
???connection = new DatabaseConnection(conn,databaseName);
???
???//忽略约束
???connection.getConnection().prepareStatement("set @@session.foreign_key_checks = 0").execute();
???
???if("com.mysql.jdbc.Driver".equals(driverClassName)){//MySQL
????connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());?
???}else{//SQLServer
????connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MsSqlDataTypeFactory());?
???}
?
??} catch (Exception e1) {
???e1.printStackTrace();
??}
??
??return connection;
? }
?
??? /**
???? * 导出数据库.
???? * @param connection
???? * @param fileName
???? * @param streamed
???? * @param flat
???? * @throws SQLException
???? * @throws DatabaseUnitException
???? * @throws FileNotFoundException
???? * @throws IOException
???? */
??? public void exportDatabase(String fileName)
??? ?throws SQLException, DatabaseUnitException, FileNotFoundException,IOException {??
??? ?
??????? IDataSet dataset =getConnection().createDataSet();
??????? FlatXmlDataSet.write(dataset, new FileOutputStream(fileName));
??? }??
?
??? /**
???? * 直接还原数据库(不清除数据).
???? * @param connection
???? * @param input
???? * @param streamed
???? * @param flat
???? * @throws DatabaseUnitException
???? * @throws SQLException
???? */
??? public void refreshData(String input)
??? ?throws DatabaseUnitException, SQLException,FileNotFoundException,IOException {??
??? ?
??????? IDataSet dataSet = new FlatXmlDataSet(new File(input), true);
??????? DatabaseOperation.CLEAN_INSERT.execute(getConnection(), dataSet);
??? }

}