001: /**
002: * Copyright (C) 2006 NetMind Consulting Bt.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 3 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package hu.netmind.persistence;
018:
019: import javax.sql.DataSource;
020: import java.sql.Connection;
021: import java.sql.DriverManager;
022: import java.sql.SQLException;
023: import java.io.PrintWriter;
024:
025: /**
026: * This is a data source for backwards compatibility. Older JDBC implementations
027: * only supported driver based connection establishing, this is a wrapper
028: * to make data source out of a driver.
029: * @author Brautigam Robert
030: * @version Revision: $Revision$
031: */
032: public class DriverDataSource implements DataSource {
033: private String url;
034:
035: public DriverDataSource(String driverClass, String url) {
036: try {
037: // Register driver
038: Class.forName(driverClass);
039: // Remember url
040: this .url = url;
041: } catch (Exception e) {
042: throw new StoreException("could not allocate driver", e);
043: }
044: }
045:
046: /**
047: * Get the connection using the url specified in constructor.
048: * @return A connection from the DriverManager.
049: */
050: public Connection getConnection() throws SQLException {
051: return DriverManager.getConnection(url);
052: }
053:
054: /**
055: * Get the connection using the url specified in constructor.
056: * @param params Additional parameters.
057: * @return A connection from the DriverManager.
058: */
059: public Connection getConnection(String params) throws SQLException {
060: return DriverManager.getConnection(url + ";" + params);
061: }
062:
063: /**
064: * Get the connection using the url specified in constructor, with
065: * username and password given.
066: * @param username The username.
067: * @param password The password.
068: * @return A connection from the DriverManager.
069: */
070: public Connection getConnection(String username, String password)
071: throws SQLException {
072: return DriverManager.getConnection(url, username, password);
073: }
074:
075: /**
076: * Get the login timeout.
077: */
078: public int getLoginTimeout() {
079: return DriverManager.getLoginTimeout();
080: }
081:
082: /**
083: * Get PrintWriter.
084: */
085: public PrintWriter getLogWriter() {
086: return DriverManager.getLogWriter();
087: }
088:
089: /**
090: * Set the login timeout.
091: */
092: public void setLoginTimeout(int timeout) {
093: DriverManager.setLoginTimeout(timeout);
094: }
095:
096: /**
097: * Set PrintWriter.
098: */
099: public void setLogWriter(PrintWriter writer) {
100: DriverManager.setLogWriter(writer);
101: }
102:
103: public boolean isWrapperFor(Class c) {
104: return false;
105: }
106:
107: public Object unwrap(Class c) throws SQLException {
108: throw new SQLException(
109: "DriverDataSource is not a wrapper for a specific object.");
110: }
111: }
|