-
Notifications
You must be signed in to change notification settings - Fork 376
Expand file tree
/
Copy pathDatabasePopulatorUtils.java
More file actions
47 lines (40 loc) · 1.46 KB
/
DatabasePopulatorUtils.java
File metadata and controls
47 lines (40 loc) · 1.46 KB
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
46
47
package transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabasePopulatorUtils {
private static final Logger log = LoggerFactory.getLogger(DatabasePopulatorUtils.class);
public static void execute(final DataSource dataSource) {
Connection connection = null;
Statement statement = null;
try {
final var url = DatabasePopulatorUtils.class.getClassLoader().getResource("schema.sql");
final var file = new File(url.toURI());
final var sql = Files.readString(file.toPath());
connection = dataSource.getConnection();
statement = connection.createStatement();
statement.execute(sql);
} catch (NullPointerException | IOException | SQLException | URISyntaxException e) {
log.error(e.getMessage(), e.getCause());
} finally {
try {
if (statement != null) {
statement.close();
}
} catch (SQLException ignored) {}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException ignored) {}
}
}
private DatabasePopulatorUtils() {}
}