Java : Driver (JDBC) with Examples

Driver (Java SE 19 & JDK 19) API Examples.
You will find code examples on most Driver methods.


Summary

The interface that every driver class must implement.

Class diagram

final var driver = DriverManager.getDriver("jdbc:derby:");

System.out.println("-- driver --");
System.out.println("package : " + driver.getClass().getPackage().getName());
System.out.println("major version : " + driver.getMajorVersion());
System.out.println("minor version : " + driver.getMinorVersion());

// Result
// ↓
//-- driver --
//package : org.apache.derby.iapi.jdbc
//major version : 10
//minor version : 16

Note :

  • The code examples in this page use Apache Derby as the JDBC driver. Apache Derby was once included in older JDKs, but is not included in the latest JDK.

Methods

boolean acceptsURL (String url)

Retrieves whether the driver thinks that it can open a connection to the given URL.

final var protocol = "jdbc:derby:";
final var driver = DriverManager.getDriver(protocol);

final var path = Path.of("R:", "java-work", "db");
final var url = protocol + path;
System.out.println(url); // jdbc:derby:R:\java-work\db

final var ret = driver.acceptsURL(url);
System.out.println(ret); // true
final var driver = DriverManager.getDriver("jdbc:derby:");

final var ret = driver.acceptsURL("abc");
System.out.println(ret); // false

Connection connect (String url, Properties info)

Attempts to make a database connection to the given URL.

final var protocol = "jdbc:derby:";
final var driver = DriverManager.getDriver(protocol);

final var path = Path.of("R:", "java-work", "db");
final var url = protocol + path + ";create=true";
System.out.println(url); // jdbc:derby:R:\java-work\db;create=true

try (final var connection = driver.connect(url, new Properties())) {
    final var meta = connection.getMetaData();
    System.out.println(meta.getDriverName()); // Apache Derby Embedded JDBC Driver
    System.out.println(meta.getDriverVersion()); // 10.16.1.1 - (1901046)
}

int getMajorVersion ()

Retrieves the driver's major version number.

final var driver = DriverManager.getDriver("jdbc:derby:");
System.out.println(driver.getMajorVersion()); // 10
System.out.println(driver.getMinorVersion()); // 16

int getMinorVersion ()

Gets the driver's minor version number.

Please see getMajorVersion().

Logger getParentLogger ()

Return the parent Logger of all the Loggers used by this driver.

// Apache Derby doesn't seem to support the Logger.
try {
    final var driver = DriverManager.getDriver("jdbc:derby:");
    final var logger = driver.getParentLogger();
} catch (SQLFeatureNotSupportedException e) {
    System.out.println(e);
}

// Result
// ↓
//java.sql.SQLFeatureNotSupportedException: Feature not implemented: getParentLogger().

DriverPropertyInfo[] getPropertyInfo (String url, Properties info)

Gets information about the possible properties for this driver.

final var protocol = "jdbc:derby:";
final var driver = DriverManager.getDriver(protocol);

final var values = driver.getPropertyInfo(protocol, new Properties());
for (final var value : values) {
    System.out.println(value.name + " : " + value.description);
}

// Result
// ↓
//databaseName : Database identity
//encryptionProvider : Cryptographic service provider
//encryptionAlgorithm : Cryptographic algorithm
//encryptionKeyLength : Cryptographic key length
//encryptionKey : External cryptographic key
//territory : Locale for the database
//collation : Collation for character datatypes
//user : User name
//logDevice : Log directory path
//rollForwardRecoveryFrom : Backup path for roll-forward recovery
//createFrom : Backup path for creating database from backup
//restoreFrom : Backup path for restoring database from backup
//bootPassword : Secret cryptographic key
//password : User password
//shutdown : Shut down Derby
//deregister : Deregister AutoloadedDriver
//create : Create database
//dataEncryption : Encrypt database on disk
//upgrade : Upgrade database
final var protocol = "jdbc:derby:";
final var driver = DriverManager.getDriver(protocol);

final var info = new Properties();
info.setProperty("shutdown", "true");

final var values = driver.getPropertyInfo(protocol, info);
System.out.println(values.length); // 0

boolean jdbcCompliant ()

Reports whether this driver is a genuine JDBC Compliant driver.

final var driver = DriverManager.getDriver("jdbc:derby:");

final var ret = driver.jdbcCompliant();
System.out.println(ret); // true

To top of page