Top Description Fields Constructors Methods
org.springframework.jdbc.datasource.init

public Class ResourceDatabasePopulator

extends Object
implements DatabasePopulator
Class Inheritance
All Implemented Interfaces
org.springframework.jdbc.datasource.init.DatabasePopulator
Imports
java.sql.Connection, java.util.ArrayList, .Arrays, .List, javax.sql.DataSource, org.springframework.core.io.Resource, org.springframework.core.io.support.EncodedResource, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils

Populates, initializes, or cleans up a database using SQL scripts defined in external resources.
Authors
Keith Donald, Dave Syer, Juergen Hoeller, Chris Beams, Oliver Gierke, Sam Brannen, Chris Baldwin, Phillip Webb
Since
3.0
See Also
DatabasePopulatorUtils, ScriptUtils

Field Summary

Modifier and TypeField and Description
private String
private String
private String[]
private boolean
private boolean
pack-priv List<Resource>
private String
private String

Constructor Summary

AccessConstructor and Description
public
ResourceDatabasePopulator()

Construct a new ResourceDatabasePopulator with default settings.

public
ResourceDatabasePopulator(Resource...
the scripts to execute to initialize or clean up the database (never null)
scripts
)

Construct a new ResourceDatabasePopulator with default settings for the supplied scripts.

public
ResourceDatabasePopulator(boolean
flag to indicate that all failures in SQL should be logged but not cause a failure
continueOnError
,
boolean
flag to indicate that a failed SQL DROP statement can be ignored
ignoreFailedDrops
,
String
the encoding for the supplied SQL scripts (may be null or empty to indicate platform encoding)
sqlScriptEncoding
,
Resource...
the scripts to execute to initialize or clean up the database (never null)
scripts
)

Construct a new ResourceDatabasePopulator with the supplied values.

Method Summary

Modifier and TypeMethod and Description
public void
addScript(Resource
the path to an SQL script (never null)
script
)

Add a script to execute to initialize or clean up the database.

public void
addScripts(Resource...
the scripts to execute (never null)
scripts
)

Add multiple scripts to execute to initialize or clean up the database.

private void
public void
execute(DataSource
the DataSource to execute against (never null)
dataSource
)

Execute this ResourceDatabasePopulator against the given DataSource.

public void
populate(Connection
the JDBC connection to use; already configured and ready to use; never null
connection
)

Implements org.springframework.jdbc.datasource.init.DatabasePopulator.populate.

Populate, initialize, or clean up the database using the provided JDBC connection.
public void
setBlockCommentEndDelimiter(String
the end delimiter for block comments (never null or empty)
blockCommentEndDelimiter
)

Set the end delimiter that identifies block comments within the SQL scripts.

public void
setBlockCommentStartDelimiter(String
the start delimiter for block comments (never null or empty)
blockCommentStartDelimiter
)

Set the start delimiter that identifies block comments within the SQL scripts.

public void
setCommentPrefix(String
the prefix for single-line comments
commentPrefix
)

Set the prefix that identifies single-line comments within the SQL scripts.

public void
setCommentPrefixes(String...
the prefixes for single-line comments
commentPrefixes
)

Set the prefixes that identify single-line comments within the SQL scripts.

public void
setContinueOnError(boolean
true if script execution should continue on error
continueOnError
)

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

public void
setIgnoreFailedDrops(boolean
true if failed drop statements should be ignored
ignoreFailedDrops
)

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

public void
setScripts(Resource...
the scripts to execute (never null)
scripts
)

Set the scripts to execute to initialize or clean up the database, replacing any previously added scripts.

public void
setSeparator(String
the script statement separator
separator
)

Specify the statement separator, if a custom one.

public void
setSqlScriptEncoding(String
the encoding used in scripts (may be null or empty to indicate platform encoding)
sqlScriptEncoding
)

Specify the encoding for the configured SQL scripts, if different from the platform encoding.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

blockCommentEndDelimiterback to summary
private String blockCommentEndDelimiter
blockCommentStartDelimiterback to summary
private String blockCommentStartDelimiter
commentPrefixesback to summary
private String[] commentPrefixes
continueOnErrorback to summary
private boolean continueOnError
ignoreFailedDropsback to summary
private boolean ignoreFailedDrops
scriptsback to summary
pack-priv List<Resource> scripts
separatorback to summary
private String separator
sqlScriptEncodingback to summary
private String sqlScriptEncoding
Annotations
@Nullable

Constructor Detail

ResourceDatabasePopulatorback to summary
public ResourceDatabasePopulator()

Construct a new ResourceDatabasePopulator with default settings.

Since
4.0.3
ResourceDatabasePopulatorback to summary
public ResourceDatabasePopulator(Resource... scripts)

Construct a new ResourceDatabasePopulator with default settings for the supplied scripts.

Parameters
scripts:Resource[]

the scripts to execute to initialize or clean up the database (never null)

