2

I'm having trouble to find an example of usage of JDBC 4.2 new interfaces DriverAction and SQLType.

Can anyone give me an example?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154

1 Answers1

4

DriverAction

DriverAction is only relevant for driver implementations, if that driver wants to be informed if the driver is deregistered (eg to release resources or to perform other clean up actions), as an end-user of JDBC you have nothing to do with it.

Its purpose is, as described in the documentation:

An interface that must be implemented when a Driver wants to be notified by DriverManager.

A DriverAction implementation is not intended to be used directly by applications. A JDBC Driver may choose to create its DriverAction implementation in a private class to avoid it being called directly.

(emphasis mine)

SQLType

To understand SQLType you first need to look at the enum JDBCType. This is an enum that declares the same JDBC types as in java.sql.Types, but then as enum objects. This enum implements SQLType. The addition of the interface SQLType allows driver implementations to add similar type safe enums for driver or database specific types that are not defined in JDBC.

It is used - for example - in PreparedStatement.setObject(int, Object, SQLType) as an equivalent to PreparedStatement.setObject(int, Object, int).

What does this add you may ask? First of all it is better for type safety. In the past drivers allocated java.sql.Types-like integers for database specific types that might conflict with type codes added in newer JDBC versions. Second it might allow for more readable diagnostics when you provide an unsupported type (eg instead of "Unsupported type -1233" it could display "Unsupported type XYZ (vendor : MyXYZDB)" using the getName and getVendor) methods, and finally (also a form of type safety) it prevents users from incorrectly using the type codes of database brand X in database brand Y (or at least: allow the driver to detect this incorrect use).

That said: I think that for most basic and regular usages of JDBC this interface does not add much.

Community
  • 1
  • 1
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197