1 19 package com.mysql.jdbc.jdbc2.optional; 20 21 import java.io.PrintWriter ; 22 import java.io.Serializable ; 23 24 import java.sql.SQLException ; 25 26 import java.util.Properties ; 27 28 import javax.naming.NamingException ; 29 import javax.naming.Reference ; 30 import javax.naming.Referenceable ; 31 import javax.naming.StringRefAddr ; 32 33 import javax.sql.DataSource ; 34 35 36 41 public class MysqlDataSource implements DataSource , Referenceable , Serializable { 42 43 protected static com.mysql.jdbc.Driver mysqlDriver = null; 44 45 static { 46 try { 47 mysqlDriver = (com.mysql.jdbc.Driver) Class.forName( 48 "com.mysql.jdbc.Driver").newInstance(); 49 } catch (Exception E) { 50 throw new RuntimeException ( 51 "Can not load Driver class com.mysql.jdbc.Driver"); 52 } 53 } 54 55 56 protected PrintWriter logWriter = null; 57 58 59 protected String databaseName = null; 60 61 62 protected String encoding = null; 63 64 65 protected String hostName = null; 66 67 68 protected String password = null; 69 70 71 protected String profileSql = "false"; 72 73 74 protected String url = null; 75 76 77 protected String user = null; 78 79 80 protected boolean explicitUrl = false; 81 82 83 protected int port = 3306; 84 85 88 public MysqlDataSource() { 89 } 90 91 99 public java.sql.Connection getConnection() throws SQLException { 100 return getConnection(user, password); 101 } 102 103 113 public java.sql.Connection getConnection(String userID, String password) 114 throws SQLException { 115 Properties props = new Properties (); 116 117 if (userID == null) { 118 userID = ""; 119 } 120 121 if (password == null) { 122 password = ""; 123 } 124 125 props.put("user", userID); 126 props.put("password", password); 127 props.put("profileSql", getProfileSql()); 128 129 return getConnection(props); 130 } 131 132 137 public void setDatabaseName(String dbName) { 138 databaseName = dbName; 139 } 140 141 146 public String getDatabaseName() { 147 return (databaseName != null) ? databaseName : ""; 148 } 149 150 155 public void setLogWriter(PrintWriter output) throws SQLException { 156 logWriter = output; 157 } 158 159 164 public java.io.PrintWriter getLogWriter() { 165 return logWriter; 166 } 167 168 175 public void setLoginTimeout(int seconds) throws SQLException { 176 } 177 178 183 public int getLoginTimeout() { 184 return 0; 185 } 186 187 192 public void setPassword(String pass) { 193 password = pass; 194 } 195 196 201 public void setPort(int p) { 202 port = p; 203 } 204 205 210 public int getPort() { 211 return port; 212 } 213 214 221 public void setPortNumber(int p) { 222 setPort(p); 223 } 224 225 230 public int getPortNumber() { 231 return getPort(); 232 } 233 234 239 public void setProfileSql(String flag) { 240 profileSql = flag; 241 } 242 243 248 public String getProfileSql() { 249 return profileSql; 250 } 251 252 259 public Reference getReference() throws NamingException { 260 String factoryName = "com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory"; 261 Reference ref = new Reference (getClass().getName(), factoryName, null); 262 ref.add(new StringRefAddr ("user", getUser())); 263 ref.add(new StringRefAddr ("password", password)); 264 ref.add(new StringRefAddr ("serverName", getServerName())); 265 ref.add(new StringRefAddr ("port", "" + getPort())); 266 ref.add(new StringRefAddr ("databaseName", getDatabaseName())); 267 ref.add(new StringRefAddr ("profileSql", getProfileSql())); 268 269 return ref; 270 } 271 272 277 public void setServerName(String serverName) { 278 hostName = serverName; 279 } 280 281 286 public String getServerName() { 287 return (hostName != null) ? hostName : ""; 288 } 289 290 295 300 public void setURL(String url) { 301 setUrl(url); 302 } 303 304 309 public String getURL() { 310 return getUrl(); 311 } 312 313 320 public void setUrl(String url) { 321 this.url = url; 322 explicitUrl = true; 323 } 324 325 331 public String getUrl() { 332 if (!explicitUrl) { 333 String builtUrl = "jdbc:mysql://"; 334 builtUrl = builtUrl + getServerName() + ":" + getPort() + "/" 335 + getDatabaseName(); 336 337 return builtUrl; 338 } else { 339 return this.url; 340 } 341 } 342 343 348 public void setUser(String userID) { 349 user = userID; 350 } 351 352 357 public String getUser() { 358 return user; 359 } 360 361 370 protected java.sql.Connection getConnection(Properties props) 371 throws SQLException { 372 String jdbcUrlToUse = null; 373 374 if (!explicitUrl) { 375 StringBuffer jdbcUrl = new StringBuffer ("jdbc:mysql://"); 376 377 if (hostName != null) { 378 jdbcUrl.append(hostName); 379 } 380 381 jdbcUrl.append(":"); 382 jdbcUrl.append(port); 383 jdbcUrl.append("/"); 384 385 if (databaseName != null) { 386 jdbcUrl.append(databaseName); 387 } 388 389 jdbcUrlToUse = jdbcUrl.toString(); 390 } else { 391 jdbcUrlToUse = this.url; 392 } 393 394 return mysqlDriver.connect(jdbcUrlToUse, props); 395 } 396 } 397 | Popular Tags |