Since
4.0.3
ResourceDatabasePopulatorback to summary
public ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops, String sqlScriptEncoding, Resource... scripts)

Construct a new ResourceDatabasePopulator with the supplied values.

Parameters
continueOnError:boolean

flag to indicate that all failures in SQL should be logged but not cause a failure

ignoreFailedDrops:boolean

flag to indicate that a failed SQL DROP statement can be ignored

sqlScriptEncoding:String

the encoding for the supplied SQL scripts (may be null or empty to indicate platform encoding)

scripts:Resource[]

the scripts to execute to initialize or clean up the database (never null)

Since
4.0.3

Method Detail

addScriptback to summary
public void addScript(Resource script)

Add a script to execute to initialize or clean up the database.

Parameters
script:Resource

the path to an SQL script (never null)

addScriptsback to summary
public void addScripts(Resource... scripts)

Add multiple scripts to execute to initialize or clean up the database.

Parameters
scripts:Resource[]

the scripts to execute (never null)

assertContentsOfScriptArrayback to summary
private void assertContentsOfScriptArray(Resource... scripts)
executeback to summary
public void execute(DataSource dataSource) throws ScriptException

Execute this ResourceDatabasePopulator against the given DataSource.

Delegates to DatabasePopulatorUtils#execute.

Parameters
dataSource:DataSource

the DataSource to execute against (never null)

Exceptions
ScriptException:
if an error occurs
Since
4.1
See Also
populate(Connection)
populateback to summary
public void populate(Connection connection) throws ScriptException

Implements org.springframework.jdbc.datasource.init.DatabasePopulator.populate.

Doc from org.springframework.jdbc.datasource.init.DatabasePopulator.populate.

Populate, initialize, or clean up the database using the provided JDBC connection.

Warning

Concrete implementations should not close the provided Connection.

Concrete implementations may throw an SQLException if an error is encountered but are strongly encouraged to throw a specific ScriptException instead. For example, Spring's ResourceDatabasePopulator and DatabasePopulatorUtils wrap all SQLExceptions in ScriptExceptions.

Parameters
connection:Connection

the JDBC connection to use; already configured and ready to use; never null

Annotations
@Override
Exceptions
ScriptException:
in all other error cases
See Also
execute(DataSource)
setBlockCommentEndDelimiterback to summary
public void setBlockCommentEndDelimiter(String blockCommentEndDelimiter)

Set the end delimiter that identifies block comments within the SQL scripts.

Defaults to "*/".

Parameters
blockCommentEndDelimiter:String

the end delimiter for block comments (never null or empty)

Since
4.0.3
See Also
setBlockCommentStartDelimiter
setBlockCommentStartDelimiterback to summary
public void setBlockCommentStartDelimiter(String blockCommentStartDelimiter)

Set the start delimiter that identifies block comments within the SQL scripts.

Defaults to "/*".

Parameters
blockCommentStartDelimiter:String

the start delimiter for block comments (never null or empty)

Since
4.0.3
See Also
setBlockCommentEndDelimiter
setCommentPrefixback to summary
public void setCommentPrefix(String commentPrefix)

Set the prefix that identifies single-line comments within the SQL scripts.

Defaults to "--".

Parameters
commentPrefix:String

the prefix for single-line comments

See Also
setCommentPrefixes(String...)
setCommentPrefixesback to summary
public void setCommentPrefixes(String... commentPrefixes)

Set the prefixes that identify single-line comments within the SQL scripts.

Defaults to ["--"].

Parameters
commentPrefixes:String[]

the prefixes for single-line comments

Since
5.2
setContinueOnErrorback to summary
public void setContinueOnError(boolean continueOnError)

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

Defaults to false.

Parameters
continueOnError:boolean

true if script execution should continue on error

setIgnoreFailedDropsback to summary
public void setIgnoreFailedDrops(boolean ignoreFailedDrops)

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

This is useful for a non-embedded database whose SQL dialect does not support an IF EXISTS clause in a DROP statement.

The default is false so that if the populator runs accidentally, it will fail fast if a script starts with a DROP statement.

Parameters
ignoreFailedDrops:boolean

true if failed drop statements should be ignored

setScriptsback to summary
public void setScripts(Resource... scripts)

Set the scripts to execute to initialize or clean up the database, replacing any previously added scripts.

Parameters
scripts:Resource[]

the scripts to execute (never null)

setSeparatorback to summary
public void setSeparator(String separator)

Specify the statement separator, if a custom one.

Defaults to ";" if not specified and falls back to "\n" as a last resort; may be set to ScriptUtils#EOF_STATEMENT_SEPARATOR to signal that each script contains a single statement without a separator.

Parameters
separator:String

the script statement separator

setSqlScriptEncodingback to summary
public void setSqlScriptEncoding(String sqlScriptEncoding)

Specify the encoding for the configured SQL scripts, if different from the platform encoding.

Parameters
sqlScriptEncoding:String

the encoding used in scripts (may be null or empty to indicate platform encoding)

See Also
addScript(Resource)