1 /* DriverManager.java -- Manage JDBC drivers
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
30 import java.io.PrintStream;
31 import java.io.PrintWriter;
32 import java.util.Enumeration;
33 import java.util.Properties;
34 import java.util.StringTokenizer;
35 import java.util.Vector;
38 * This class manages the JDBC drivers in the system. It maintains a
39 * registry of drivers and locates the appropriate driver to handle a
42 * On startup, <code>DriverManager</code> loads all the managers specified
43 * by the system property <code>jdbc.drivers</code>. The value of this
44 * property should be a colon separated list of fully qualified driver
45 * class names. Additional drivers can be loaded at any time by
46 * simply loading the driver class with <code>class.forName(String)</code>.
47 * The driver should automatically register itself in a static
50 * The methods in this class are all <code>static</code>. This class
51 * cannot be instantiated.
53 * @author Aaron M. Renn (arenn@urbanophile.com)
55 public class DriverManager
63 * This is the log stream for JDBC drivers.
65 private static PrintStream log_stream;
68 * This is the log writer for JDBC drivers.
70 private static PrintWriter log_writer;
73 * This is the login timeout used by JDBC drivers.
75 private static int login_timeout;
78 * This is the list of JDBC drivers that are loaded.
80 private static Vector drivers;
81 // Hmm, seems like we might want to do a Hashtable and lookup by something,
82 // but what would it be?
84 // Load all drivers on startup
87 drivers = new Vector();
89 String driver_string = System.getProperty("jdbc.drivers");
90 if (driver_string != null)
92 StringTokenizer st = new StringTokenizer(driver_string);
93 while (st.hasMoreTokens())
95 String driver_classname = st.nextToken();
99 Class.forName(driver_classname); // The driver registers itself
101 catch (Exception e) { ; } // Ignore not founds
107 /*************************************************************************/
114 * This method returns the login timeout in use by JDBC drivers systemwide.
116 * @return The login timeout.
121 return(login_timeout);
124 /*************************************************************************/
127 * This method set the login timeout used by JDBC drivers. This is a
128 * system-wide parameter that applies to all drivers.
130 * @param login_timeout The new login timeout value.
133 setLoginTimeout(int login_timeout)
135 DriverManager.login_timeout = login_timeout;
138 /*************************************************************************/
141 * This method returns the log writer being used by all JDBC drivers.
142 * This method should be used in place of the deprecated
143 * <code>getLogStream</code> method.
145 * @return The log writer in use by JDBC drivers.
147 public static PrintWriter
153 /*************************************************************************/
156 * This method sets the log writer being used by JDBC drivers. This is a
157 * system-wide parameter that affects all drivers. Note that since there
158 * is no way to retrieve a <code>PrintStream</code> from a
159 * <code>PrintWriter</code>, this method cannot set the log stream in
160 * use by JDBC. Thus any older drivers may not see this setting.
162 * @param log_writer The new log writer for JDBC.
165 setLogWriter(PrintWriter log_writer)
167 DriverManager.log_writer = log_writer;
170 /*************************************************************************/
173 * This method returns the log stream in use by JDBC.
175 * @return The log stream in use by JDBC.
177 * @deprecated Use <code>getLogWriter()</code> instead.
179 public static PrintStream
185 /*************************************************************************/
188 * This method sets the log stream in use by JDBC.
190 * @param log_stream The log stream in use by JDBC.
192 * @deprecated Use <code>setLogWriter</code> instead.
195 setLogStream(PrintStream log_stream)
197 DriverManager.log_stream = log_stream;
200 /*************************************************************************/
203 * This method prints the specified line to the log stream.
205 * @param str The string to write to the log stream.
210 if (log_stream != null) // Watch for user not using logging
211 log_stream.println(str);
214 /*************************************************************************/
217 * This method registers a new driver with the manager. This is normally
218 * called by the driver itself in a static initializer.
220 * @param driver The new <code>Driver</code> to add.
222 * @exception SQLException If an error occurs.
225 registerDriver(Driver driver) throws SQLException
227 if (!drivers.contains(driver))
228 drivers.addElement(driver);
231 /*************************************************************************/
234 * This method de-registers a driver from the manager.
236 * @param driver The <code>Driver</code> to unregister.
238 * @exception SQLException If an error occurs.
241 deregisterDriver(Driver driver) throws SQLException
243 if (drivers.contains(driver))
244 drivers.removeElement(driver);
247 /*************************************************************************/
250 * This method returns a list of all the currently registered JDBC drivers
251 * that were loaded by the current <code>ClassLoader</code>.
253 * @return An <code>Enumeration</code> of all currently loaded JDBC drivers.
255 public static Enumeration
258 Vector v = new Vector();
259 Enumeration e = drivers.elements();
262 ClassLoader cl = Thread.currentThread().getContextClassLoader();
264 while(e.hasMoreElements())
266 Object obj = e.nextElement();
268 ClassLoader loader = obj.getClass().getClassLoader();
271 loader = ClassLoader.getSystemClassLoader();
272 if (!loader.equals(cl))
278 return(v.elements());
281 /*************************************************************************/
284 * This method returns a driver that can connect to the specified
285 * JDBC URL string. This will be selected from among drivers loaded
286 * at initialization time and those drivers manually loaded by the
287 * same class loader as the caller.
289 * @param url The JDBC URL string to find a driver for.
291 * @return A <code>Driver</code> that can connect to the specified
292 * URL, or <code>null</code> if a suitable driver cannot be found.
294 * @exception SQLException If an error occurs.
297 getDriver(String url) throws SQLException
299 // FIXME: Limit driver search to the appropriate subset of loaded drivers.
301 Enumeration e = drivers.elements();
302 while(e.hasMoreElements())
304 Driver d = (Driver)e.nextElement();
305 if (d.acceptsURL(url))
312 /*************************************************************************/
315 * This method attempts to return a connection to the specified
318 * @param url The JDBC URL string to connect to.
320 * @return A <code>Connection</code> to that URL.
322 * @exception SQLException If an error occurs.
324 public static Connection
325 getConnection(String url) throws SQLException
327 return(getConnection(url, new Properties()));
330 /*************************************************************************/
333 * This method attempts to return a connection to the specified
334 * JDBC URL string using the specified username and password.
336 * @param url The JDBC URL string to connect to.
337 * @param user The username to connect with.
338 * @param password The password to connect with.
340 * @return A <code>Connection</code> to that URL.
342 * @exception SQLException If an error occurs.
344 public static Connection
345 getConnection(String url, String user, String password) throws SQLException
347 Properties p = new Properties();
350 p.setProperty("user", user);
351 if (password != null)
352 p.setProperty("password", password);
354 return(getConnection(url, p));
357 /*************************************************************************/
360 * This method attempts to return a connection to the specified
361 * JDBC URL string using the specified connection properties.
363 * @param url The JDBC URL string to connect to.
364 * @param properties The connection properties.
366 * @return A <code>Connection</code> to that URL.
368 * @exception SQLException If an error occurs.
370 public static Connection
371 getConnection(String url, Properties properties) throws SQLException
373 Driver d = getDriver(url);
375 throw new SQLException("Driver not found for URL: " + url);
377 return(d.connect(url, properties));
380 /*************************************************************************/
386 // Keep bozos from trying to instantiate us.
393 } // class DriverManager