KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > test > tools > SQLTools


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.test.tools;
19
20 import java.io.File JavaDoc;
21 import java.sql.Connection JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.io.BufferedReader JavaDoc;
27 import java.io.FileReader JavaDoc;
28 import java.io.StringReader JavaDoc;
29
30
31 /**
32  *
33  * @author Stefano Nichele @ funambol
34  * @version $Id: SQLTools.java,v 1.1 2005/05/16 17:32:57 nichele Exp $
35  */

36 public class SQLTools {
37
38     // ---------------------------------------------------------- Public Methods
39

40     /**
41      * Executes all sql queries contained in the given script file.
42      * <br/>Performs a simply parser of the file ignoring line that start with '--'
43      *
44      * @param conn the connection to use to execute the query
45      * @param file the script file
46      *
47      * @throws IOException
48      * @throws SQLException
49      */

50     public static void executeScript(Connection JavaDoc conn, File JavaDoc file) throws IOException JavaDoc, SQLException JavaDoc {
51
52         StringBuffer JavaDoc query = new StringBuffer JavaDoc();
53
54         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
55
56         String JavaDoc line = null;
57         StringReader JavaDoc lineReader = null;
58         int c = 0;
59
60         while ( (line = br.readLine()) != null ) {
61             if (line.startsWith("--")) {
62                 // ignore comment line
63
continue;
64             }
65
66             lineReader = new StringReader JavaDoc(line);
67
68             while ( (c = lineReader.read()) != -1) {
69
70                 if ( (char)c == ';' ) {
71                     executeQuery(conn, query.toString().trim());
72                     query.delete(0, query.length());
73                 } else {
74                     query.append( (char)c);
75                 }
76
77             }
78
79             lineReader.close();
80         }
81
82         br.close();
83
84     }
85
86     /**
87      * Executes a query on the given connection
88      *
89      * @param conn the connection to use to execute the query
90      * @param query the query to execute
91      *
92      * @throws SQLException
93      * @return int
94      */

95     public static int executeQuery(Connection JavaDoc conn, String JavaDoc query) throws SQLException JavaDoc {
96       Statement JavaDoc stmt = conn.createStatement();
97       int i = stmt.executeUpdate(query);
98       stmt.close();
99       return i;
100      }
101
102
103
104 }
Popular Tags