public class OracleDriver
extends oracle.jdbc.driver.OracleDriver
java.sql.Driver
interface.
The JDBC driver registration is automatically done via the Java Standard Edition
Service Provider mechanism introduced in JDK6
. Oracle JDBC driver
implements this feature and it is automatically registered if the Oracle JDBC driver
jar is present in the classpath.
You can open a connection to the database with the static
getConnection()
method of the java.sql.DriverManager
class. The type of the object returned is java.sql.Connection
.
The following signature takes the URL, username, and password as separate parameters:
getConnection(String URL, String user, String password);
The following signature takes a URL, together with a properties object that specifies username and password (perhaps among other things):
getConnection(String URL, Properties info);
Where the URL
is of the form:
jdbc:oracle:<drivertype>:@<database>
In addition to the URL, use an object of the standard Java
Properties
class as input. For example:
java.util.Properties info = new java.util.Properties();
info.put ("user", "scott");
info.put ("password","tiger");
info.put ("defaultRowPrefetch","15");
getConnection ("jdbc:oracle:thin:@",info);
Oracle JDBC Thin Driver supports the following URL formats to establish a connection with an Oracle Database. Use any of the below format to establish connection to the Oracle Database.
As the name suggests the EZConnect URL is the easiest option to connect to Oracle.
jdbc:oracle:thin:@[[protocol:]//]
host1[,host12,host13][:port1][,host2:port2]/service_name[:server][/instance_name]
[?[parameter-name1=parameter-value1][¶meter-name2=parameter-value2],...]
In the above format, the parts that are enclosed in square brackets are optional. TCP, TCPS and BEQ are supported
protocols. Specifying the protocol is optional and the default value is TCP. The URL must contain
at least one database host name. Multiple host names are separated by a comma. The host name is followed by an
optional port number. If multiple hosts share the same port number then the port number can be specified at the end of
the host names list. The default port value is 1521
.
Multiple ADDRESS
values can be grouped under a ADDRESS_LIST
by separating them with a
semi-colon like below :
dedicated|shared|pooled
.
An EZConnect URL can have an optional list of name-value pairs as parameters after the '?' delimiter.
Name and value are separated by an '=' and multiple properties are separated by an '&'.
The Thin driver creates a TNS URL using the information provided in the EZConnect format. While resolving
to the TNS format, a name-value pair specified in the EZConnect format is resolved to a TNS URL parameter. If a
name-value pair can not be resolved to a TNS URL parameter then it is handled as a connection property.
The list of supported name value pairs are as follows
Parameter Name | Description |
---|---|
ssl_server_dn_match | Specifies if DN matching is to be enforced. Applicable only for TCPS connection and the default is ON. |
ssl_server_cert_dn | Specifies the DN of the database server. |
https_proxy | Specifies proxy host to be used while establishing connection to the Oracle Database. |
https_proxy_port | Specifies proxy port to be used while establishing connection to the Oracle Database. |
enable | The keepalive feature on the supported TCP transports can be enabled for a client by
specifying enable=broken |
failover | To enable or disable connect-time failover for multiple protocol addresses.
Supported values on | off |
load_balance | To enable or disable client load balancing for multiple protocol addresses.
Supported values on | off |
recv_buf_size | To specify, in bytes, the buffer space for receive operations of sessions |
send_buf_size | To specify, in bytes, the buffer space for send operations of sessions. |
sdu | To instruct JavaNet to optimize the transfer rate of data packets being sent across the network with a specified session data unit (SDU) size in bytes. |
source_route | To enable routing through multiple protocol addresses.
Supported values on | off |
retry_count | To specify the number of times an ADDRESS list is traversed before the connection attempt is terminated. The default value is 0. |
retry_delay | To specify the delay in seconds between subsequent retries for a connection.
This parameter works in conjunction with retry_count parameter. |
service_tag | To specify the value of service tag. While establishing connection to the Oracle Database, the configured value is sent as part of connect data. |
pool_connection_class | Use this parameter to explicitly name the connection class for DRCP connection. |
pool_purity | Use this parameter to specify if an application needs a new session that is not
tainted with any prior session state or to reuse a previous session.
Supported values are NEW | SELF . |
Parameter Name | Description |
---|---|
wallet_location | Specifies the location of the wallet file. More Info.. |
ssl_ciphers | Use this to specify the set of cipher suites to be used while SSL protocol negotiation. More Info.. |
connect_timeout | To specify the timeout duration in seconds for a client to establish an Oracle Net session to an Oracle database. More Info.. |
transport_connect_timeout | To specify the transportation timeout duration in seconds for a client to establish an network connection to an Oracle Database. More Info.. |
encryption_client | Specifies the encryption level supported by client. Supported values are accepted | rejected | requested | required
More Info..
|
encryption_types_client | Specifies the list of encryption algorithms supported by client. Supported algorithms are AES128, AES192, AES256, RC4_40, RC4_56, RC4_128, RC4_256,
DES, DES40, 3DES112, 3DES168.
More Info..
|
crypto_checksum_client | Specifies the data integrity level supported by client.
Supported values are accepted | rejected | requested | required
More Info..
|
crypto_checksum_types_client | Specifies the list of data integrity algorithms supported by client.
Supported algorithms are SHA256, SHA384, SHA512, SHA1, MD5
More Info..
|
token_auth | Configures OracleConnection.CONNECTION_PROPERTY_TOKEN_AUTHENTICATION |
token_location | Configures OracleConnection.CONNECTION_PROPERTY_TOKEN_LOCATION |
password_auth | Configures OracleConnection.CONNECTION_PROPERTY_PASSWORD_AUTHENTICATION |
oci_iam_url | Configures OracleConnection.CONNECTION_PROPERTY_OCI_IAM_URL |
oci_tenancy | Configures OracleConnection.CONNECTION_PROPERTY_OCI_TENANCY |
oci_compartment | Configures OracleConnection.CONNECTION_PROPERTY_OCI_COMPARTMENT |
oci_database | Configures OracleConnection.CONNECTION_PROPERTY_OCI_DATABASE |
oci_config_file | Configures OracleConnection.CONNECTION_PROPERTY_OCI_CONFIG_FILE |
oci_profile | Configures OracleConnection.CONNECTION_PROPERTY_OCI_PROFILE |
azure_db_app_id_uri | Configures OracleConnection.CONNECTION_PROPERTY_AZURE_DB_APP_ID_URI |
tenant_id | Configures OracleConnection.CONNECTION_PROPERTY_TENANT_ID |
client_id | Configures OracleConnection.CONNECTION_PROPERTY_CLIENT_ID |
client_certificate | Configures OracleConnection.CONNECTION_PROPERTY_CLIENT_CERTIFICATE |
redirect_uri | Configures OracleConnection.CONNECTION_PROPERTY_REDIRECT_URI |
Other connection properties specified in oracle.jdbc.OracleConnection
can also be configured
through EZConnect format. Please use the value of the connection property constant name declared in
oracle.jdbc.OracleConnection for configuring the property.
Connection property constant names start with CONNECTION_PROPERTY_
.
Starting in 23ai, the JDBC thin driver supports the Bequeath protocol (BEQ). The Bequeath protocol should only be used when there is no network listener available. This support is limited to Linux platforms. In order to connect, the driver must locate the Oracle server process executable. This is done according to the value of ORACLE_HOME environment variable. There are two ways to provide a value to the ORACLE_HOME variable, either in the url (see second example below) or the environment of the current application. The ORACLE_HOME usually points to the database installation (ex /var/lib/oracle/dbhome). The second mandatory variable is the database SID. similarly to ORACLE_HOME, ORACLE_SID must be provided in the url or the current environment. To establish a connection, BEQ must be enabled in the authentication services property (CONNECTION_PROPERTY_THIN_NET_AUTHENTICATION_SERVICES). When the BEQ protocol is used, the BEQ authentication service is also automatically enabled.
jdbc:oracle:thin:@(DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
jdbc:oracle:thin:@(DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq))(ENVS=ORACLE_HOME=/var/lib/oracle/dbhome,ORACLE_SID=oraclesid))
This is a descriptive format. Each section is enclosed in parenthesis. Please refer to the Oracle Net Services documentation to know more about the TNS formats and the details of all the supported parameters.
jdbc:oracle:thin:@(DESCRIPTION= (ADDRESS=(protocol_address_information)) (CONNECT_DATA= (SERVICE_NAME=service_name)))
NONE
, INBAND_DOWN
, LOCAL
and SOCKET
values of OracleConnection.ConnectionValidation
are not supported.
jdbc:oracle:thin:@(DESCRIPTION= (ADDRESS=(PROTOCOL=tcp)(HOST=sales-svr)(PORT=1521)) (CONNECT_DATA=(SERVICE_NAME=sales.us.example.com)))
jdbc:oracle:thin:@(DESCRIPTION= (ADDRESS_LIST= (ADDRESS=(PROTOCOL=tcp)(HOST=sales1-svr)(PORT=1521)) (ADDRESS=(PROTOCOL=tcp)(HOST=sales2-svr)(PORT=1521))) (CONNECT_DATA=(SERVICE_NAME=sales.us.example.com)))
jdbc:oracle:thin:@(DESCRIPTION= (LOAD_BALANCE=on) (FAILOVER=on) (ADDRESS_LIST= (SOURCE_ROUTE=yes) (ADDRESS=(PROTOCOL=tcp)(HOST=host1)(PORT=1630)) (ADDRESS=(PROTOCOL=tcp)(HOST=host2)(PORT=1521))) (ADDRESS_LIST= (SOURCE_ROUTE=yes) (ADDRESS=(PROTOCOL=tcp)(HOST=host3)(port=1630)) (ADDRESS=(PROTOCOL=tcp)(HOST=host4)(port=1521))) (CONNECT_DATA=(SERVICE_NAME=sales.us.example.com)))
jdbc:oracle:thin:@(DESCRIPTION=(LOCAL=YES)(ADDRESS=(PROTOCOL=beq)))
An alias declared in a tnsnames.ora file present in the TNS ADMIN directory can also be used
in the URL. At runtime the JDBC Thin Driver resolves the specified alias by referring to the
tnsnames.ora file. The TNS ADMIN directory can be specified through the URL or through the
connection property oracle.net.tns_admin
. Any environmental variables in the
specified TNS ADMIN path will be resolved automatically.
jdbc:oracle:thin:@<alias-name>
jdbc:oracle:thin:@<alias-name>?TNS_ADMIN=/work/tnsadmin/
jdbc:oracle:thin:@inst1
jdbc:oracle:thin:@inst1?TNS_ADMIN=/work/tnsadmin/
jdbc:oracle:thin:@inst1?TNS_ADMIN=$ORACLE_HOME/tnsadmin/
jdbc:oracle:thin:@inst1?TNS_ADMIN=%ORACLE_HOME%/tnsadmin/
The JDBC Thin driver can retrieve the TNS URL from an LDAP server. Use the following format for
configuring the LDAP server details. JDBC Thin Driver uses the value of the orclnetdescstring
attribute under the specified DN. If the LDAP connection is established using TCPS (ldaps) connection then
the wallet / keystore
properties can be specified through connection properties. Please see
the LDAP connection properties in the below table. Optionally you can configure LDAP authentication
details through connection properties or through wallet secret store.
Property Name | Description |
---|---|
oracle.net.ldap.ssl.walletLocation | Use this property to specify the wallet location. The driver will use this wallet while SSL negotiation with LDAP server. More Info.. |
oracle.net.ldap.ssl.walletPassword | Use this property to specify the password of the wallet file which will be used while SSL negotiation with LDAP Server. More Info.. |
oracle.net.ldap.security.authentication | Specifies the authentication mechanism to be used by the LDAP service provider
in the JDK. Supported values none | simple .
More Info..
|
oracle.net.ldap.security.principal | Use this property to specify the value of the username(DN) which will be used
while authenticating with the LDAP server. This property can also be configured
via wallet secret store entry oracle.ldap.client.dn
More Info..
|
oracle.net.ldap.security.credentials | Use this property to configure the password which will be used while authenticating
with the LDAP server. This property can also be configured
via wallet secret store entry oracle.ldap.client.password
More Info..
|
jdbc:oracle:thin:@ldap://<ldap-host>:<ldap-port>/<dn>
jdbc:oracle:thin:@ldaps://<ldap-host>:<ldap-port>/<dn>
jdbc:oracle:thin:@ldap://myldapserver:2636/cn=orcl,cn=OracleContext,dc=example,dc=com
jdbc:oracle:thin:@ldaps://myldapserver:1636/cn=orcl,cn=OracleContext,dc=example,dc=com
A Service Provider URL is defined such that if the JDBC URL starts with
jdbc:oracle:<driver type>:@config-<provider-type>
(for example
jdbc:oracle:thin:@config-azure://myappconfig/sales_app1?label=dev
)
then the driver will attempt to load the <provider-type> provider from the
list of registered service providers. These providers implement the
OracleConfigurationProvider
interface which allows
the provider to set Connection Properties (including the physical URL and user/password).
jdbc:oracle:@<driver-type>:@config-<provider-type>://<parameters>
jdbc:oracle:thin:@config-azure://myappconfig/sales_app1?label=dev
jdbc:oracle:thin:@config-ociobject://n/mytenancy/b/mybucket/o/sales_app.json
jdbc:oracle:thin:@config-file://config.json
jdbc:oracle:thin:@config-https://myserver/config/myapp?key=dev
{ "dev": { "connect_descriptor": "myhost:5521/myservice", "user": "scott", "password": { "type": "base64", "value": "dGlnZXI=" }, "jdbc": { "oracle.jdbc.ReadTimeout": 1000, "defaultRowPrefetch": 20, "autoCommit": "false" } } }
oracle.jdbc.OracleConnection
contains
constant fields for almost all the properties supported by the JDBC Driver.
The name of the constants start with CONNECTION_PROPERTY_
. In Java code, the
constants fields can be used directly. Use the String value of a constant field to configure
the property through a URL or through a properties file. The following are the different methods of
configuring JDBC connection properties.
While using the EZConnect URL format, the connection properties can be configured via URL. After the '?' delimiter, Easy Connect supports a list of parameters as name-value pairs which can be optionally specified. The name and value of a connection property is separated by an '=' and multiple properties are separated by an '&'
jdbc:oracle:thin:@//salesserver1:1521/sales.us.example.com?oracle.net.networkCompression=on
jdbc:oracle:thin:@//salesserver1:1521/sales.us.example.com?oracle.net.networkCompression=on&oracle.net.networkCompressionThreshold=1024
Connection properties can also be configured via a properties file named ojdbc.properties
.
This properties file should be present under the TNS ADMIN directory. The TNS ADMIN directory can be
configured through the URL.
jdbc:oracle:thin:@//salesserver1:1521/sales.us.example.com?TNS_ADMIN=$ORACLE_HOME/tnsadmin/
jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=sales-svr)(PORT=1521))
(CONNECT_DATA=(SERVICE_NAME=sales.us.example.com))?TNS_ADMIN=$ORACLE_HOME/tnsadmin/
Connection properties can be configured through System properties as well. The below example shows how to configure a system property for a standalone Java application.
java -Doracle.net.networkCompression=on MyApp
When a Service Provider is used in the URL, the provider returns Connection
Properties that are applied to the OracleDataSource as explained in the
OracleConfigurationProvider
interface.
The JDBC Driver supports multiple authentication mechanisms:
A username and password pair is used to authenticate to the Oracle Database Server. The username and password are specified using the following options.
jdbc:oracle:thin:myuser/mypassword@//salesserver1:1521/sales.us.example.com
oracle.net.wallet_location
the JDBC Thin driver retrives the username and password
from the wallet. Please refer to Oracle documentation on how to store the username and password to
the wallet file.The JDBC Driver can authenticate to the Oracle Database using a certificate from a wallet / keystore.
For this, the Oracle Database Server has to be configured to support the TCPS authentication.
And, the JDBC driver must be configured with the connection property
oracle.net.authentication_services = (TCPS)
along with wallet / keystore
properties.
The JDBC Driver can authenticate to the Oracle Database using Kerberos.
For this, the Oracle Database Server has to be configured to support Kerberos authentication.
Please refer to Oracle documentation on how to configure the Oracle Database for Kerberos authentication.
Configure the following connection properties to enable Kerberos authentication in the JDBC Driver.
oracle.net.authentication_services=(KERBEROS5)
Additional configuration can be specified in a configuration file indicated by
the system property
oracle.net.kerberos5_mutual_authentication=true
oracle.net.kerberos5_cc_name=<path to the kerberos credentials cache file>
java.security.krb5.conf
. Please refer to JDK
documentation for more information.
The JDBC Driver can authenticate to the Oracle Database using RADIUS. For this, the Oracle Database Server has to be configured to support RADIUS authentication. Please refer to Oracle documentation on how to configure the Oracle Database for RADIUS authentication. Configure the following connection properties to enable RADIUS authentication in the JDBC driver.
oracle.net.authentication_services=(RADIUS) user=<username> password=<passcode>
As explained in the previous paragraphs, a Service Provider can set a number of Connection Properties, including Username and Password.
Oracle provides four types of JDBC driver.
thin
. To connect user scott
with password
tiger
to a database with SID
(system identifier)
orcl
through port 1521 of host myhost
, using the Thin
driver, you would write :
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@myhost:1521:orcl", "scott", "tiger");
oci
. To connect user scott
with password tiger
to a database with SID
(system
identifier) orcl
through port 1521 of host myhost
,
using the OCI driver, you would write :
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci:@myhost:1521:orcl", "scott", "tiger");
Note that you can also specify the database by a TNSNAMES
entry.
You can find the available TNSNAMES
entries listed in the file
tnsnames.ora
on the client computer from which you are connecting.
For example, if you want to connect to the database on host myhost
as user scott
with password tiger
that has a
TNSNAMES
entry of MyHostString
, enter:
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci8:@MyHostString","scott","tiger");
If your JDBC client and Oracle server are running on the same machine, the
OCI driver can use IPC (InterProcess Communication) to connect to the database
instead of a network connection. An IPC connection is much faster than a
network connection.
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci8:@","scott","tiger");
thin
and there is no difference
in your code between using the Thin driver from a client application or from
inside a server.kprb
and it actually runs within a default
session. You are already "connected". Therefore the connection should never
be closed.You can also use the Oracle-specific defaultConnection() method of the OracleDriver class which is generally recommended:DriverManager.getConnection("jdbc:oracle:kprb:");
or:DriverManager.getConnection("jdbc:default:connection:");
OracleDriver ora = new OracleDriver();
Connection conn = ora.defaultConnection();
Note: You are no longer required to register the OracleDriver
class for connecting with the Server-Side Internal driver, although there is
no harm in doing so. This is true whether you are using
getConnection()
or defaultConnection()
to make the
connection.DriverManager.getConnection()
method returns a new Java Connection
object every time you call it.
Note that although the method is not creating a new physical connection (only a
single implicit connection is used), it is returning a new object.In Oracle Database 23ai, the JDBC driver has been enhanced with new diagnosabilty features that help diagnose problems within the driver and the application. These features are based on the standard frameworks java.util.logging and the javax.management MBean.
This feature logs information about events that occur when JDBC driver runs. Events can include user-visible events, such as SQL exceptions, running of SQL statements, and detailed JDBC internal events. Logging can be enabled in the following ways.
This property can be set either as a system property or a regular JDBC property.
Logging can be enabled or disabled during the runtime of application
by invoking logging operations defined in DiagnosticsMXBean
.
The Diagnose First Failure feature helps diagnose failures on the first occurrence. The collected diagnostic data is dumped to the configured log handler when failure occurs. This feature is enabled by default. In the case where both logging and Diagnose First Failure are enabled, the collected data of Diagnose First Failure will be written to logging. Diagnose First Failure can also be disabled in the following ways.
This property can be set either as a system property or a regular JDBC property.
Diagnose First Failure can be enabled or disabled during the runtime
of application by invoking Diagnose First Failure operations defined in
DiagnosticsMXBean
.
Collecting the sensitive data like SQL parameter values is not permitted and disabled in the JDBC driver by default. The driver collects the sensitive data for logging only when Sensitive Diagnostics is permitted and enabled. Sensitive Diagnostics can be permitted and enabled in the following ways.
To permit sensitive diagnostics set the system property oracle.jdbc.diagnostic.permitSensitiveDiagnostics to true.
To enable sensitive diagnostics set the property oracle.jdbc.diagnostic.enableSensitiveDiagnostics to true.
Note that Sensitive diagnostics cannot be enabled without setting the permit sensitive diagnostics system property.
Sensitive diagnostics can be enabled or disabled during the runtime
of application by invoking sensitive diagnostics operations defined in
DiagnosticsMXBean
.
Diagnostics can be configured by setting the property java.util.logging.config.file with the configuration file name.
Sample configuration file:
handlers=java.util.logging.FileHandler java.util.logging.FileHandler.level = FINEST java.util.logging.FileHandler.formatter = oracle.jdbc.diagnostics.OracleSimpleFormatter java.util.logging.FileHandler.pattern = client.log java.util.logging.FileHandler.limit = 1000000000 java.util.logging.FileHandler.count = 5 oracle.ucp.level=FINEST oracle.jdbc.level=FINEST
access_string, accumulate_batch_result, batch_string, convert_nchar_literals_string, database_string, dataSizeBytes, dataSizeChars, dataSizeUnitsPropertyName, DEFAULT_CONNECTION_PROPERTIES, default_execute_batch_string, default_row_prefetch_string, defaultConn, defaultnchar_string, defaultncharprop_string, disable_defineColumnType_string, dll_string, dms_parent_name_string, dms_parent_type_string, dms_stmt_caching_metrics_string, dms_stmt_metrics_string, execute_batch_string, fixed_string_string, include_synonyms_string, j2ee_compliance, jdbc_string, logon_as_internal_str, nls_lang_backdoor, no_caching_buffers, oracle_string, password_string, permit_timestamp_date_mismatch_string, prefetch_string, prelim_auth_string, process_escapes_string, protocol_string, protocolFullName_string, proxy_client_name, read_timeout, remarks_string, report_remarks_string, restrict_getTables_string, retain_v9_bind_behavior_string, row_prefetch_string, server_string, set_new_password_string, SetFloatAndDoubleUseBinary_string, synonyms_string, systemTypeMap, tcp_no_delay, useFetchSizeWithLongColumn_prop_string, useFetchSizeWithLongColumn_string, user_string, v8compatible_string, xa_trans_loose
Constructor and Description |
---|
OracleDriver() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
getBuildDate()
Returns a String that specifies exactly when the jar file was
built.
|
static java.lang.String |
getDriverVersion()
Returns a String that specifies the Oracle version number
of the driver.
|
static java.lang.String |
getJDBCVersion()
Returns a String that specifies the version of the JDBC
spec supporte by the driver.
|
static boolean |
isDebug()
Returns true if this jar includes debug code.
|
static boolean |
isDMS()
Returns true if this jar includes DMS instrumentation.
|
static boolean |
isInServer()
Returns true if this jar was built to run in the Oracle Java VM.
|
static boolean |
isJDK14()
Deprecated.
|
static boolean |
isPrivateDebug()
Returns true if this jar includes Oracle internal debug code.
|
static void |
main(java.lang.String[] args)
Prints a description of the Oracle JDBC driver .jar file
to System.out.
|
_INTERNAL_ORACLE_connectAsync, acceptsURL, connect, connect, defaultConnection, getCompileTime, getExecutorService, getMajorVersion, getMinorVersion, getMonitorLock, getParentLogger, getPropertyInfo, getSystemPropertyDateZeroTime, getSystemPropertyDateZeroTimeExtra, getSystemPropertyFastConnectionFailover, jdbcCompliant, processSqlEscapes, registerMBeans, setExecutorService, unRegisterMBeans
public static boolean isDMS()
public static boolean isInServer()
public static boolean isJDK14()
public static boolean isDebug()
public static boolean isPrivateDebug()
public static java.lang.String getJDBCVersion()
public static java.lang.String getDriverVersion()
public static java.lang.String getBuildDate()
public static void main(java.lang.String[] args) throws java.lang.Exception
args
- expects getVersion and Connection URLjava.lang.Exception