View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2toolkit library.
13   *
14   *  The Initial Owner of the Original Code is
15   *  Power Of Two S.R.L. (www.pow2.com)
16   *
17   *  Portions created by Power Of Two S.R.L. are
18   *  Copyright (C) Power Of Two S.R.L.
19   *  All Rights Reserved.
20   *
21   * Contributor(s):
22   *
23   *  Henner Kollman <Henner.Kollmann@aucos.de>
24   *    adds the support for custom JDBC properties
25   */
26  
27  package com.pow2.dao;
28  
29  
30  import java.util.*;
31  import java.sql.*;
32  
33  
34  /***
35   *  Simple Connection provider.
36   *  <br>
37   *  provides non-pooled connections.
38   *
39   * @author  Luca Fossato <fossato@pow2.com>
40   * @created  25 giugno 2002
41   */
42  public class SimpleConnectionProvider extends ConnectionProvider
43  {
44    /***
45     *  Default constructor.
46     *
47     * @exception  Exception Description of the Exception
48     * @throws  Exception because of the <code>throws Exception</code> clause
49     *          of the  <code>init</code> method.
50     */
51    public SimpleConnectionProvider() throws Exception
52    {
53      super();
54    }
55  
56  
57    /***
58     *  Get a JDBC Connection
59     *
60     * @return  a JDBC Connection
61     * @exception  SQLException Description of the Exception
62     */
63    protected Connection getConnection() throws SQLException
64    {
65      Properties props  = prefs.getProperties();
66      Connection con    = null;
67  
68      // uses custom jdbc properties;
69      if ((props != null) && !props.isEmpty())
70      {
71        props.put("user",     prefs.getUser());
72        props.put("password", prefs.getPassword());
73        con = DriverManager.getConnection(prefs.getJdbcURL(), props);
74      }
75  
76      // "plain" flavour;
77      else
78      {
79        con = DriverManager.getConnection(prefs.getJdbcURL(), prefs.getUser(), prefs.getPassword());
80      }
81  
82      return con;
83    }
84  
85  
86    /***
87     *  Initialize the ConnectionProvider.
88     *
89     * @throws  Exception if any error occurs
90     */
91    protected void init() throws Exception
92    {
93      Class.forName(prefs.getJdbcDriver()).newInstance();
94    }
95  }