dbunit 用来做数据库数据的导入导出

比较有用的文档地址:

http://www.dbunit.org/faq.html#streaming

http://www.dbunit.org/properties.html#qualifiedtablenames

http://www.dbunit.org/components.html#streamingdataset

工具类

java 代码
  1. package dbunit;   
  2.   
  3. import java.io.FileNotFoundException;   
  4. import java.io.FileOutputStream;   
  5. import java.io.IOException;   
  6. import java.sql.Connection;   
  7. import java.sql.DriverManager;   
  8. import java.sql.SQLException;   
  9. import java.util.HashSet;   
  10. import java.util.Iterator;   
  11. import java.util.Map;   
  12. import java.util.Set;   
  13.   
  14. import org.dbunit.DatabaseUnitException;   
  15. import org.dbunit.database.DatabaseConfig;   
  16. import org.dbunit.database.DatabaseConnection;   
  17. import org.dbunit.database.ForwardOnlyResultSetTableFactory;   
  18. import org.dbunit.database.IDatabaseConnection;   
  19. import org.dbunit.database.QueryDataSet;   
  20. import org.dbunit.dataset.CachedDataSet;   
  21. import org.dbunit.dataset.DataSetException;   
  22. import org.dbunit.dataset.IDataSet;   
  23. import org.dbunit.dataset.stream.IDataSetProducer;   
  24. import org.dbunit.dataset.stream.StreamingDataSet;   
  25. import org.dbunit.dataset.xml.FlatXmlDataSet;   
  26. import org.dbunit.dataset.xml.FlatXmlProducer;   
  27. import org.dbunit.dataset.xml.XmlDataSet;   
  28. import org.dbunit.dataset.xml.XmlProducer;   
  29. import org.dbunit.operation.DatabaseOperation;   
  30. import org.xml.sax.InputSource;   
  31.   
  32. public abstract class DBXMLHelper {   
  33.   
  34.     // partial database export   
  35.     public static void exportPartial(IDatabaseConnection connection,   
  36.             String fileName, Set tableNames, Map sqls, boolean streamed,   
  37.             boolean flat) throws SQLException, DataSetException,   
  38.             FileNotFoundException, IOException {   
  39.         connection.getConfig().setFeature(   
  40.                 DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);   
  41.         if (streamed)   
  42.             connection.getConfig().setProperty(   
  43.                     DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY,   
  44.                     new ForwardOnlyResultSetTableFactory());   
  45.   
  46.         QueryDataSet partialDataSet = new QueryDataSet(connection);   
  47.         // all data   
  48.         if (null != tableNames)   
  49.             for (Iterator it = tableNames.iterator(); it.hasNext();) {   
  50.                 partialDataSet.addTable((String) it.next());   
  51.             }   
  52.         // sql query   
  53.         if (null != sqls)   
  54.             for (Iterator it = sqls.keySet().iterator(); it.hasNext();) {   
  55.                 String key = (String) it.next();   
  56.                 partialDataSet.addTable(key, (String) sqls.get(key));   
  57.             }   
  58.   
  59.         if (flat)   
  60.             FlatXmlDataSet   
  61.                     .write(partialDataSet, new FileOutputStream(fileName));   
  62.         else  
  63.             XmlDataSet.write(partialDataSet, new FileOutputStream(fileName));   
  64.     }   
  65.   
  66.     // full database export   
  67.     public static void exportDatabase(IDatabaseConnection connection,   
  68.             String fileName, boolean streamed, boolean flat)   
  69.             throws SQLException, DatabaseUnitException, FileNotFoundException,   
  70.             IOException {   
  71.         if (streamed)   
  72.             connection.getConfig().setProperty(   
  73.                     DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY,   
  74.                     new ForwardOnlyResultSetTableFactory());   
  75.   
  76.         IDataSet fullDataSet = connection.createDataSet();   
  77.         if (flat)   
  78.             FlatXmlDataSet.write(fullDataSet, new FileOutputStream(fileName));   
  79.         else  
  80.             XmlDataSet.write(fullDataSet, new FileOutputStream(fileName));   
  81.     }   
  82.   
  83.     public static void refreshData(IDatabaseConnection connection,   
  84.             String input, boolean streamed, boolean flat)   
  85.             throws DatabaseUnitException, SQLException {   
  86.         // 把Documenth转换为DBUnit的DataSet并解决中文编码问题   
  87.         // is.setEncoding("GBK");   
  88.         IDataSetProducer producer;   
  89.         IDataSet dataSet;   
  90.         if (flat)   
  91.             producer = new FlatXmlProducer(new InputSource(input));   
  92.         else  
  93.             producer = new XmlProducer(new InputSource(input));   
  94.         if (streamed)   
  95.             dataSet = new StreamingDataSet(producer);   
  96.         else  
  97.             dataSet = new CachedDataSet(producer);   
  98.   
  99.         // 执行插入操作   
  100.         DatabaseOperation.REFRESH.execute(connection, dataSet);   
  101.     }   
  102.   
  103.     public static void cleanInsertData(IDatabaseConnection connection,   
  104.             String input, boolean flat) throws DatabaseUnitException,   
  105.             SQLException {   
  106.         IDataSetProducer producer;   
  107.         IDataSet dataSet;   
  108.         if (flat)   
  109.             producer = new FlatXmlProducer(new InputSource(input));   
  110.         else  
  111.             producer = new XmlProducer(new InputSource(input));   
  112.   
  113.         dataSet = new CachedDataSet(producer);   
  114.   
  115.         // 执行插入操作   
  116.         DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);   
  117.     }   
  118.   
  119. }   

你可能感兴趣的:(html,sql,xml,idea)