Merge from mainline.
[official-gcc.git] / libjava / classpath / java / sql / DriverManager.java
blob94f743b92dad439ce85a7ffa25740a5ca329b769
1 /* DriverManager.java -- Manage JDBC drivers
2 Copyright (C) 1999, 2000, 2001, 2003, 2004, 2006
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 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
68 /**
69 * This is the log stream for JDBC drivers.
71 private static PrintStream log_stream;
73 /**
74 * This is the log writer for JDBC drivers.
76 private static PrintWriter log_writer;
78 /**
79 * This is the login timeout used by JDBC drivers.
81 private static int login_timeout;
83 /**
84 * This is the list of JDBC drivers that are loaded.
86 private static Vector drivers;
87 // Hmm, seems like we might want to do a Hashtable and lookup by something,
88 // but what would it be?
90 // Load all drivers on startup
91 static
93 drivers = new Vector();
95 String driver_string = System.getProperty("jdbc.drivers");
96 if (driver_string != null)
98 StringTokenizer st = new StringTokenizer(driver_string);
99 while (st.hasMoreTokens())
101 String driver_classname = st.nextToken();
105 Class.forName(driver_classname); // The driver registers itself
107 catch (Exception e)
109 // Ignore not founds
116 /** Can't be instantiated. */
117 private DriverManager()
122 * This method returns the log writer being used by all JDBC drivers.
123 * This method should be used in place of the deprecated
124 * <code>getLogStream</code> method.
126 * @return The log writer in use by JDBC drivers.
128 public static PrintWriter getLogWriter()
130 return log_writer;
134 * This method sets the log writer being used by JDBC drivers. This is a
135 * system-wide parameter that affects all drivers. Note that since there
136 * is no way to retrieve a <code>PrintStream</code> from a
137 * <code>PrintWriter</code>, this method cannot set the log stream in
138 * use by JDBC. Thus any older drivers may not see this setting.
140 * @param out The new log writer for JDBC.
142 public static void setLogWriter(PrintWriter out)
144 DriverManager.log_writer = out;
148 * This method attempts to return a connection to the specified
149 * JDBC URL string using the specified connection properties.
151 * @param url The JDBC URL string to connect to.
152 * @param properties The connection properties.
154 * @return A <code>Connection</code> to that URL.
156 * @exception SQLException If an error occurs.
158 public static Connection getConnection(String url, Properties properties)
159 throws SQLException
161 Driver d = getDriver(url);
162 if (d == null)
163 throw new SQLException("Driver not found for URL: " + url);
165 return d.connect(url, properties);
170 * This method attempts to return a connection to the specified
171 * JDBC URL string using the specified username and password.
173 * @param url The JDBC URL string to connect to.
174 * @param user The username to connect with.
175 * @param password The password to connect with.
176 * @return A <code>Connection</code> to that URL.
177 * @exception SQLException If an error occurs.
179 public static Connection getConnection(String url, String user,
180 String password) throws SQLException
182 Properties p = new Properties();
184 if (user != null)
185 p.setProperty("user", user);
186 if (password != null)
187 p.setProperty("password", password);
189 return getConnection(url, p);
193 * This method attempts to return a connection to the specified
194 * JDBC URL string.
196 * @param url The JDBC URL string to connect to.
198 * @return A <code>Connection</code> to that URL.
200 * @exception SQLException If an error occurs.
202 public static Connection getConnection(String url) throws SQLException
204 return getConnection(url, new Properties());
208 * This method returns a driver that can connect to the specified
209 * JDBC URL string. This will be selected from among drivers loaded
210 * at initialization time and those drivers manually loaded by the
211 * same class loader as the caller.
213 * @param url The JDBC URL string to find a driver for.
215 * @return A <code>Driver</code> that can connect to the specified
216 * URL.
218 * @exception SQLException If an error occurs, or no suitable driver can be found.
220 public static Driver getDriver(String url) throws SQLException
222 // FIXME: Limit driver search to the appropriate subset of loaded drivers.
223 Enumeration e = drivers.elements();
224 while(e.hasMoreElements())
226 Driver d = (Driver)e.nextElement();
227 if (d.acceptsURL(url))
228 return d;
231 throw new SQLException("No driver found for " + url);
235 * This method registers a new driver with the manager. This is normally
236 * called by the driver itself in a static initializer.
238 * @param driver The new <code>Driver</code> to add.
240 * @exception SQLException If an error occurs.
242 public static void registerDriver(Driver driver) throws SQLException
244 if (! drivers.contains(driver))
245 drivers.addElement(driver);
249 * This method de-registers a driver from the manager.
251 * @param driver The <code>Driver</code> to unregister.
253 * @exception SQLException If an error occurs.
255 public static void deregisterDriver(Driver driver) throws SQLException
257 if (drivers.contains(driver))
258 drivers.removeElement(driver);
262 * This method returns a list of all the currently registered JDBC drivers
263 * that were loaded by the current <code>ClassLoader</code>.
265 * @return An <code>Enumeration</code> of all currently loaded JDBC drivers.
267 public static Enumeration 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();
293 * This method set the login timeout used by JDBC drivers. This is a
294 * system-wide parameter that applies to all drivers.
296 * @param seconds The new login timeout value.
298 public static void setLoginTimeout(int seconds)
300 DriverManager.login_timeout = seconds;
304 * This method returns the login timeout in use by JDBC drivers systemwide.
306 * @return The login timeout.
308 public static int getLoginTimeout()
310 return login_timeout;
314 * This method sets the log stream in use by JDBC.
316 * @param stream The log stream in use by JDBC.
317 * @deprecated Use <code>setLogWriter</code> instead.
319 public static void setLogStream(PrintStream stream)
321 DriverManager.log_stream = stream;
325 * This method returns the log stream in use by JDBC.
327 * @return The log stream in use by JDBC.
328 * @deprecated Use <code>getLogWriter()</code> instead.
330 public static PrintStream getLogStream()
332 return log_stream;
336 * This method prints the specified line to the log stream.
338 * @param message The string to write to the log stream.
340 public static void println(String message)
342 if (log_stream != null) // Watch for user not using logging
343 log_stream.println(message);