001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2;
007:
008: import java.sql.Connection;
009: import java.sql.DriverManager;
010: import java.sql.DriverPropertyInfo;
011: import java.sql.SQLException;
012: import java.util.Properties;
013:
014: import org.h2.engine.Constants;
015: import org.h2.jdbc.JdbcConnection;
016: import org.h2.message.Message;
017: import org.h2.message.TraceSystem;
018:
019: /**
020: * The database driver. An application should not use this class directly. The
021: * only thing the application needs to do is load the driver. This can be done
022: * using Class.forName. To load the driver and open a database connection, use
023: * the following code:
024: *
025: * <pre>
026: * Class.forName("org.h2.Driver");
027: * Connection conn = DriverManager.getConnection(
028: * "jdbc:h2:˜/test", "sa", "sa");
029: * </pre>
030: */
031: public class Driver implements java.sql.Driver {
032:
033: private static final Driver INSTANCE = new Driver();
034:
035: static {
036: try {
037: DriverManager.registerDriver(INSTANCE);
038: } catch (SQLException e) {
039: TraceSystem.traceThrowable(e);
040: }
041: }
042:
043: /**
044: * This method should not be called by an application.
045: *
046: * @return the new connection
047: */
048: public Connection connect(String url, Properties info)
049: throws SQLException {
050: try {
051: if (info == null) {
052: info = new Properties();
053: }
054: if (!acceptsURL(url)) {
055: return null;
056: }
057: synchronized (this ) {
058: return new JdbcConnection(url, info);
059: }
060: } catch (Throwable e) {
061: throw Message.convert(e);
062: }
063: }
064:
065: /**
066: * This method should not be called by an application.
067: *
068: * @return if the driver understands the URL
069: */
070: public boolean acceptsURL(String url) {
071: return url != null && url.startsWith(Constants.START_URL);
072: }
073:
074: /**
075: * This method should not be called by an application.
076: *
077: * @return the major version number
078: */
079: public int getMajorVersion() {
080: return Constants.VERSION_MAJOR;
081: }
082:
083: /**
084: * This method should not be called by an application.
085: *
086: * @return the minor version number
087: */
088: public int getMinorVersion() {
089: return Constants.VERSION_MINOR;
090: }
091:
092: /**
093: * This method should not be called by an application.
094: *
095: * @return a zero length array
096: */
097: public DriverPropertyInfo[] getPropertyInfo(String url,
098: Properties info) {
099: return new DriverPropertyInfo[0];
100: }
101:
102: /**
103: * This method should not be called by an application.
104: *
105: * @return true
106: */
107: public boolean jdbcCompliant() {
108: return true;
109: }
110:
111: /**
112: * INTERNAL
113: */
114: public static Driver load() {
115: return INSTANCE;
116: }
117:
118: }
|