KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > DriverManagerDataSource


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: DriverManagerDataSource.java,v 1.4 2003/05/24 03:28:35 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.sql.Connection JavaDoc;
15 import java.sql.DriverManager JavaDoc;
16 import java.sql.SQLException JavaDoc;
17 import javax.jdo.JDOFatalUserException;
18 import javax.sql.DataSource JavaDoc;
19
20
21 public class DriverManagerDataSource implements DataSource JavaDoc
22 {
23     private final String JavaDoc driverName;
24     private final String JavaDoc URL;
25
26
27     public DriverManagerDataSource(String JavaDoc driverName, String JavaDoc 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 JavaDoc e)
39             {
40                 throw new JDOFatalUserException("Invalid driver class " + driverName, e);
41             }
42         }
43     }
44
45     public Connection JavaDoc getConnection() throws SQLException JavaDoc
46     {
47         return DriverManager.getConnection(URL);
48     }
49
50     public Connection JavaDoc getConnection(String JavaDoc userName, String JavaDoc password) throws SQLException JavaDoc
51     {
52         return DriverManager.getConnection(URL, userName, password);
53     }
54
55     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc
56     {
57         return DriverManager.getLogWriter();
58     }
59
60     public void setLogWriter(PrintWriter JavaDoc out) throws SQLException JavaDoc
61     {
62         DriverManager.setLogWriter(out);
63     }
64
65     public int getLoginTimeout() throws SQLException JavaDoc
66     {
67         return DriverManager.getLoginTimeout();
68     }
69
70     public void setLoginTimeout(int seconds) throws SQLException JavaDoc
71     {
72         DriverManager.setLoginTimeout(seconds);
73     }
74
75     public boolean equals(Object JavaDoc 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