/** * 批量执行sql文件 * * @param connection jdbc 链接对象 * @param sqlResourcePaths sql 文件路径 * @throws java.sql.SQLException */ private void executeScript(Connection connection, String... sqlResourcePaths) throws SQLException { for (String sqlResourcePath : sqlResourcePaths) { Resource resource = resourceLoader.getResource(sqlResourcePath); ScriptUtils.executeSqlScript(connection, resource); } connection.close(); }
/** * Execute the given SQL script using default settings for separator separators, comment * delimiters, and exception handling flags. * * <p>Statement separators and comments will be removed before executing individual statements * within the supplied script. * * <p><b>Do not use this method to execute DDL if you expect rollback.</b> * * @param connection the JDBC connection to use to execute the script; already configured and * ready to use * @param resource the resource (potentially associated with a specific encoding) to load the SQL * script from * @throws ScriptException if an error occurred while executing the SQL script * @see #executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, * String) * @see #DEFAULT_COMMENT_PREFIX * @see #DEFAULT_STATEMENT_SEPARATOR * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER */ public static void executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException { executeSqlScript( connection, resource, false, false, DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER); }
/** * {@inheritDoc} * * @see #execute(DataSource) */ @Override public void populate(Connection connection) throws ScriptException { Assert.notNull(connection, "Connection must not be null"); for (Resource script : getScripts()) { ScriptUtils.executeSqlScript( connection, encodeScript(script), this.continueOnError, this.ignoreFailedDrops, this.commentPrefix, this.separator, this.blockCommentStartDelimiter, this.blockCommentEndDelimiter); } }
/** Imports data into database by executing the sql datadump script */ private void importData() { context = new AnnotationConfigApplicationContext(SpringConf.class); CustomResourceLoader customResourceLoader = (CustomResourceLoader) context.getBean("customResourceLoader"); try { Resource query = customResourceLoader.getDataDumpResource(configFile.getDatadumpPath()); ScriptUtils.executeSqlScript(dataSource.getConnection(), query); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ScriptException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Execute the given SQL script using default settings for separator separators, comment * delimiters, and exception handling flags. * * <p>Statement separators and comments will be removed before executing individual statements * within the supplied script. * * <p><b>Do not use this method to execute DDL if you expect rollback.</b> * * @param connection the JDBC connection to use to execute the script; already configured and * ready to use * @param resource the resource to load the SQL script from; encoded with the current platform's * default encoding * @throws ScriptException if an error occurred while executing the SQL script * @see #executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, * String) * @see #DEFAULT_COMMENT_PREFIX * @see #DEFAULT_STATEMENT_SEPARATOR * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER */ public static void executeSqlScript(Connection connection, Resource resource) throws ScriptException { executeSqlScript(connection, new EncodedResource(resource)); }