1 10 11 package com.triactive.jdo; 12 13 import java.io.PrintWriter ; 14 import java.sql.Connection ; 15 import java.sql.DriverManager ; 16 import java.sql.SQLException ; 17 import javax.jdo.JDOFatalUserException; 18 import javax.sql.DataSource ; 19 20 21 public class DriverManagerDataSource implements DataSource 22 { 23 private final String driverName; 24 private final String URL; 25 26 27 public DriverManagerDataSource(String driverName, String URL) 28 { 29 this.driverName = driverName; 30 this.URL = URL; 31 32 if (driverName != null) 33 { 34 try 35 { 36 Class.forName(driverName).newInstance(); 37 } 38 catch (Exception e) 39 { 40 throw new JDOFatalUserException("Invalid driver class " + driverName, e); 41 } 42 } 43 } 44 45 public Connection getConnection() throws SQLException 46 { 47 return DriverManager.getConnection(URL); 48 } 49 50 public Connection getConnection(String userName, String password) throws SQLException 51 { 52 return DriverManager.getConnection(URL, userName, password); 53 } 54 55 public PrintWriter getLogWriter() throws SQLException 56 { 57 return DriverManager.getLogWriter(); 58 } 59 60 public void setLogWriter(PrintWriter out) throws SQLException 61 { 62 DriverManager.setLogWriter(out); 63 } 64 65 public int getLoginTimeout() throws SQLException 66 { 67 return DriverManager.getLoginTimeout(); 68 } 69 70 public void setLoginTimeout(int seconds) throws SQLException 71 { 72 DriverManager.setLoginTimeout(seconds); 73 } 74 75 public boolean equals(Object obj) 76 { 77 if (obj == this) 78 return true; 79 80 if (!(obj instanceof DriverManagerDataSource)) 81 return false; 82 83 DriverManagerDataSource dmds = (DriverManagerDataSource)obj; 84 85 if (driverName == null) { if (dmds.driverName != null) return false; } 86 else if (!driverName.equals(dmds.driverName)) return false; 87 88 if (URL == null) { if (dmds.URL != null) return false; } 89 else if (!URL.equals(dmds.URL)) return false; 90 91 return true; 92 } 93 94 public int hashCode() 95 { 96 return (driverName == null ? 0 : driverName.hashCode()) 97 ^ (URL == null ? 0 : URL.hashCode()); 98 } 99 } 100 | Popular Tags |