001// Copyright 2004, 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.contrib.table.model.sql;
016
017import java.sql.Connection;
018import java.sql.DriverManager;
019import java.sql.SQLException;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023
024/**
025 * @author mindbridge
026 */
027public class SimpleSqlConnectionSource implements ISqlConnectionSource
028{
029    private static final Log LOG = LogFactory.getLog(SimpleSqlConnectionSource.class);
030
031    private String m_strUrl;
032
033    private String m_strUser;
034
035    private String m_strPwd;
036
037    public SimpleSqlConnectionSource(String strUrl)
038    {
039        this(strUrl, null, null);
040    }
041
042    public SimpleSqlConnectionSource(String strUrl, String strUser, String strPwd)
043    {
044        m_strUrl = strUrl;
045        m_strUser = strUser;
046        m_strPwd = strPwd;
047    }
048
049    /**
050     * @see org.apache.tapestry.contrib.table.model.sql.ISqlConnectionSource#obtainConnection()
051     */
052    public Connection obtainConnection() throws SQLException
053    {
054        if (m_strUser == null)
055            return DriverManager.getConnection(m_strUrl);
056
057        return DriverManager.getConnection(m_strUrl, m_strUser, m_strPwd);
058    }
059
060    /**
061     * @see org.apache.tapestry.contrib.table.model.sql.ISqlConnectionSource#returnConnection(Connection)
062     */
063    public void returnConnection(Connection objConnection)
064    {
065        try
066        {
067            objConnection.close();
068        }
069        catch (SQLException e)
070        {
071            LOG.warn("Could not close connection", e);
072        }
073    }
074
075}