KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > conprovider > SingleConnectionProvider


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/conprovider/SingleConnectionProvider.java,v 1.8 2004/08/18 12:25:57 hkollmann Exp $
3  * $Revision: 1.8 $
4  * $Date: 2004/08/18 12:25:57 $
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 JavaDoc;
27 import java.sql.DriverManager JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import java.util.Properties JavaDoc;
31
32
33
34 /**
35  * Single Connection provider. <br> provides one connection for all
36  *
37  * @author Henner Kollmann
38  */

39 public class SingleConnectionProvider extends ConnectionProvider {
40    private static Connection JavaDoc con;
41
42    /**
43     * Default constructor.
44     *
45     * @exception Exception Description of the Exception
46     * @throws Exception because of the <code>throws Exception</code> clause of
47     * the <code>init</code> method.
48     */

49    public SingleConnectionProvider() throws Exception JavaDoc {
50       super();
51    }
52
53    /**
54     * Get a JDBC Connection
55     *
56     * @return a JDBC Connection
57     *
58     * @exception SQLException Description of the Exception
59     */

60    protected synchronized Connection JavaDoc getConnection() throws SQLException JavaDoc {
61       if (con == null) {
62          Properties JavaDoc props = getPrefs()
63                                .getProperties();
64
65          // uses custom jdbc properties;
66
if ((props != null) && !props.isEmpty()) {
67             props.put("user", getPrefs().getUser());
68             props.put("password", getPrefs().getPassword());
69             con = DriverManager.getConnection(getPrefs().getJdbcURL(), props);
70          }
71          // "plain" flavour;
72
else {
73             con = DriverManager.getConnection(getPrefs().getJdbcURL(),
74                                               getPrefs().getUser(),
75                                               getPrefs().getPassword());
76          }
77       }
78
79       return new SingleConnectionWrapper(con);
80    }
81
82
83    /**
84     * Initialize the ConnectionProvider.
85     *
86     * @throws Exception if any error occurs
87     */

88    protected void init() throws Exception JavaDoc {
89       Class.forName(getPrefs().getJdbcDriver())
90            .newInstance();
91    }
92 }
93
Popular Tags