source: trunk/src/gcc/libjava/java/sql/DriverManager.java@ 1124

Last change on this file since 1124 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 10.9 KB
Line 
1/* DriverManager.java -- Manage JDBC drivers
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.sql;
40
41import java.io.PrintStream;
42import java.io.PrintWriter;
43import java.util.Enumeration;
44import java.util.Properties;
45import java.util.StringTokenizer;
46import java.util.Vector;
47
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.
63 *
64 * @author Aaron M. Renn ([email protected])
65 */
66public class DriverManager
67{
68
69/*
70 * Class Variables
71 */
72
73/**
74 * This is the log stream for JDBC drivers.
75 */
76private static PrintStream log_stream;
77
78/**
79 * This is the log writer for JDBC drivers.
80 */
81private static PrintWriter log_writer;
82
83/**
84 * This is the login timeout used by JDBC drivers.
85 */
86private static int login_timeout;
87
88/**
89 * This is the list of JDBC drivers that are loaded.
90 */
91private static Vector drivers;
92 // Hmm, seems like we might want to do a Hashtable and lookup by something,
93 // but what would it be?
94
95// Load all drivers on startup
96static
97{
98 drivers = new Vector();
99
100 String driver_string = System.getProperty("jdbc.drivers");
101 if (driver_string != null)
102 {
103 StringTokenizer st = new StringTokenizer(driver_string);
104 while (st.hasMoreTokens())
105 {
106 String driver_classname = st.nextToken();
107
108 try
109 {
110 Class.forName(driver_classname); // The driver registers itself
111 }
112 catch (Exception e) { ; } // Ignore not founds
113 }
114 }
115
116}
117
118/*************************************************************************/
119
120/*
121 * Class Methods
122 */
123
124/**
125 * This method returns the login timeout in use by JDBC drivers systemwide.
126 *
127 * @return The login timeout.
128 */
129public static int
130getLoginTimeout()
131{
132 return(login_timeout);
133}
134
135/*************************************************************************/
136
137/**
138 * This method set the login timeout used by JDBC drivers. This is a
139 * system-wide parameter that applies to all drivers.
140 *
141 * @param login_timeout The new login timeout value.
142 */
143public static void
144setLoginTimeout(int login_timeout)
145{
146 DriverManager.login_timeout = login_timeout;
147}
148
149/*************************************************************************/
150
151/**
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.
155 *
156 * @return The log writer in use by JDBC drivers.
157 */
158public static PrintWriter
159getLogWriter()
160{
161 return(log_writer);
162}
163
164/*************************************************************************/
165
166/**
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.
172 *
173 * @param log_writer The new log writer for JDBC.
174 */
175public static void
176setLogWriter(PrintWriter log_writer)
177{
178 DriverManager.log_writer = log_writer;
179}
180
181/*************************************************************************/
182
183/**
184 * This method returns the log stream in use by JDBC.
185 *
186 * @return The log stream in use by JDBC.
187 *
188 * @deprecated Use <code>getLogWriter()</code> instead.
189 */
190public static PrintStream
191getLogStream()
192{
193 return(log_stream);
194}
195
196/*************************************************************************/
197
198/**
199 * This method sets the log stream in use by JDBC.
200 *
201 * @param log_stream The log stream in use by JDBC.
202 *
203 * @deprecated Use <code>setLogWriter</code> instead.
204 */
205public static void
206setLogStream(PrintStream log_stream)
207{
208 DriverManager.log_stream = log_stream;
209}
210
211/*************************************************************************/
212
213/**
214 * This method prints the specified line to the log stream.
215 *
216 * @param str The string to write to the log stream.
217 */
218public static void
219println(String str)
220{
221 if (log_stream != null) // Watch for user not using logging
222 log_stream.println(str);
223}
224
225/*************************************************************************/
226
227/**
228 * This method registers a new driver with the manager. This is normally
229 * called by the driver itself in a static initializer.
230 *
231 * @param driver The new <code>Driver</code> to add.
232 *
233 * @exception SQLException If an error occurs.
234 */
235public static void
236registerDriver(Driver driver) throws SQLException
237{
238 if (!drivers.contains(driver))
239 drivers.addElement(driver);
240}
241
242/*************************************************************************/
243
244/**
245 * This method de-registers a driver from the manager.
246 *
247 * @param driver The <code>Driver</code> to unregister.
248 *
249 * @exception SQLException If an error occurs.
250 */
251public static void
252deregisterDriver(Driver driver) throws SQLException
253{
254 if (drivers.contains(driver))
255 drivers.removeElement(driver);
256}
257
258/*************************************************************************/
259
260/**
261 * This method returns a list of all the currently registered JDBC drivers
262 * that were loaded by the current <code>ClassLoader</code>.
263 *
264 * @return An <code>Enumeration</code> of all currently loaded JDBC drivers.
265 */
266public static Enumeration
267getDrivers()
268{
269 Vector v = new Vector();
270 Enumeration e = drivers.elements();
271
272 // Is this right?
273 ClassLoader cl = Thread.currentThread().getContextClassLoader();
274
275 while(e.hasMoreElements())
276 {
277 Object obj = e.nextElement();
278
279 ClassLoader loader = obj.getClass().getClassLoader();
280
281 if (loader == null)
282 loader = ClassLoader.getSystemClassLoader();
283 if (!loader.equals(cl))
284 continue;
285
286 v.addElement(obj);
287 }
288
289 return(v.elements());
290}
291
292/*************************************************************************/
293
294/**
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.
299 *
300 * @param url The JDBC URL string to find a driver for.
301 *
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.
304 *
305 * @exception SQLException If an error occurs.
306 */
307public static Driver
308getDriver(String url) throws SQLException
309{
310 // FIXME: Limit driver search to the appropriate subset of loaded drivers.
311
312 Enumeration e = drivers.elements();
313 while(e.hasMoreElements())
314 {
315 Driver d = (Driver)e.nextElement();
316 if (d.acceptsURL(url))
317 return(d);
318 }
319
320 return(null);
321}
322
323/*************************************************************************/
324
325/**
326 * This method attempts to return a connection to the specified
327 * JDBC URL string.
328 *
329 * @param url The JDBC URL string to connect to.
330 *
331 * @return A <code>Connection</code> to that URL.
332 *
333 * @exception SQLException If an error occurs.
334 */
335public static Connection
336getConnection(String url) throws SQLException
337{
338 return(getConnection(url, new Properties()));
339}
340
341/*************************************************************************/
342
343/**
344 * This method attempts to return a connection to the specified
345 * JDBC URL string using the specified username and password.
346 *
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.
350 *
351 * @return A <code>Connection</code> to that URL.
352 *
353 * @exception SQLException If an error occurs.
354 */
355public static Connection
356getConnection(String url, String user, String password) throws SQLException
357{
358 Properties p = new Properties();
359
360 if (user != null)
361 p.setProperty("user", user);
362 if (password != null)
363 p.setProperty("password", password);
364
365 return(getConnection(url, p));
366}
367
368/*************************************************************************/
369
370/**
371 * This method attempts to return a connection to the specified
372 * JDBC URL string using the specified connection properties.
373 *
374 * @param url The JDBC URL string to connect to.
375 * @param properties The connection properties.
376 *
377 * @return A <code>Connection</code> to that URL.
378 *
379 * @exception SQLException If an error occurs.
380 */
381public static Connection
382getConnection(String url, Properties properties) throws SQLException
383{
384 Driver d = getDriver(url);
385 if (d == null)
386 throw new SQLException("Driver not found for URL: " + url);
387
388 return(d.connect(url, properties));
389}
390
391/*************************************************************************/
392
393/*
394 * Constructors
395 */
396
397// Keep bozos from trying to instantiate us.
398private
399DriverManager()
400{
401 ;
402}
403
404} // class DriverManager
405
Note: See TracBrowser for help on using the repository browser.