KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > jdbc > EmbeddedDriver


1 /*
2
3    Derby - Class org.apache.derby.jdbc.EmbeddedDriver
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.jdbc;
23
24 import java.sql.DriverManager JavaDoc;
25 import java.sql.Driver JavaDoc;
26 import java.sql.Connection JavaDoc;
27 import java.sql.DriverPropertyInfo JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import java.io.PrintStream JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import org.apache.derby.iapi.reference.MessageId;
34 import org.apache.derby.iapi.reference.Attribute;
35 import org.apache.derby.iapi.services.i18n.MessageService;
36 import org.apache.derby.iapi.jdbc.JDBCBoot;
37
38
39 /**
40     The embedded JDBC driver (Type 4) for Derby.
41     <P>
42     The driver automatically supports the correct JDBC specification version
43     for the Java Virtual Machine's environment.
44     <UL>
45     <LI> JDBC 4.0 - Java SE 6
46     <LI> JDBC 3.0 - Java 2 - JDK 1.4, J2SE 5.0
47     <LI> JDBC 2.0 - Java 2 - JDK 1.2,1.3
48     </UL>
49
50     <P>
51     Loading this JDBC driver boots the database engine
52     within the same Java virtual machine.
53     <P>
54     The correct code to load the Derby engine using this driver is
55     (with approriate try/catch blocks):
56      <PRE>
57      Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
58
59      // or
60
61      new org.apache.derby.jdbc.EmbeddedDriver();
62
63     
64     </PRE>
65     When loaded in this way, the class boots the actual JDBC driver indirectly.
66     The JDBC specification recommends the Class.ForName method without the .newInstance()
67     method call, but adding the newInstance() guarantees
68     that Derby will be booted on any Java Virtual Machine.
69
70     <P>
71     Note that you do not need to manually load the driver this way if you are
72     running on Jave SE 6 or later. In that environment, the driver will be
73     automatically loaded for you when your application requests a connection to
74     a Derby database.
75     <P>
76     Any initial error messages are placed in the PrintStream
77     supplied by the DriverManager. If the PrintStream is null error messages are
78     sent to System.err. Once the Derby engine has set up an error
79     logging facility (by default to derby.log) all subsequent messages are sent to it.
80     <P>
81     By convention, the class used in the Class.forName() method to
82     boot a JDBC driver implements java.sql.Driver.
83
84     This class is not the actual JDBC driver that gets registered with
85     the Driver Manager. It proxies requests to the registered Derby JDBC driver.
86
87     @see java.sql.DriverManager
88     @see java.sql.DriverManager#getLogStream
89     @see java.sql.Driver
90     @see java.sql.SQLException
91 */

92
93 public class EmbeddedDriver implements Driver JavaDoc {
94
95     static {
96
97         EmbeddedDriver.boot();
98     }
99
100     private AutoloadedDriver _autoloadedDriver;
101     
102     // Boot from the constructor as well to ensure that
103
// Class.forName(...).newInstance() reboots Derby
104
// after a shutdown inside the same JVM.
105
public EmbeddedDriver() {
106         EmbeddedDriver.boot();
107     }
108
109     /*
110     ** Methods from java.sql.Driver.
111     */

112     /**
113         Accept anything that starts with <CODE>jdbc:derby:</CODE>.
114         @exception SQLException if a database-access error occurs.
115     @see java.sql.Driver
116     */

117     public boolean acceptsURL(String JavaDoc url) throws SQLException JavaDoc {
118         return getDriverModule().acceptsURL(url);
119     }
120
121     /**
122         Connect to the URL if possible
123         @exception SQLException illegal url or problem with connectiong
124     @see java.sql.Driver
125   */

126     public Connection JavaDoc connect(String JavaDoc url, Properties JavaDoc info)
127         throws SQLException JavaDoc
128     {
129         return getDriverModule().connect(url, info);
130     }
131
132   /**
133    * Returns an array of DriverPropertyInfo objects describing possible properties.
134     @exception SQLException if a database-access error occurs.
135     @see java.sql.Driver
136    */

137     public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url, Properties JavaDoc info)
138         throws SQLException JavaDoc
139     {
140         return getDriverModule().getPropertyInfo(url, info);
141     }
142
143     /**
144      * Returns the driver's major version number.
145      @see java.sql.Driver
146      */

147     public int getMajorVersion() {
148         try {
149             return (getDriverModule().getMajorVersion());
150         }
151         catch (SQLException JavaDoc se) {
152             return 0;
153         }
154     }
155
156     /**
157      * Returns the driver's minor version number.
158      @see java.sql.Driver
159      */

160     public int getMinorVersion() {
161         try {
162             return (getDriverModule().getMinorVersion());
163         }
164         catch (SQLException JavaDoc se) {
165             return 0;
166         }
167     }
168
169   /**
170    * Report whether the Driver is a genuine JDBC COMPLIANT (tm) driver.
171      @see java.sql.Driver
172    */

173     public boolean jdbcCompliant() {
174         try {
175             return (getDriverModule().jdbcCompliant());
176         }
177         catch (SQLException JavaDoc se) {
178             return false;
179         }
180     }
181
182   /**
183    * Lookup the booted driver module appropriate to our JDBC level.
184    */

185     private Driver JavaDoc getDriverModule()
186         throws SQLException JavaDoc
187     {
188         return AutoloadedDriver.getDriverModule();
189     }
190
191
192    /*
193     ** Find the appropriate driver for our JDBC level and boot it.
194     * This is package protected so that AutoloadedDriver can call it.
195     */

196     static void boot() {
197         PrintStream JavaDoc ps = DriverManager.getLogStream();
198
199         if (ps == null)
200             ps = System.err;
201
202         new JDBCBoot().boot(Attribute.PROTOCOL, ps);
203     }
204
205
206     
207 }
208
Popular Tags