1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| @Service
public class PersistentHelper {
@Autowired DossierDao dossierDao;
@Autowired TraitementDao traitementDao;
@Autowired ApplicationDao applicationDao;
@Autowired ApplicationContext context;
private JdbcTemplate jdbcTemplate;
@Autowired
public PersistentHelper(DataSource datasource){
jdbcTemplate = new JdbcTemplate(datasource,true);
}
public void executeSqlScript(String path)throws Exception{
Resource resource = context.getResource(path);
executeSqlScript(jdbcTemplate, new EncodedResource(resource,"UTF-8"));
}
public static void executeSqlScript(JdbcTemplate simpleJdbcTemplate,
EncodedResource resource) throws DataAccessException {
List<String> statements = new LinkedList<String>();
try {
LineNumberReader lnr = new LineNumberReader(resource.getReader());
String script = JdbcTestUtils.readScript(lnr);
char delimiter = ';';
if (!JdbcTestUtils.containsSqlScriptDelimiters(script, delimiter)) {
delimiter = '\n';
}
JdbcTestUtils.splitSqlScript(script, delimiter, statements);
for (String statement : statements) {
try {
simpleJdbcTemplate.update(statement);
}
catch (DataAccessException ex) {
throw ex;
}
}
}
catch (IOException ex) {
throw new DataAccessResourceFailureException("Impossible d'ouvrir le script depuis " + resource, ex);
}
} |
Partager