001: package org.apache.velocity.test.sql;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.commons.lang.StringUtils;
023:
024: import org.hsqldb.jdbcDriver;
025:
026: import java.io.FileReader;
027:
028: import java.sql.Connection;
029: import java.sql.DriverManager;
030: import java.sql.SQLException;
031: import java.sql.Statement;
032:
033: public class HsqlDB {
034: private Connection connection = null;
035:
036: public HsqlDB(String uri, String loadFile) throws Exception {
037: Class.forName(jdbcDriver.class.getName());
038:
039: this .connection = DriverManager.getConnection(uri, "sa", "");
040:
041: if (StringUtils.isNotEmpty(loadFile)) {
042: loadSqlFile(loadFile);
043: }
044: }
045:
046: public Connection getConnection() {
047: return connection;
048: }
049:
050: public void close() {
051:
052: try {
053: connection.close();
054: } catch (Exception e) {
055: System.out.println("While closing Connection"
056: + e.getMessage());
057: }
058: }
059:
060: private void loadSqlFile(String fileName) throws Exception {
061: Statement statement = null;
062:
063: try {
064: statement = connection.createStatement();
065:
066: String commands = getFileContents(fileName);
067:
068: for (int targetPos = commands.indexOf(';'); targetPos > -1; targetPos = commands
069: .indexOf(';')) {
070: String cmd = commands.substring(0, targetPos + 1);
071:
072: try {
073: statement.execute(cmd);
074: } catch (SQLException sqle) {
075: System.out.println("Statement: " + cmd + ": "
076: + sqle.getMessage());
077: }
078:
079: commands = commands.substring(targetPos + 2);
080: }
081: } finally {
082:
083: if (statement != null) {
084: statement.close();
085: }
086: }
087: }
088:
089: private String getFileContents(String fileName) throws Exception {
090: FileReader fr = null;
091:
092: try {
093: fr = new FileReader(fileName);
094:
095: char[] fileBuf = new char[1024];
096: StringBuffer sb = new StringBuffer(1000);
097: int res = -1;
098:
099: while ((res = fr.read(fileBuf, 0, 1024)) > -1) {
100: sb.append(fileBuf, 0, res);
101: }
102:
103: return sb.toString();
104: } finally {
105:
106: if (fr != null) {
107: fr.close();
108: }
109: }
110: }
111: }
|