- private static void addFile(Statement stmt, File f) throws IOException, SQLException {
- String sql = readFile(f);
- String[] stmts = sql.split(";");
- for (String string : stmts) {
- if (!string.trim().equals("")) {
- stmt.addBatch(string);
- }
- }
- }
+ public static void run(String[] args, ImportType truncate) throws ClassNotFoundException, SQLException, IOException {
+ Class.forName(args[0]);
+ 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();
+ }
+ } catch (SQLException e) {
+ e.getNextException().printStackTrace();
+ throw e;
+ } finally {
+ conn.close();
+ }
+ }