Top Description Inners Fields Constructors Methods
org.postgresql

public Class Driver

extends Object
implements Driver
Class Inheritance
All Implemented Interfaces
java.sql.Driver
Static Imports
org.postgresql.util.internal.Nullness.castNonNull

The Java SQL framework allows for multiple database drivers. Each driver should supply a class that implements the Driver interface

The DriverManager will try to load as many drivers as it can find and then for any given connection request, it will ask each driver in turn to try to connect to the target URL.

It is strongly recommended that each Driver class should be small and standalone so that the Driver class can be loaded and queried without bringing in vast quantities of supporting code.

When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager. This means that a user can load and register a driver by doing Class.forName("foo.bah.Driver")

See Also
org.postgresql.PGConnection, java.sql.Driver

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Driver.ConnectThread

Perform a connect in a separate thread; supports getting the results from the original thread while enforcing a login timeout.

Field Summary

Modifier and TypeField and Description
private @Nullable Properties
private final ResourceLock
private static final Logger
private static final Logger
private static @Nullable Driver
private static final SharedTimer

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if this driver accepts the given URL
acceptsURL
(String
the URL of the driver
url
)

Implements java.sql.Driver.acceptsURL.

Returns true if the driver thinks it can open a connection to the given URL.

public @Nullable Connection

Returns:

a connection to the URL or null if it isnt us
connect
(String
the URL of the database to connect to
url
,
@Nullable Properties
a list of arbitrary tag/value pairs as connection arguments
info
)

Implements java.sql.Driver.connect.

Try to make a database connection to the given URL.

public static void
deregister()

According to JDBC specification, this driver is registered against DriverManager when the class is loaded.

private static <T> T
private Properties
public int
getMajorVersion()

Implements java.sql.Driver.getMajorVersion.

Retrieves the driver's major version number.

public int
getMinorVersion()

Implements java.sql.Driver.getMinorVersion.

Gets the driver's minor version number.

public Logger
getParentLogger()

Implements java.sql.Driver.getParentLogger.

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

public DriverPropertyInfo[]

Returns:

An array of DriverPropertyInfo objects describing possible properties. This array may be an empty array if no properties are required
getPropertyInfo
(String
the Url of the database to connect to
url
,
Properties
a proposed list of tag/value pairs that will be sent on connect open.
info
)

Implements java.sql.Driver.getPropertyInfo.

The getPropertyInfo method is intended to allow a generic GUI tool to discover what properties it should prompt a human for in order to get enough information to connect to a database.

public static SharedTimer
public static String

Returns:

JDBC driver version
getVersion
()
Deprecated use getMajorVersion() and getMinorVersion() instead

Returns the server version series of this driver and the specific build number.

private static HostSpec[]

Returns:

the address portion of the URL
hostSpecs
(Properties props)

public static boolean

Returns:

true if the driver is registered against DriverManager
isRegistered
()

public boolean
jdbcCompliant()

Implements java.sql.Driver.jdbcCompliant.

Report whether the driver is a genuine JDBC compliant driver.

private Properties
private static Connection

Returns:

a new connection
makeConnection
(String
the original URL
url
,
Properties
the parsed/defaulted connection properties
props
)

Create a connection from URL and properties.

public static SQLFeatureNotSupportedException

Returns:

PSQLException with a localized message giving the complete description of the unimplemented function
notImplemented
(Class<?>
the call Class
callClass
,
String
the name of the unimplemented function with the type of its arguments
functionName
)

This method was added in v6.5, and simply throws an SQLException for an unimplemented method.

public static @Nullable Properties

Returns:

Properties with elements added from the url
parseURL
(String
JDBC URL to parse
url
,
@Nullable Properties
Default properties
defaults
)

Constructs a new DriverURL, splitting the specified URL into its component parts.

public static void
register()

Register the driver against DriverManager.

private void
setupLoggerFromProperties(final Properties
Connection Properties
props
)

this is an empty method left here for graalvm we removed the ability to setup the logger from properties due to a security issue

private static long

Returns:

the timeout from the URL, in milliseconds
timeout
(Properties props)

private static @Nullable String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultPropertiesback to summary
private @Nullable Properties defaultProperties
lockback to summary
private final ResourceLock lock
LOGGERback to summary
private static final Logger LOGGER
PARENT_LOGGERback to summary
private static final Logger PARENT_LOGGER
registeredDriverback to summary
private static @Nullable Driver registeredDriver
SHARED_TIMERback to summary
private static final SharedTimer SHARED_TIMER

Constructor Detail

Driverback to summary
public Driver()

Method Detail

acceptsURLback to summary
public boolean acceptsURL(String url)

Implements java.sql.Driver.acceptsURL.

