KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > sql > DataSourceWrapper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.tag.common.sql;
18
19 import java.io.PrintWriter JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.DriverManager JavaDoc;
22 import java.sql.SQLException JavaDoc;
23
24 import javax.sql.DataSource JavaDoc;
25
26 import org.apache.taglibs.standard.resources.Resources;
27
28
29 /**
30  * <p>A simple <code>DataSource</code> wrapper for the standard
31  * <code>DriverManager</code> class.
32  *
33  * @author Hans Bergsten
34  */

35 public class DataSourceWrapper implements DataSource JavaDoc {
36     private String JavaDoc driverClassName;
37     private String JavaDoc jdbcURL;
38     private String JavaDoc userName;
39     private String JavaDoc password;
40
41     public void setDriverClassName(String JavaDoc driverClassName)
42     throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc,
43            IllegalAccessException JavaDoc {
44
45     this.driverClassName = driverClassName;
46         Class.forName(driverClassName, true,
47             Thread.currentThread().getContextClassLoader()).newInstance();
48     }
49
50     public void setJdbcURL(String JavaDoc jdbcURL) {
51     this.jdbcURL = jdbcURL;
52     }
53
54     public void setUserName(String JavaDoc userName) {
55     this.userName = userName;
56     }
57
58     public void setPassword(String JavaDoc password) {
59     this.password = password;
60     }
61
62     /**
63      * Returns a Connection using the DriverManager and all
64      * set properties.
65      */

66     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
67     Connection JavaDoc conn = null;
68     if (userName != null) {
69         conn = DriverManager.getConnection(jdbcURL, userName, password);
70     }
71     else {
72         conn = DriverManager.getConnection(jdbcURL);
73     }
74     return conn;
75     }
76
77     /**
78      * Always throws a SQLException. Username and password are set
79      * in the constructor and can not be changed.
80      */

81     public Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password)
82             throws SQLException JavaDoc {
83         throw new SQLException JavaDoc(Resources.getMessage("NOT_SUPPORTED"));
84     }
85     
86     /**
87      * Always throws a SQLException. Not supported.
88      */

89     public int getLoginTimeout() throws SQLException JavaDoc {
90         throw new SQLException JavaDoc(Resources.getMessage("NOT_SUPPORTED"));
91     }
92     
93     /**
94      * Always throws a SQLException. Not supported.
95      */

96     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
97         throw new SQLException JavaDoc(Resources.getMessage("NOT_SUPPORTED"));
98     }
99     
100     /**
101      * Always throws a SQLException. Not supported.
102      */

103     public void setLoginTimeout(int seconds) throws SQLException JavaDoc {
104         throw new SQLException JavaDoc(Resources.getMessage("NOT_SUPPORTED"));
105     }
106     
107     /**
108      * Always throws a SQLException. Not supported.
109      */

110     public synchronized void setLogWriter(PrintWriter JavaDoc out) throws SQLException JavaDoc {
111         throw new SQLException JavaDoc(Resources.getMessage("NOT_SUPPORTED"));
112     }
113
114
115 }
116
Popular Tags