/**
   * Import data into database.
   *
   * @param connection database connection
   * @throws Exception throws various exceptions
   */
  public void importData(Connection connection) throws Exception {
    InputStream inStream;
    IDataSet dataSet;

    DatabaseConnection dbconnection = new DatabaseConnection(connection);

    // initialize the data set
    DatabaseConfig config = dbconnection.getConfig();
    config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());

    inStream = this.getClass().getResourceAsStream(getDataFile());

    try {

      // put check here to ensure, finally is always executed
      if (inStream == null) {
        throw new FileNotFoundException("Datafile: " + getDataFile() + " not found");
      }

      // if no modified data set is available the test data set is loaded
      dataSet = new FlatXmlDataSet(inStream);

      DatabaseOperation.CLEAN_INSERT.execute(dbconnection, dataSet);
    } finally {
      dbconnection.close();
      connection.close();
    }
  }
Example #2
0
 /**
  * Close the database connection (if any).
  *
  * @throws SQLException if failed to close connection.
  */
 private void disconnect() throws SQLException {
   if (connection != null) {
     connection.close();
     connection = null;
   }
 }