* gcc.c-torture/compile/20001226-1.x: Only xfail for Xtensa
[official-gcc.git] / libjava / java / sql / DriverManager.java
blobaf6ef12324a95091ef34de7ebeb6337a8d40de45
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)
9 any later version.
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
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.sql;
41 import java.io.PrintStream;
42 import java.io.PrintWriter;
43 import java.util.Enumeration;
44 import java.util.Properties;
45 import java.util.StringTokenizer;
46 import java.util.Vector;
48 /**
49 * This class manages the JDBC drivers in the system. It maintains a
50 * registry of drivers and locates the appropriate driver to handle a
51 * JDBC database URL.
52 * <p>
53 * On startup, <code>DriverManager</code> loads all the managers specified
54 * by the system property <code>jdbc.drivers</code>. The value of this
55 * property should be a colon separated list of fully qualified driver
56 * class names. Additional drivers can be loaded at any time by
57 * simply loading the driver class with <code>class.forName(String)</code>.
58 * The driver should automatically register itself in a static
59 * initializer.
60 * <p>
61 * The methods in this class are all <code>static</code>. This class
62 * cannot be instantiated.
64 * @author Aaron M. Renn (arenn@urbanophile.com)
66 public class DriverManager
70 * Class Variables
73 /**
74 * This is the log stream for JDBC drivers.
76 private static PrintStream log_stream;
78 /**
79 * This is the log writer for JDBC drivers.
81 private static PrintWriter log_writer;
83 /**
84 * This is the login timeout used by JDBC drivers.
86 private static int login_timeout;
88 /**
89 * This is the list of JDBC drivers that are loaded.
91 private static Vector drivers;
92 // Hmm, seems like we might want to do a Hashtable and lookup by something,
93 // but what would it be?
95 // Load all drivers on startup
96 static
98 drivers = new Vector();
100 String driver_string = System.getProperty("jdbc.drivers");
101 if (driver_string != null)
103 StringTokenizer st = new StringTokenizer(driver_string);
104 while (st.hasMoreTokens())
106 String driver_classname = st.nextToken();
110 Class.forName(driver_classname); // The driver registers itself
112 catch (Exception e) { ; } // Ignore not founds
118 /*************************************************************************/
121 * Class Methods
125 * This method returns the login timeout in use by JDBC drivers systemwide.
127 * @return The login timeout.
129 public static int
130 getLoginTimeout()
132 return(login_timeout);
135 /*************************************************************************/
138 * This method set the login timeout used by JDBC drivers. This is a
139 * system-wide parameter that applies to all drivers.
141 * @param login_timeout The new login timeout value.
143 public static void
144 setLoginTimeout(int login_timeout)
146 DriverManager.login_timeout = login_timeout;
149 /*************************************************************************/
152 * This method returns the log writer being used by all JDBC drivers.
153 * This method should be used in place of the deprecated
154 * <code>getLogStream</code> method.
156 * @return The log writer in use by JDBC drivers.
158 public static PrintWriter
159 getLogWriter()
161 return(log_writer);
164 /*************************************************************************/
167 * This method sets the log writer being used by JDBC drivers. This is a
168 * system-wide parameter that affects all drivers. Note that since there
169 * is no way to retrieve a <code>PrintStream</code> from a
170 * <code>PrintWriter</code>, this method cannot set the log stream in
171 * use by JDBC. Thus any older drivers may not see this setting.
173 * @param log_writer The new log writer for JDBC.
175 public static void
176 setLogWriter(PrintWriter log_writer)
178 DriverManager.log_writer = log_writer;
181 /*************************************************************************/
184 * This method returns the log stream in use by JDBC.
186 * @return The log stream in use by JDBC.
188 * @deprecated Use <code>getLogWriter()</code> instead.
190 public static PrintStream
191 getLogStream()
193 return(log_stream);
196 /*************************************************************************/
199 * This method sets the log stream in use by JDBC.
201 * @param log_stream The log stream in use by JDBC.
203 * @deprecated Use <code>setLogWriter</code> instead.
205 public static void
206 setLogStream(PrintStream log_stream)
208 DriverManager.log_stream = log_stream;
211 /*************************************************************************/
214 * This method prints the specified line to the log stream.
216 * @param str The string to write to the log stream.
218 public static void
219 println(String str)
221 if (log_stream != null) // Watch for user not using logging
222 log_stream.println(str);
225 /*************************************************************************/
228 * This method registers a new driver with the manager. This is normally
229 * called by the driver itself in a static initializer.
231 * @param driver The new <code>Driver</code> to add.
233 * @exception SQLException If an error occurs.
235 public static void
236 registerDriver(Driver driver) throws SQLException
238 if (!drivers.contains(driver))
239 drivers.addElement(driver);
242 /*************************************************************************/
245 * This method de-registers a driver from the manager.
247 * @param driver The <code>Driver</code> to unregister.
249 * @exception SQLException If an error occurs.
251 public static void
252 deregisterDriver(Driver driver) throws SQLException
254 if (drivers.contains(driver))
255 drivers.removeElement(driver);
258 /*************************************************************************/
261 * This method returns a list of all the currently registered JDBC drivers
262 * that were loaded by the current <code>ClassLoader</code>.
264 * @return An <code>Enumeration</code> of all currently loaded JDBC drivers.
266 public static Enumeration
267 getDrivers()
269 Vector v = new Vector();
270 Enumeration e = drivers.elements();
272 // Is this right?
273 ClassLoader cl = Thread.currentThread().getContextClassLoader();
275 while(e.hasMoreElements())
277 Object obj = e.nextElement();
279 ClassLoader loader = obj.getClass().getClassLoader();
281 if (loader == null)
282 loader = ClassLoader.getSystemClassLoader();
283 if (!loader.equals(cl))
284 continue;
286 v.addElement(obj);
289 return(v.elements());
292 /*************************************************************************/
295 * This method returns a driver that can connect to the specified
296 * JDBC URL string. This will be selected from among drivers loaded
297 * at initialization time and those drivers manually loaded by the
298 * same class loader as the caller.
300 * @param url The JDBC URL string to find a driver for.
302 * @return A <code>Driver</code> that can connect to the specified
303 * URL, or <code>null</code> if a suitable driver cannot be found.
305 * @exception SQLException If an error occurs.
307 public static Driver
308 getDriver(String url) throws SQLException
310 // FIXME: Limit driver search to the appropriate subset of loaded drivers.
312 Enumeration e = drivers.elements();
313 while(e.hasMoreElements())
315 Driver d = (Driver)e.nextElement();
316 if (d.acceptsURL(url))
317 return(d);
320 return(null);
323 /*************************************************************************/
326 * This method attempts to return a connection to the specified
327 * JDBC URL string.
329 * @param url The JDBC URL string to connect to.
331 * @return A <code>Connection</code> to that URL.
333 * @exception SQLException If an error occurs.
335 public static Connection
336 getConnection(String url) throws SQLException
338 return(getConnection(url, new Properties()));
341 /*************************************************************************/
344 * This method attempts to return a connection to the specified
345 * JDBC URL string using the specified username and password.
347 * @param url The JDBC URL string to connect to.
348 * @param user The username to connect with.
349 * @param password The password to connect with.
351 * @return A <code>Connection</code> to that URL.
353 * @exception SQLException If an error occurs.
355 public static Connection
356 getConnection(String url, String user, String password) throws SQLException
358 Properties p = new Properties();
360 if (user != null)
361 p.setProperty("user", user);
362 if (password != null)
363 p.setProperty("password", password);
365 return(getConnection(url, p));
368 /*************************************************************************/
371 * This method attempts to return a connection to the specified
372 * JDBC URL string using the specified connection properties.
374 * @param url The JDBC URL string to connect to.
375 * @param properties The connection properties.
377 * @return A <code>Connection</code> to that URL.
379 * @exception SQLException If an error occurs.
381 public static Connection
382 getConnection(String url, Properties properties) throws SQLException
384 Driver d = getDriver(url);
385 if (d == null)
386 throw new SQLException("Driver not found for URL: " + url);
388 return(d.connect(url, properties));
391 /*************************************************************************/
394 * Constructors
397 // Keep bozos from trying to instantiate us.
398 private
399 DriverManager()
404 } // class DriverManager