1 23 24 package org.objectweb.jorm.mapper.rdb.lib; 25 26 import org.objectweb.jorm.api.PException; 27 28 import javax.sql.DataSource ; 29 import java.sql.DriverManager ; 30 import java.sql.SQLException ; 31 import java.sql.Connection ; 32 import java.io.PrintWriter ; 33 34 38 public class ConnectionSpecJDBC implements DataSource { 39 String url; 40 String user = null; 41 String passwd = null; 42 PrintWriter pw; 43 int timeout = 0; 44 45 48 public ConnectionSpecJDBC(String url, String driver) throws PException { 49 this.url = url; 50 try { 51 Class.forName(driver); 52 } catch (ClassNotFoundException e) { 53 throw new PException(e, "Unable to load JDBC driver class: " + driver); 54 } 55 } 56 57 60 public ConnectionSpecJDBC(String url, String driver, String user, String passwd) 61 throws PException { 62 this(url, driver); 63 this.user = user; 64 this.passwd = passwd; 65 } 66 67 68 71 public Connection getConnection() throws SQLException { 72 if (user != null && passwd != null) { 73 return DriverManager.getConnection(url, user, passwd); 74 } else { 75 return DriverManager.getConnection(url); 76 } 77 } 78 79 public Connection getConnection(String username, String password) 80 throws SQLException { 81 return DriverManager.getConnection(url, username, password); 82 } 83 84 public PrintWriter getLogWriter() throws SQLException { 85 return pw; 86 } 87 88 public void setLogWriter(PrintWriter out) throws SQLException { 89 pw = out; 90 } 91 92 public void setLoginTimeout(int seconds) throws SQLException { 93 timeout = seconds; 94 } 95 96 public int getLoginTimeout() throws SQLException { 97 return timeout; 98 } 99 } 100 | Popular Tags |