Returns true if the driver thinks it can open a connection to the given URL. Typically, drivers will return true if they understand the subprotocol specified in the URL and false if they don't. Our protocols start with jdbc:postgresql:

Parameters
url:String

the URL of the driver

Returns:boolean

true if this driver accepts the given URL

Annotations
@Override
See Also
java.sql.Driver#acceptsURL
connectback to summary
public @Nullable Connection connect(String url, @Nullable Properties info) throws SQLException

Implements java.sql.Driver.connect.

Try to make a database connection to the given URL. The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL. This will be common, as when the JDBC driverManager is asked to connect to a given URL, it passes the URL to each loaded driver in turn.

The driver should raise an SQLException if it is the right driver to connect to the given URL, but has trouble connecting to the database.

The java.util.Properties argument can be used to pass arbitrary string tag/value pairs as connection arguments.

  • user - (required) The user to connect as
  • password - (optional) The password for the user
  • ssl -(optional) Use SSL when connecting to the server
  • readOnly - (optional) Set connection to read-only by default
  • charSet - (optional) The character set to be used for converting to/from the database to unicode. If multibyte is enabled on the server then the character set of the database is used as the default, otherwise the jvm character encoding is used as the default. This value is only used when connecting to a 7.2 or older server.
  • loglevel - (optional) Enable logging of messages from the driver. The value is an integer from 0 to 2 where: OFF = 0, INFO =1, DEBUG = 2 The output is sent to DriverManager.getPrintWriter() if set, otherwise it is sent to System.out.
  • compatible - (optional) This is used to toggle between different functionality as it changes across different releases of the jdbc driver code. The values here are versions of the jdbc client and not server versions. For example in 7.1 get/setBytes worked on LargeObject values, in 7.2 these methods were changed to work on bytea values. This change in functionality could be disabled by setting the compatible level to be "7.1", in which case the driver will revert to the 7.1 functionality.

Normally, at least "user" and "password" properties should be included in the properties. For a list of supported character encoding , see http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html Note that you will probably want to have set up the Postgres database itself to use the same encoding, with the -E <encoding> argument to createdb.

Our protocol takes the forms:

 jdbc:postgresql://host:port/database?param1=val1&...
Parameters
url:String

the URL of the database to connect to

info:@Nullable Properties

a list of arbitrary tag/value pairs as connection arguments

Returns:@Nullable Connection

a connection to the URL or null if it isnt us

Annotations
@Override
Exceptions
SQLException:
if a database access error occurs or the url is null
See Also
java.sql.Driver#connect
deregisterback to summary
public static void deregister() throws SQLException

According to JDBC specification, this driver is registered against DriverManager when the class is loaded. To avoid leaks, this method allow unregistering the driver so that the class can be gc'ed if necessary.

Exceptions
SQLException:
if deregistering the driver fails
IllegalStateException:
if the driver is not registered
doPrivilegedback to summary
private static <T> T doPrivileged(PrivilegedExceptionAction<T> action) throws Throwable
getDefaultPropertiesback to summary
private Properties getDefaultProperties() throws IOException
getMajorVersionback to summary
public int getMajorVersion()

Implements java.sql.Driver.getMajorVersion.

Doc from java.sql.Driver.getMajorVersion.

Retrieves the driver's major version number. Initially this should be 1.

Returns:int

this driver's major version number

Annotations
@Override
getMinorVersionback to summary
public int getMinorVersion()

Implements java.sql.Driver.getMinorVersion.

Doc from java.sql.Driver.getMinorVersion.

Gets the driver's minor version number. Initially this should be 0.

Returns:int

this driver's minor version number

Annotations
@Override
getParentLoggerback to summary
public Logger getParentLogger()

Implements java.sql.Driver.getParentLogger.

Doc from java.sql.Driver.getParentLogger.

Return the parent Logger of all the Loggers used by this driver. This should be the Logger farthest from the root Logger that is still an ancestor of all of the Loggers used by this driver. Configuring this Logger will affect all of the log messages generated by the driver. In the worst case, this may be the root Logger.

Returns:Logger

the parent Logger for this driver

Annotations
@Override
getPropertyInfoback to summary
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)

Implements java.sql.Driver.getPropertyInfo.

The getPropertyInfo method is intended to allow a generic GUI tool to discover what properties it should prompt a human for in order to get enough information to connect to a database.

Note that depending on the values the human has supplied so far, additional values may become necessary, so it may be necessary to iterate through several calls to getPropertyInfo

Parameters
url:String

the Url of the database to connect to

info:Properties

a proposed list of tag/value pairs that will be sent on connect open.

Returns:DriverPropertyInfo[]

