KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nqadmin > swingSet > datasources > SSConnection


1 /* $Id: SSConnection.java,v 1.9 2005/02/21 20:29:15 prasanth Exp $
2  *
3  * Tab Spacing = 4
4  *
5  * Copyright (c) 2004-2005, The Pangburn Company and Prasanth R. Pasala
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer. Redistributions in binary
13  * form must reproduce the above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or other materials
15  * provided with the distribution. The names of its contributors may not be
16  * used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package com.nqadmin.swingSet.datasources;
34
35 import java.sql.Connection JavaDoc;
36 import java.sql.DriverManager JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.lang.ClassNotFoundException JavaDoc;
40 import java.io.ObjectInputStream JavaDoc;
41 import java.io.Serializable JavaDoc;
42 import java.beans.PropertyChangeSupport JavaDoc;
43 import java.beans.PropertyChangeListener JavaDoc;
44 import java.beans.VetoableChangeSupport JavaDoc;
45 import java.beans.VetoableChangeListener JavaDoc;
46 import java.beans.PropertyVetoException JavaDoc;
47  
48 /**
49  * SSConnection.java
50  *<p>
51  * SwingSet - Open Toolkit For Making Swing Controls Database-Aware
52  *<p><pre>
53  * The SSConnection class is a wrapper for Connection.
54  * It provides methods to specify the url, username, password & driver class name.
55  * The createConnection should be called before calling the getConnection method.
56  * When ever any connection parameters are changed createConnection has to be called
57  * to change to connection object.
58   *</pre><p>
59  * @author $Author: prasanth $
60  * @version $Revision: 1.9 $
61  */

