001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.sql;
018:
019: import java.sql.Connection;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022: import java.sql.Statement;
023:
024: import org.ofbiz.base.util.Debug;
025: import org.ofbiz.entity.GenericDelegator;
026: import org.ofbiz.entity.GenericEntityException;
027: import org.ofbiz.entity.jdbc.ConnectionFactory;
028:
029: /**
030: * DOCUMENT ME!
031: *
032: */
033: public class SQLUtil {
034: public static final String module = SQLUtil.class.getName();
035: String helperName = null;
036:
037: public SQLUtil() {
038: }
039:
040: /**
041: * DOCUMENT ME!
042: *
043: * @param delegator
044: *
045: * @return
046: */
047: public Connection getConnection(GenericDelegator delegator) {
048: Connection connection = null;
049:
050: if (helperName == null) {
051: helperName = delegator.getEntityHelperName("UiScreen");
052: }
053:
054: try {
055: connection = ConnectionFactory.getConnection(helperName);
056: } catch (SQLException sqle) {
057: Debug
058: .logError(
059: "[SQLUtil.getConnection]: Unable to esablish a connection with the database... Error was:",
060: module);
061: Debug.logError(sqle.getMessage(), module);
062: } catch (GenericEntityException gee) {
063: Debug
064: .logError(
065: "[SQLUtil.getConnection]: Unable to esablish a connection with the database... Error was:",
066: module);
067: Debug.logError(gee.getMessage(), module);
068: }
069:
070: return connection;
071: }
072:
073: /**
074: * DOCUMENT ME!
075: *
076: * @param delegator
077: * @param sqlStmt
078: *
079: * @return
080: */
081: public ResultSet executeSQL(GenericDelegator delegator,
082: String sqlStmt) {
083: Statement stmt = null;
084: ResultSet rs = null;
085: Connection conn = null;
086:
087: try {
088: conn = getConnection(delegator);
089: stmt = conn.createStatement();
090: rs = stmt.executeQuery(sqlStmt);
091:
092: return rs;
093: } catch (SQLException sqle) {
094: Debug.logError(
095: "[SQLUtil.executeSQL]: SQL Exception while executing the following:\n"
096: + sqlStmt + "\nError was:", module);
097: Debug.logError(sqle.getMessage(), module);
098:
099: return null;
100: } finally {
101: try {
102: if (stmt != null) {
103: stmt.close();
104: }
105: } catch (SQLException sqle) {
106: }
107:
108: try {
109: if (conn != null) {
110: conn.close();
111: }
112: } catch (SQLException sqle) {
113: }
114: }
115: }
116: }
|