1 package com.quadcap.services; 2 3 40 41 import java.io.PrintWriter ; 42 43 import java.util.Properties ; 44 45 import java.sql.Driver ; 46 import java.sql.DriverManager ; 47 import java.sql.SQLException ; 48 49 import javax.naming.Reference ; 50 import javax.naming.Referenceable ; 51 import javax.naming.StringRefAddr ; 52 53 60 public class DataSource implements javax.sql.DataSource , Referenceable { 61 String name; 62 String driverClass = null; 63 PrintWriter logWriter = null; 64 int loginTimeout = 0; 65 String url; 66 Properties props = new Properties (); 67 Driver driver = null; 68 69 72 public DataSource() { 73 } 74 75 public void setName(String name) { 76 this.name = name; 77 } 78 79 public String getName() { 80 return name; 81 } 82 83 public void setDriverClass(String s) { 84 this.driverClass = s; 85 try { 86 Class.forName(driverClass); 87 } catch (Throwable t) { 88 } 89 } 90 91 public String getDriverClass() { 92 return driverClass; 93 } 94 95 98 public String getUrl() { return url; } 99 100 103 public void setUrl(String url) { 104 this.url = url; 105 } 106 107 113 public void addConnectionProperty(String name, String val) { 114 props.setProperty(name, val); 115 } 116 117 123 public java.sql.Connection getConnection() 124 throws SQLException 125 { 126 return getDriver().connect(url, props); 127 } 128 129 139 public java.sql.Connection getConnection(String username, String password) 140 throws SQLException 141 { 142 Properties p = new Properties (props); 143 p.setProperty("user", username); 144 p.setProperty("password", password); 145 return getDriver().connect(url, p); 146 } 147 148 151 public PrintWriter getLogWriter() { 152 return logWriter; 153 } 154 155 164 public void setLogWriter(PrintWriter w) { 165 this.logWriter = w; 166 } 167 168 173 public int getLoginTimeout() { 174 return loginTimeout; 175 } 176 177 184 public void setLoginTimeout(int timeout) { 185 this.loginTimeout = timeout; 186 } 187 188 public Reference getReference() { 189 Reference ref = new Reference (DataSource.class.getName(), 190 "com.quadcap.services.DataSources", 191 null); 192 ref.add(new StringRefAddr ("name", name)); 193 return ref; 194 } 195 196 final private Driver getDriver() throws SQLException { 197 if (driver == null) { 198 driver = DriverManager.getDriver(url); 199 } 200 return driver; 201 } 202 } 203 204 | Popular Tags |