View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/conprovider/SimpleConnectionProvider.java,v 1.8 2005/11/30 20:31:17 hkollmann Exp $
3    * $Revision: 1.8 $
4    * $Date: 2005/11/30 20:31:17 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.conprovider;
25  
26  import java.sql.Connection;
27  import java.sql.DriverManager;
28  import java.sql.SQLException;
29  
30  import java.util.Properties;
31  
32  
33  
34  /***
35   * Simple Connection provider. <br> provides non-pooled connections.
36   *
37   * @author Luca Fossato
38   */
39  public class SimpleConnectionProvider extends AbstractConnectionProvider {
40     /***
41      * Default constructor.
42      *
43      * @exception Exception Description of the Exception
44      * @throws Exception because of the <code>throws Exception</code> clause of
45      *         the  <code>init</code> method.
46      */
47     public SimpleConnectionProvider() throws Exception {
48        super();
49     }
50  
51     /***
52      * Get a JDBC Connection
53      *
54      * @return a JDBC Connection
55      *
56      * @exception SQLException Description of the Exception
57      */
58     protected Connection getConnection() throws SQLException {
59        Properties props = getPrefs()
60                              .getProperties();
61        Connection con = null;
62  
63        // uses custom jdbc properties;
64        if ((props != null) && !props.isEmpty()) {
65           props.put("user", getPrefs().getUser());
66           props.put("password", getPrefs().getPassword());
67           con = DriverManager.getConnection(getPrefs().getJdbcURL(), props);
68        }
69        // "plain" flavour;
70        else {
71           con = DriverManager.getConnection(getPrefs().getJdbcURL(),
72                                             getPrefs().getUser(),
73                                             getPrefs().getPassword());
74        }
75  
76        return con;
77     }
78  
79  
80     /***
81      * Initialize the ConnectionProvider.
82      *
83      * @throws Exception if any error occurs
84      */
85     protected void init() throws Exception {
86        Class.forName(getPrefs().getJdbcDriver())
87             .newInstance();
88     }
89  }