- Connection conn = DriverManager.getConnection(args[1], args[2], args[3]);
- conn.setAutoCommit(false);
- Statement stmt = conn.createStatement();
- addFile(stmt, new File("doc/tableStructure.sql"), truncate);
- File localData = new File("doc/sampleData.sql");
- if (localData.exists()) {
- addFile(stmt, localData, ImportType.PRODUCTION);
- }
- stmt.executeBatch();
- conn.commit();
- stmt.close();
- }
-
- private static void addFile(Statement stmt, File f, ImportType type) throws IOException, SQLException {
- String sql = readFile(f);
- sql = sql.replaceAll("--[^\n]+\n", "\n");
- String[] stmts = sql.split(";");
- Pattern p = Pattern.compile("\\s*DROP TABLE IF EXISTS `([^`]+)`");
- for (String string : stmts) {
- Matcher m = p.matcher(string);
- string = string.trim();
- if (string.equals("")) {
- continue;
- }
- if (m.matches() && type == ImportType.TRUNCATE) {
- String sql2 = "TRUNCATE `" + m.group(1) + "`";
- stmt.addBatch(sql2);
- continue;
- }
- if (type == ImportType.PRODUCTION || string.startsWith("INSERT")) {
- stmt.addBatch(string);
- } else if (type == ImportType.TEST) {
- stmt.addBatch(string.replace("ENGINE=InnoDB", "ENGINE=Memory"));
+ final Connection conn = DriverManager.getConnection(args[1], args[2], args[3]);
+ try {
+ conn.setAutoCommit(false);
+ Statement stmt = conn.createStatement();
+ try {
+ try (InputStream structure = DatabaseConnection.class.getResourceAsStream("tableStructure.sql")) {
+ SQLFileManager.addFile(stmt, structure, truncate);
+ }
+ File localData = new File("doc/sampleData.sql");
+ if (localData.exists()) {
+ try (FileInputStream f = new FileInputStream(localData)) {
+ SQLFileManager.addFile(stmt, f, ImportType.PRODUCTION);
+ }
+ }
+ stmt.executeBatch();
+ conn.commit();
+ } finally {
+ stmt.close();