An array of DriverPropertyInfo objects describing possible properties. This array may be an empty array if no properties are required

Annotations
@Override
See Also
java.sql.Driver#getPropertyInfo
getSharedTimerback to summary
public static SharedTimer getSharedTimer()
getVersionback to summary
public static String getVersion()

Deprecated

use getMajorVersion() and getMinorVersion() instead

Returns the server version series of this driver and the specific build number.

Returns:String

JDBC driver version

Annotations
@Deprecated
hostSpecsback to summary
private static HostSpec[] hostSpecs(Properties props)
Returns:HostSpec[]

the address portion of the URL

isRegisteredback to summary
public static boolean isRegistered()
Returns:boolean

true if the driver is registered against DriverManager

jdbcCompliantback to summary
public boolean jdbcCompliant()

Implements java.sql.Driver.jdbcCompliant.

Report whether the driver is a genuine JDBC compliant driver. A driver may only report "true" here if it passes the JDBC compliance tests, otherwise it is required to return false. JDBC compliance requires full support for the JDBC API and full support for SQL 92 Entry Level.

For PostgreSQL, this is not yet possible, as we are not SQL92 compliant (yet).

Returns:boolean

Doc from java.sql.Driver.jdbcCompliant.

true if this driver is JDBC Compliant; false otherwise

Annotations
@Override
loadDefaultPropertiesback to summary
private Properties loadDefaultProperties() throws IOException
makeConnectionback to summary
private static Connection makeConnection(String url, Properties props) throws SQLException

Create a connection from URL and properties. Always does the connection work in the current thread without enforcing a timeout, regardless of any timeout specified in the properties.

Parameters
url:String

the original URL

props:Properties

the parsed/defaulted connection properties

Returns:Connection

a new connection

Exceptions
SQLException:
if the connection could not be made
notImplementedback to summary
public static SQLFeatureNotSupportedException notImplemented(Class<?> callClass, String functionName)

This method was added in v6.5, and simply throws an SQLException for an unimplemented method. I decided to do it this way while implementing the JDBC2 extensions to JDBC, as it should help keep the overall driver size down. It now requires the call Class and the function name to help when the driver is used with closed software that don't report the stack trace

Parameters
callClass:Class<?>

the call Class

functionName:String

the name of the unimplemented function with the type of its arguments

Returns:SQLFeatureNotSupportedException

PSQLException with a localized message giving the complete description of the unimplemented function

parseURLback to summary
public static @Nullable Properties parseURL(String url, @Nullable Properties defaults)

Constructs a new DriverURL, splitting the specified URL into its component parts.

Parameters
url:String

JDBC URL to parse

defaults:@Nullable Properties

Default properties

Returns:@Nullable Properties

Properties with elements added from the url

registerback to summary
public static void register() throws SQLException

Register the driver against DriverManager. This is done automatically when the class is loaded. Dropping the driver from DriverManager's list is possible using deregister() method.

Exceptions
SQLException:
if registering the driver fails
IllegalStateException:
if the driver is already registered
setupLoggerFromPropertiesback to summary
private void setupLoggerFromProperties(final Properties props)

this is an empty method left here for graalvm we removed the ability to setup the logger from properties due to a security issue

Parameters
props:Properties

Connection Properties

timeoutback to summary
private static long timeout(Properties props)
Returns:long

the timeout from the URL, in milliseconds

urlDecodeback to summary
private static @Nullable String urlDecode(String url)
org.postgresql back to summary

private Class Driver.ConnectThread

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable

Perform a connect in a separate thread; supports getting the results from the original thread while enforcing a login timeout.

Field Summary

Modifier and TypeField and Description
private boolean
private final ResourceLock
private final Condition
private final Properties
private @Nullable Connection
private @Nullable Throwable
private final String

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Connection

Returns:

the new connection, if successful
getResult
(long
timeout in milliseconds
timeout
)

Get the connection result from this (assumed running) thread.

public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

abandonedback to summary
private boolean abandoned
lockback to summary
private final ResourceLock lock
lockConditionback to summary
private final Condition lockCondition
propsback to summary
private final Properties props
resultback to summary
private @Nullable Connection result
resultExceptionback to summary
private @Nullable Throwable resultException
urlback to summary
private final String url

Constructor Detail

ConnectThreadback to summary
pack-priv ConnectThread(String url, Properties props)

Method Detail

getResultback to summary
public Connection getResult(long timeout) throws SQLException

Get the connection result from this (assumed running) thread. If the timeout is reached without a result being available, a SQLException is thrown.

Parameters
timeout:long

timeout in milliseconds

Returns:Connection

the new connection, if successful

Exceptions
SQLException:
if a connection error occurs or the timeout is reached
runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override