Class Driver
- java.lang.Object
-
- org.postgresql.Driver
-
- All Implemented Interfaces:
java.sql.Driver
public class Driver extends java.lang.Object implements java.sql.Driver
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:
PGConnection
,Driver
-
-
Nested Class Summary
Nested Classes Modifier and Type Class 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
Fields Modifier and Type Field Description private static java.lang.String
DEFAULT_PORT
private java.util.Properties
defaultProperties
private static java.util.logging.Logger
LOGGER
private static java.util.logging.Logger
PARENT_LOGGER
private static Driver
registeredDriver
private static SharedTimer
SHARED_TIMER
-
Constructor Summary
Constructors Constructor Description Driver()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
acceptsURL(java.lang.String url)
Returns true if the driver thinks it can open a connection to the given URL.java.sql.Connection
connect(java.lang.String url, java.util.Properties info)
Try to make a database connection to the given URL.private static java.lang.String
database(java.util.Properties props)
static void
deregister()
According to JDBC specification, this driver is registered againstDriverManager
when the class is loaded.private java.util.Properties
getDefaultProperties()
int
getMajorVersion()
int
getMinorVersion()
java.util.logging.Logger
getParentLogger()
java.sql.DriverPropertyInfo[]
getPropertyInfo(java.lang.String url, java.util.Properties info)
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.static SharedTimer
getSharedTimer()
static java.lang.String
getVersion()
Deprecated.usegetMajorVersion()
andgetMinorVersion()
insteadprivate static HostSpec[]
hostSpecs(java.util.Properties props)
static boolean
isRegistered()
boolean
jdbcCompliant()
Report whether the driver is a genuine JDBC compliant driver.private java.util.Properties
loadDefaultProperties()
private static java.sql.Connection
makeConnection(java.lang.String url, java.util.Properties props)
Create a connection from URL and properties.static java.sql.SQLFeatureNotSupportedException
notImplemented(java.lang.Class<?> callClass, java.lang.String functionName)
This method was added in v6.5, and simply throws an SQLException for an unimplemented method.static java.util.Properties
parseURL(java.lang.String url, java.util.Properties defaults)
Constructs a new DriverURL, splitting the specified URL into its component parts.static void
register()
Register the driver againstDriverManager
.private void
setupLoggerFromProperties(java.util.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 issueprivate static long
timeout(java.util.Properties props)
private static java.lang.String
user(java.util.Properties props)
-
-
-
Field Detail
-
registeredDriver
private static Driver registeredDriver
-
PARENT_LOGGER
private static final java.util.logging.Logger PARENT_LOGGER
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
SHARED_TIMER
private static final SharedTimer SHARED_TIMER
-
DEFAULT_PORT
private static final java.lang.String DEFAULT_PORT
- See Also:
- Constant Field Values
-
defaultProperties
private java.util.Properties defaultProperties
-
-
Method Detail
-
getDefaultProperties
private java.util.Properties getDefaultProperties() throws java.io.IOException
- Throws:
java.io.IOException
-
loadDefaultProperties
private java.util.Properties loadDefaultProperties() throws java.io.IOException
- Throws:
java.io.IOException
-
connect
public java.sql.Connection connect(java.lang.String url, java.util.Properties info) throws java.sql.SQLException
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&...
- Specified by:
connect
in interfacejava.sql.Driver
- Parameters:
url
- the URL of the database to connect toinfo
- a list of arbitrary tag/value pairs as connection arguments- Returns:
- a connection to the URL or null if it isnt us
- Throws:
java.sql.SQLException
- if a database access error occurs or the url isnull
- See Also:
Driver.connect(java.lang.String, java.util.Properties)
-
setupLoggerFromProperties
private void setupLoggerFromProperties(java.util.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
- Connection Properties
-
makeConnection
private static java.sql.Connection makeConnection(java.lang.String url, java.util.Properties props) throws java.sql.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
- the original URLprops
- the parsed/defaulted connection properties- Returns:
- a new connection
- Throws:
java.sql.SQLException
- if the connection could not be made
-
acceptsURL
public boolean acceptsURL(java.lang.String url)
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:- Specified by:
acceptsURL
in interfacejava.sql.Driver
- Parameters:
url
- the URL of the driver- Returns:
- true if this driver accepts the given URL
- See Also:
Driver.acceptsURL(java.lang.String)
-
getPropertyInfo
public java.sql.DriverPropertyInfo[] getPropertyInfo(java.lang.String url, java.util.Properties info)
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
- Specified by:
getPropertyInfo
in interfacejava.sql.Driver
- Parameters:
url
- the Url of the database to connect toinfo
- a proposed list of tag/value pairs that will be sent on connect open.- Returns:
- An array of DriverPropertyInfo objects describing possible properties. This array may be an empty array if no properties are required
- See Also:
Driver.getPropertyInfo(java.lang.String, java.util.Properties)
-
getMajorVersion
public int getMajorVersion()
- Specified by:
getMajorVersion
in interfacejava.sql.Driver
-
getMinorVersion
public int getMinorVersion()
- Specified by:
getMinorVersion
in interfacejava.sql.Driver
-
getVersion
@Deprecated public static java.lang.String getVersion()
Deprecated.usegetMajorVersion()
andgetMinorVersion()
insteadReturns the server version series of this driver and the specific build number.- Returns:
- JDBC driver version
-
jdbcCompliant
public boolean 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).
- Specified by:
jdbcCompliant
in interfacejava.sql.Driver
-
parseURL
public static java.util.Properties parseURL(java.lang.String url, java.util.Properties defaults)
Constructs a new DriverURL, splitting the specified URL into its component parts.- Parameters:
url
- JDBC URL to parsedefaults
- Default properties- Returns:
- Properties with elements added from the url
-
hostSpecs
private static HostSpec[] hostSpecs(java.util.Properties props)
- Returns:
- the address portion of the URL
-
user
private static java.lang.String user(java.util.Properties props)
- Returns:
- the username of the URL
-
database
private static java.lang.String database(java.util.Properties props)
- Returns:
- the database name of the URL
-
timeout
private static long timeout(java.util.Properties props)
- Returns:
- the timeout from the URL, in milliseconds
-
notImplemented
public static java.sql.SQLFeatureNotSupportedException notImplemented(java.lang.Class<?> callClass, java.lang.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 strace- Parameters:
callClass
- the call ClassfunctionName
- the name of the unimplemented function with the type of its arguments- Returns:
- PSQLException with a localized message giving the complete description of the unimplemeted function
-
getParentLogger
public java.util.logging.Logger getParentLogger()
- Specified by:
getParentLogger
in interfacejava.sql.Driver
-
getSharedTimer
public static SharedTimer getSharedTimer()
-
register
public static void register() throws java.sql.SQLException
Register the driver againstDriverManager
. This is done automatically when the class is loaded. Dropping the driver from DriverManager's list is possible usingderegister()
method.- Throws:
java.lang.IllegalStateException
- if the driver is already registeredjava.sql.SQLException
- if registering the driver fails
-
deregister
public static void deregister() throws java.sql.SQLException
According to JDBC specification, this driver is registered againstDriverManager
when the class is loaded. To avoid leaks, this method allow unregistering the driver so that the class can be gc'ed if necessary.- Throws:
java.lang.IllegalStateException
- if the driver is not registeredjava.sql.SQLException
- if deregistering the driver fails
-
isRegistered
public static boolean isRegistered()
- Returns:
true
if the driver is registered againstDriverManager
-
-