1 32 33 package com.nqadmin.swingSet.datasources; 34 35 import java.sql.Connection ; 36 import java.sql.DriverManager ; 37 import java.sql.SQLException ; 38 import java.io.IOException ; 39 import java.lang.ClassNotFoundException ; 40 import java.io.ObjectInputStream ; 41 import java.io.Serializable ; 42 import java.beans.PropertyChangeSupport ; 43 import java.beans.PropertyChangeListener ; 44 import java.beans.VetoableChangeSupport ; 45 import java.beans.VetoableChangeListener ; 46 import java.beans.PropertyVetoException ; 47 48 62 public class SSConnection implements Serializable { 63 64 67 protected String url = ""; 68 69 72 protected String username = ""; 73 74 77 protected String password = ""; 78 79 82 protected String driverName = ""; 83 84 87 transient protected Connection connection; 88 89 92 private PropertyChangeSupport pChangeSupport = new PropertyChangeSupport (this); 93 94 97 private VetoableChangeSupport vChangeSupport = new VetoableChangeSupport (this); 98 99 102 public SSConnection() { 103 } 104 105 111 public SSConnection(String _url) { 112 url = _url; 113 } 114 115 123 public SSConnection(String _url, String _username, String _password) { 124 url = _url; 125 username = _username; 126 password = _password; 127 } 128 129 138 public SSConnection(String _url, String _username, String _password, String _driverName) { 139 url = _url; 140 username = _username; 141 password = _password; 142 driverName = _driverName; 143 } 144 145 150 public void addPropertyChangeListener(PropertyChangeListener _listener) { 151 pChangeSupport.addPropertyChangeListener(_listener); 152 } 153 154 159 public void removePropertyChangeListener(PropertyChangeListener _listener) { 160 pChangeSupport.removePropertyChangeListener(_listener); 161 } 162 163 168 public void addVetoableChangeListener(VetoableChangeListener _listener) { 169 vChangeSupport.addVetoableChangeListener(_listener); 170 } 171 172 177 public void removeVetoableChangeListener(VetoableChangeListener _listener) { 178 vChangeSupport.removeVetoableChangeListener(_listener); 179 } 180 181 187 public void setUrl(String _url) { 188 String oldValue = url; 189 url = _url; 190 pChangeSupport.firePropertyChange("url", oldValue, url); 191 192 } 193 194 199 public String getUrl() { 200 return url; 201 } 202 203 208 public void setUsername(String _username) { 209 String oldValue = username; 210 username = _username; 211 pChangeSupport.firePropertyChange("username", oldValue, username); 212 } 213 214 219 public String getUsername() { 220 return username; 221 } 222 223 228 public void setPassword(String _password) { 229 String oldValue = password; 230 password = _password; 231 pChangeSupport.firePropertyChange("password", oldValue, password); 232 } 233 234 239 public String getPassword() { 240 return password; 241 } 242 243 248 public void setDriverName(String _driverName) { 249 String oldValue = driverName; 250 driverName = _driverName; 251 pChangeSupport.firePropertyChange("driverName", oldValue, password); 252 } 253 254 259 public String getDriverName() { 260 return driverName; 261 } 262 263 268 public Connection getConnection() { 269 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 se){ 276 se.printStackTrace(); 277 }catch(ClassNotFoundException cnfe){ 278 cnfe.printStackTrace(); 279 } 280 } 281 return connection; 282 } 283 284 288 public void createConnection() throws SQLException , ClassNotFoundException { 289 Class.forName(driverName); 290 connection = DriverManager.getConnection(url, username, password); 291 } 292 293 296 protected void readObject(ObjectInputStream objIn) throws IOException , ClassNotFoundException { 297 objIn.defaultReadObject(); 298 try{ 299 createConnection(); 300 }catch(SQLException se) { 301 se.printStackTrace(); 302 } 303 } 304 } 305 306 | Popular Tags |