KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > test > HsqlDB


1 package org.apache.turbine.test;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.FileReader JavaDoc;
20
21 import java.sql.Connection JavaDoc;
22 import java.sql.DriverManager JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.Statement JavaDoc;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import org.hsqldb.jdbcDriver;
31
32 public class HsqlDB
33 {
34     private Connection JavaDoc connection = null;
35     private static Log log = LogFactory.getLog(HsqlDB.class);
36
37     public HsqlDB(String JavaDoc uri, String JavaDoc loadFile)
38             throws Exception JavaDoc
39     {
40         Class.forName(jdbcDriver.class.getName());
41
42         this.connection = DriverManager.getConnection(uri, "sa", "");
43
44             if (StringUtils.isNotEmpty(loadFile))
45             {
46                 loadSqlFile(loadFile);
47             }
48     }
49
50     public Connection JavaDoc getConnection()
51     {
52         return connection;
53     }
54
55     public void close()
56     {
57         try
58         {
59             connection.close();
60         }
61         catch (Exception JavaDoc e)
62         {
63         }
64     }
65
66     private void loadSqlFile(String JavaDoc fileName)
67             throws Exception JavaDoc
68     {
69         Statement JavaDoc statement = null;
70         try
71         {
72             statement = connection.createStatement();
73             String JavaDoc commands = getFileContents(fileName);
74
75             for (int targetPos = commands.indexOf(';'); targetPos > -1; targetPos = commands.indexOf(';'))
76             {
77                 String JavaDoc cmd = commands.substring(0, targetPos + 1);
78                 try
79                 {
80                     statement.execute(cmd);
81                 }
82                 catch (SQLException JavaDoc sqle)
83                 {
84                     log.warn("Statement: " + cmd + ": " + sqle.getMessage());
85                 }
86                 
87                 commands = commands.substring(targetPos + 2);
88             }
89         }
90         finally
91         {
92             if (statement != null)
93             {
94                 statement.close();
95             }
96         }
97     }
98
99     private String JavaDoc getFileContents(String JavaDoc fileName)
100             throws Exception JavaDoc
101     {
102         FileReader JavaDoc fr = new FileReader JavaDoc(fileName);
103
104         char fileBuf[] = new char[1024];
105         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(1000);
106         int res = -1;
107
108         while ((res = fr.read(fileBuf, 0, 1024)) > -1)
109         {
110             sb.append(fileBuf, 0, res);
111         }
112         fr.close();
113         return sb.toString();
114     }
115 }
116
117
Popular Tags