org.springframework.jdbc.datasource.init

## Class ResourceDatabasePopulator

- java.lang.Object
- - org.springframework.jdbc.datasource.init.ResourceDatabasePopulator

- All Implemented Interfaces:
    DatabasePopulator

      public class ResourceDatabasePopulator
      extends Object
      implements DatabasePopulator

  Populates a database from SQL scripts defined in external resources.

  Call `addScript(Resource)` to add a SQL script location. Call `setSqlScriptEncoding(String)` to set the encoding for all added scripts.

  Since:
    3.0
  Author:
    Keith Donald, Dave Syer, Juergen Hoeller, Chris Beams, Oliver Gierke, Sam Brannen

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ResourceDatabasePopulator()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addScript(Resource script)` Add a script to execute to populate the database.
    `void` | `populate(Connection connection)` Populate the database using the JDBC connection provided.
    `void` | `setCommentPrefix(String commentPrefix)` Set the line prefix that identifies comments in the SQL script.
    `void` | `setContinueOnError(boolean continueOnError)` Flag to indicate that all failures in SQL should be logged but not cause a failure.
    `void` | `setIgnoreFailedDrops(boolean ignoreFailedDrops)` Flag to indicate that a failed SQL `DROP` statement can be ignored.
    `void` | `setScripts(Resource... scripts)` Set the scripts to execute to populate the database.
    `void` | `setSeparator(String separator)` Specify the statement separator, if a custom one.
    `void` | `setSqlScriptEncoding(String sqlScriptEncoding)` Specify the encoding for SQL scripts, if different from the platform encoding.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### ResourceDatabasePopulator

          public ResourceDatabasePopulator()

  - ### Method Detail

    - #### addScript

          public void addScript(Resource script)

      Add a script to execute to populate the database.
      Parameters:
        `script` - the path to a SQL script

    - #### setScripts

          public void setScripts(Resource... scripts)

      Set the scripts to execute to populate the database.
      Parameters:
        `scripts` - the scripts to execute

    - #### setSqlScriptEncoding

          public void setSqlScriptEncoding(String sqlScriptEncoding)

      Specify the encoding for SQL scripts, if different from the platform encoding. Note setting this property has no effect on added scripts that are already `encoded resources`.
      See Also:
        `addScript(Resource)`

    - #### setSeparator

          public void setSeparator(String separator)

      Specify the statement separator, if a custom one. Default is ";".

    - #### setCommentPrefix

          public void setCommentPrefix(String commentPrefix)

      Set the line prefix that identifies comments in the SQL script. Default is "--".

    - #### setContinueOnError

          public void setContinueOnError(boolean continueOnError)

      Flag to indicate that all failures in SQL should be logged but not cause a failure. Defaults to false.

    - #### setIgnoreFailedDrops

          public void setIgnoreFailedDrops(boolean ignoreFailedDrops)

      Flag to indicate that a failed SQL `DROP` statement can be ignored.

      This is useful for non-embedded databases whose SQL dialect does not support an `IF EXISTS` clause in a `DROP`. The default is false so that if the populator runs accidentally, it will fail fast when the script starts with a `DROP`.

    - #### populate

          public void populate(Connection connection)
                        throws SQLException

      Description copied from interface: `DatabasePopulator`
      Populate the database using the JDBC connection provided.
      Specified by:
        `populate` in interface `DatabasePopulator`
      Parameters:
        `connection` - the JDBC connection to use to populate the db; already configured and ready to use
      Throws:
        `SQLException` - if an unrecoverable data access exception occurs during database population