001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: *
019: * Created: 26-Nov-2004 by jejking
020: * Version: $Revision: 1.2 $
021: * Last Updated: $Date: 2005/01/18 12:02:35 $
022: */
023: package org.openharmonise.install.server.utils;
024:
025: import java.io.BufferedReader;
026: import java.io.File;
027: import java.io.FileReader;
028: import java.sql.Connection;
029: import java.sql.DriverManager;
030: import java.sql.Statement;
031:
032: /**
033: * @author John King
034: *
035: */
036: public class BatchSQLRunner {
037:
038: public static void main(String[] args) throws Exception {
039: String sqlFile = fixPaths(args[0]);
040: String driver = args[1];
041: String user = args[2];
042: String password = args[3];
043: String url = args[4];
044: StringBuffer sqlBuffer = new StringBuffer(10000);
045:
046: System.err.println("reading in sql file");
047:
048: BufferedReader in = new BufferedReader(new FileReader(sqlFile));
049: String str;
050: while ((str = in.readLine()) != null) {
051: if (str.startsWith("--")) {
052: continue;
053: } else {
054: sqlBuffer.append(str);
055: sqlBuffer.append("\n");
056: }
057: }
058:
059: in.close();
060:
061: System.err.println(sqlBuffer.toString());
062:
063: // load JDBC Driver
064: Class.forName(driver);
065:
066: // prepare connection
067: Connection con = DriverManager.getConnection(url, user,
068: password);
069: System.err.println("got connection");
070:
071: Statement stmt = con.createStatement();
072: System.err.println("got statement");
073: stmt.executeUpdate(sqlBuffer.toString());
074:
075: stmt.close();
076: con.close();
077: }
078:
079: /**
080: * Swaps \ for / if we're on Windows.
081: *
082: * @param string
083: * @return
084: */
085: private static String fixPaths(String filePath) {
086: if (File.separator.equals("\\")) {
087:
088: StringBuffer sb = new StringBuffer();
089: String bits[] = filePath.split("/");
090: for (int i = 0; i < bits.length; i++) {
091: sb.append(bits[i]);
092: if (i != bits.length - 1) {
093: sb.append("\\");
094: }
095:
096: }
097:
098: filePath = sb.toString();
099: }
100: return filePath;
101: }
102:
103: }
|