001: /*
002:
003: Derby - Class org.apache.derby.jdbc.EmbeddedDriver
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.jdbc;
023:
024: import java.sql.DriverManager;
025: import java.sql.Driver;
026: import java.sql.Connection;
027: import java.sql.DriverPropertyInfo;
028: import java.sql.SQLException;
029:
030: import java.io.PrintStream;
031: import java.util.Properties;
032:
033: import org.apache.derby.iapi.reference.MessageId;
034: import org.apache.derby.iapi.reference.Attribute;
035: import org.apache.derby.iapi.services.i18n.MessageService;
036: import org.apache.derby.iapi.jdbc.JDBCBoot;
037:
038: /**
039: The embedded JDBC driver (Type 4) for Derby.
040: <P>
041: The driver automatically supports the correct JDBC specification version
042: for the Java Virtual Machine's environment.
043: <UL>
044: <LI> JDBC 4.0 - Java SE 6
045: <LI> JDBC 3.0 - Java 2 - JDK 1.4, J2SE 5.0
046: <LI> JDBC 2.0 - Java 2 - JDK 1.2,1.3
047: </UL>
048:
049: <P>
050: Loading this JDBC driver boots the database engine
051: within the same Java virtual machine.
052: <P>
053: The correct code to load the Derby engine using this driver is
054: (with approriate try/catch blocks):
055: <PRE>
056: Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
057:
058: // or
059:
060: new org.apache.derby.jdbc.EmbeddedDriver();
061:
062:
063: </PRE>
064: When loaded in this way, the class boots the actual JDBC driver indirectly.
065: The JDBC specification recommends the Class.ForName method without the .newInstance()
066: method call, but adding the newInstance() guarantees
067: that Derby will be booted on any Java Virtual Machine.
068:
069: <P>
070: Note that you do not need to manually load the driver this way if you are
071: running on Jave SE 6 or later. In that environment, the driver will be
072: automatically loaded for you when your application requests a connection to
073: a Derby database.
074: <P>
075: Any initial error messages are placed in the PrintStream
076: supplied by the DriverManager. If the PrintStream is null error messages are
077: sent to System.err. Once the Derby engine has set up an error
078: logging facility (by default to derby.log) all subsequent messages are sent to it.
079: <P>
080: By convention, the class used in the Class.forName() method to
081: boot a JDBC driver implements java.sql.Driver.
082:
083: This class is not the actual JDBC driver that gets registered with
084: the Driver Manager. It proxies requests to the registered Derby JDBC driver.
085:
086: @see java.sql.DriverManager
087: @see java.sql.DriverManager#getLogStream
088: @see java.sql.Driver
089: @see java.sql.SQLException
090: */
091:
092: public class EmbeddedDriver implements Driver {
093:
094: static {
095:
096: EmbeddedDriver.boot();
097: }
098:
099: private AutoloadedDriver _autoloadedDriver;
100:
101: // Boot from the constructor as well to ensure that
102: // Class.forName(...).newInstance() reboots Derby
103: // after a shutdown inside the same JVM.
104: public EmbeddedDriver() {
105: EmbeddedDriver.boot();
106: }
107:
108: /*
109: ** Methods from java.sql.Driver.
110: */
111: /**
112: Accept anything that starts with <CODE>jdbc:derby:</CODE>.
113: @exception SQLException if a database-access error occurs.
114: @see java.sql.Driver
115: */
116: public boolean acceptsURL(String url) throws SQLException {
117: return getDriverModule().acceptsURL(url);
118: }
119:
120: /**
121: Connect to the URL if possible
122: @exception SQLException illegal url or problem with connectiong
123: @see java.sql.Driver
124: */
125: public Connection connect(String url, Properties info)
126: throws SQLException {
127: return getDriverModule().connect(url, info);
128: }
129:
130: /**
131: * Returns an array of DriverPropertyInfo objects describing possible properties.
132: @exception SQLException if a database-access error occurs.
133: @see java.sql.Driver
134: */
135: public DriverPropertyInfo[] getPropertyInfo(String url,
136: Properties info) throws SQLException {
137: return getDriverModule().getPropertyInfo(url, info);
138: }
139:
140: /**
141: * Returns the driver's major version number.
142: @see java.sql.Driver
143: */
144: public int getMajorVersion() {
145: try {
146: return (getDriverModule().getMajorVersion());
147: } catch (SQLException se) {
148: return 0;
149: }
150: }
151:
152: /**
153: * Returns the driver's minor version number.
154: @see java.sql.Driver
155: */
156: public int getMinorVersion() {
157: try {
158: return (getDriverModule().getMinorVersion());
159: } catch (SQLException se) {
160: return 0;
161: }
162: }
163:
164: /**
165: * Report whether the Driver is a genuine JDBC COMPLIANT (tm) driver.
166: @see java.sql.Driver
167: */
168: public boolean jdbcCompliant() {
169: try {
170: return (getDriverModule().jdbcCompliant());
171: } catch (SQLException se) {
172: return false;
173: }
174: }
175:
176: /**
177: * Lookup the booted driver module appropriate to our JDBC level.
178: */
179: private Driver getDriverModule() throws SQLException {
180: return AutoloadedDriver.getDriverModule();
181: }
182:
183: /*
184: ** Find the appropriate driver for our JDBC level and boot it.
185: * This is package protected so that AutoloadedDriver can call it.
186: */
187: static void boot() {
188: PrintStream ps = DriverManager.getLogStream();
189:
190: if (ps == null)
191: ps = System.err;
192:
193: new JDBCBoot().boot(Attribute.PROTOCOL, ps);
194: }
195:
196: }
|