62  public class SSConnection implements Serializable JavaDoc {
63
64     /**
65      * URL to the database.
66      */

67     protected String JavaDoc url = "";
68
69     /**
70      * Username to be used while connecting to the database.
71      */

72     protected String JavaDoc username = "";
73
74     /**
75      * Password to be used for the username specified.
76      */

77     protected String JavaDoc password = "";
78
79     /**
80      * Database driver class name.
81      */

82     protected String JavaDoc driverName = "";
83
84     /**
85      * Database connection object.
86      */

87     transient protected Connection JavaDoc connection;
88
89     /**
90      * Convenience class for providing the property change listener support
91      */

92     private PropertyChangeSupport JavaDoc pChangeSupport = new PropertyChangeSupport JavaDoc(this);
93     
94     /**
95      * Convenience class for providing the vetoable change listener support
96      */

97     private VetoableChangeSupport JavaDoc vChangeSupport = new VetoableChangeSupport JavaDoc(this);
98      
99     /**
100      * Constructs a default SSConnection object.
101      */

102     public SSConnection() {
103     }
104
105     /**
106      * Constructs a SSConnection object with the specified database url.
107      *
108      * @param _url - url to the database to which connection has to be established.
109      * the url should be of the form jdbc:subprotocol:subname
110      */

111     public SSConnection(String JavaDoc _url) {
112         url = _url;
113     }
114
115     /**
116      * Constructs a SSConnection object with the specified database url.
117      *
118      * @param _url - url to the database to which connection has to be established.
119      * the url should be of the form jdbc:subprotocol:subname
120      * @param _username - the database username on whose behalf the connection is being made
121      * @param _password - the user's password
122      */

123     public SSConnection(String JavaDoc _url, String JavaDoc _username, String JavaDoc _password) {
124         url = _url;
125         username = _username;
126         password = _password;
127     }
128
129     /**
130      * Constructs a SSConnection object with the specified database url.
131      *
132      * @param _url - url to the database to which connection has to be established.
133      * the url should be of the form jdbc:subprotocol:subname
134      * @param _username - the database username on whose behalf the connection is being made
135      * @param _password - the user's password
136      * @param _driverName - name of the database driver to be used.
137      */

138     public SSConnection(String JavaDoc _url, String JavaDoc _username, String JavaDoc _password, String JavaDoc _driverName) {
139         url = _url;
140         username = _username;
141         password = _password;
142         driverName = _driverName;
143     }
144     
145     /**
146      * Method to add bean property change listeners.
147      *
148      * @param _listener bean property change listener
149      */

150     public void addPropertyChangeListener(PropertyChangeListener JavaDoc _listener) {
151         pChangeSupport.addPropertyChangeListener(_listener);
152     }
153     
154     /**
155      * Method to remove bean property change listeners.
156      *
157      * @param _listener bean property change listener
158      */

159     public void removePropertyChangeListener(PropertyChangeListener JavaDoc _listener) {
160         pChangeSupport.removePropertyChangeListener(_listener);
161     }
162     
163     /**
164      * Method to add bean vetoable change listeners.
165      *
166      * @param _listener bean vetoable change listener
167      */

168     public void addVetoableChangeListener(VetoableChangeListener JavaDoc _listener) {
169         vChangeSupport.addVetoableChangeListener(_listener);
170     }
171     
172     /**
173      * Method to remove bean veto change listeners.
174      *
175      * @param _listener bean veto change listener
176      */

177     public void removeVetoableChangeListener(VetoableChangeListener JavaDoc _listener) {
178         vChangeSupport.removeVetoableChangeListener(_listener);
179     }
180
181     /**
182      * Sets the url to the database.
183      *
184      * @param _url - url to the database to which connection has to be established.
185      * the url should be of the form jdbc:subprotocol:subname
186      */

187     public void setUrl(String JavaDoc _url) {
188         String JavaDoc oldValue = url;
189         url = _url;
190         pChangeSupport.firePropertyChange("url", oldValue, url);
191         
192     }
193     
194     /**
195      * Returns the url to the database.
196      *
197      * @return returns the database url.
198      */

199     public String JavaDoc getUrl() {
200         return url;
201     }
202
203     /**
204      * Sets the username to be used while connecting to the database.
205      *
206      * @param _username - the database username on whose behalf the connection is being made
207      */

208     public void setUsername(String JavaDoc _username) {
209         String JavaDoc oldValue = username;
210         username = _username;
211         pChangeSupport.firePropertyChange("username", oldValue, username);
212     }
213     
214     /**
215      * Returns the username being used to connect to the database.
216      *
217      * @return returns the database username.
218      */

219     public String JavaDoc getUsername() {
220         return username;
221     }
222
223     /**
224      * Sets the password to be used while connecting to the database.
225      *
226      * @param _password - the user's password to be used.
227      */

228     public void setPassword(String JavaDoc _password) {
229         String JavaDoc oldValue = password;
230         password = _password;
231         pChangeSupport.firePropertyChange("password", oldValue, password);
232     }
233     
234     /**
235      * Returns the password being used to connect to the database.
236      *
237      * @return returns the user's password.
238      */

239     public String JavaDoc getPassword() {
240         return password;
241     }
242
243     /**
244      * Sets the database driver class name.
245      *
246      * @param _driverName - name of the database driver to be used.
247      */

248     public void setDriverName(String JavaDoc _driverName) {
249         String JavaDoc oldValue = driverName;
250         driverName = _driverName;
251         pChangeSupport.firePropertyChange("driverName", oldValue, password);
252     }
253     
254     /**
255      * Returns the database driver being used.
256      *
257      * @return returns the database driver being used.
258      */

259     public String JavaDoc getDriverName() {
260         return driverName;
261     }
262     
263     /**
264      * Returns the database connection object.
265      *
266      * @return returns the database connection object.
267      */

268     public Connection JavaDoc getConnection() {
269     // IF THE CONNECTION IS NOT YET CREATED BUT WE HAVE ALL THE INFORMATION TO CREATE ONE
270
// THEN GO AHEAD AND TRY CREATING THE CONNECTION
271
if(connection == null && url != null && driverName != null && username != null && password != null &&
272             !url.trim().equals("") && !driverName.trim().equals("")){
273             try{
274                 createConnection();
275             }catch(SQLException JavaDoc se){
276                 se.printStackTrace();
277             }catch(ClassNotFoundException JavaDoc cnfe){
278                 cnfe.printStackTrace();
279             }
280         }
281         return connection;
282     }
283
284     /**
285      * Creates a connection to the database based on the information provided
286      * by the user.
287      */

288     public void createConnection() throws SQLException JavaDoc, ClassNotFoundException JavaDoc{
289         Class.forName(driverName);
290         connection = DriverManager.getConnection(url, username, password);
291     }
292
293     /**
294      * Recreates the connection when the object is deserialized.
295      */

296     protected void readObject(ObjectInputStream JavaDoc objIn) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
297         objIn.defaultReadObject();
298         try{
299             createConnection();
300         }catch(SQLException JavaDoc se) {
301             se.printStackTrace();
302         }
303     }
304  }
305
306 /*
307  * $Log: SSConnection.java,v $
308  * Revision 1.9 2005/02/21 20:29:15 prasanth
309  * Trying to create connection when getConnection is called if required information
310  * is available.
311  *
312  * Revision 1.8 2005/02/11 22:59:55 yoda2
313  * Imported PropertyVetoException and added some bound properties.
314  *
315  * Revision 1.7 2005/02/11 20:16:27 yoda2
316  * Added infrastructure to support property & vetoable change listeners (for beans).
317  *
318  * Revision 1.6 2005/02/10 20:13:20 yoda2
319  * Setter/getter cleanup & method reordering for consistency.
320  *
321  * Revision 1.5 2005/02/09 23:04:01 yoda2
322  * JavaDoc cleanup.
323  *
324  * Revision 1.4 2005/02/09 06:39:06 prasanth
325  * Added PropertyChangeSupport
326  *
327  * Revision 1.3 2005/02/04 22:49:09 yoda2
328  * API cleanup & updated Copyright info.
329  *
330  * Revision 1.2 2004/11/11 14:45:57 yoda2
331  * Using TextPad, converted all tabs to "soft" tabs comprised of four actual spaces.
332  *
333  * Revision 1.1 2004/10/25 21:47:50 prasanth
334  * Initial Commit
335  *
336  */
Popular Tags