diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptStatementFailedException.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptStatementFailedException.java index 0bb59c5efdf..a7544d76751 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptStatementFailedException.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptStatementFailedException.java @@ -29,6 +29,18 @@ import org.springframework.core.io.support.EncodedResource; @SuppressWarnings("serial") public class ScriptStatementFailedException extends ScriptException { + /** + * Build an error message based on the supplied parameters. + * @param statement the actual SQL statement that failed + * @param statementNumber the statement number in the SQL script (i.e., + * the nth statement present in the resource) + * @param encodedResource the resource from which the SQL statement was read + */ + public static String buildErrorMessage(String statement, int statementNumber, EncodedResource encodedResource) { + return String.format("Failed to execute SQL script statement #%s of %s: %s", + statementNumber, encodedResource, statement); + } + /** * Construct a new {@code ScriptStatementFailedException}. * @param statement the actual SQL statement that failed @@ -38,8 +50,7 @@ public class ScriptStatementFailedException extends ScriptException { * @param cause the underlying cause of the failure */ public ScriptStatementFailedException(String statement, int statementNumber, EncodedResource resource, Throwable cause) { - super("Failed to execute SQL script statement #" + statementNumber + " of resource " + resource + ": " - + statement, cause); + super(buildErrorMessage(statement, statementNumber, resource), cause); } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java index 00894080afe..a7afb12594b 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java @@ -464,8 +464,7 @@ public abstract class ScriptUtils { boolean dropStatement = StringUtils.startsWithIgnoreCase(statement.trim(), "drop"); if (continueOnError || (dropStatement && ignoreFailedDrops)) { if (logger.isDebugEnabled()) { - logger.debug("Failed to execute SQL script statement #" + stmtNumber + - " of resource " + resource + ": " + statement, ex); + logger.debug(ScriptStatementFailedException.buildErrorMessage(statement, stmtNumber, resource), ex); } } else {