KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > install > server > utils > BatchSQLRunner


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  *
19  * Created: 26-Nov-2004 by jejking
20  * Version: $Revision: 1.2 $
21  * Last Updated: $Date: 2005/01/18 12:02:35 $
22  */

23 package org.openharmonise.install.server.utils;
24
25 import java.io.BufferedReader JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileReader JavaDoc;
28 import java.sql.Connection JavaDoc;
29 import java.sql.DriverManager JavaDoc;
30 import java.sql.Statement JavaDoc;
31
32 /**
33  * @author John King
34  *
35  */

36 public class BatchSQLRunner {
37
38
39     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
40         String JavaDoc sqlFile = fixPaths(args[0]);
41         String JavaDoc driver = args[1];
42         String JavaDoc user = args[2];
43         String JavaDoc password = args[3];
44         String JavaDoc url = args[4];
45         StringBuffer JavaDoc sqlBuffer = new StringBuffer JavaDoc(10000);
46         
47         System.err.println("reading in sql file");
48         
49         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(sqlFile));
50         String JavaDoc str;
51         while ((str = in.readLine()) != null) {
52             if (str.startsWith("--")) {
53                 continue;
54             }
55             else {
56                 sqlBuffer.append(str);
57                 sqlBuffer.append("\n");
58             }
59         }
60         
61         in.close();
62         
63         System.err.println(sqlBuffer.toString());
64
65         // load JDBC Driver
66
Class.forName(driver);
67
68         // prepare connection
69
Connection JavaDoc con = DriverManager.getConnection(url, user, password);
70         System.err.println("got connection");
71         
72         Statement JavaDoc stmt = con.createStatement();
73         System.err.println("got statement");
74         stmt.executeUpdate(sqlBuffer.toString());
75         
76         stmt.close();
77         con.close();
78     }
79
80     /**
81      * Swaps \ for / if we're on Windows.
82      *
83      * @param string
84      * @return
85      */

86     private static String JavaDoc fixPaths(String JavaDoc filePath) {
87         if (File.separator.equals("\\")) {
88             
89             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
90             String JavaDoc bits[] = filePath.split("/");
91             for (int i = 0; i < bits.length; i++) {
92                 sb.append(bits[i]);
93                 if (i != bits.length - 1) {
94                     sb.append("\\");
95                 }
96                 
97             }
98             
99             filePath = sb.toString();
100         }
101         return filePath;
102     }
103
104 }
105
Popular Tags