Driver
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.
Once you have registered the driver, 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, user name, and password as separate parameters:
getConnection(String URL, String user, String password);
Where the URL is of the form:
jdbc:oracle:<drivertype>:@<database>
The following example connects user scott
with password tiger
to a database with SID orcl
through port 1521 of host myhost
, using the Thin driver.
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@myhost:1521:orcl", "scott", "tiger");
The following signature takes the URL, user name, and password all as part of a URL parameter:
getConnection(String URL);
Where the URL is of the form:
jdbc:oracle:<drivertype>:<user>/<password>@<database>
The following example connects user scott
with password tiger
to a database on host myhost
using the OCI driver. In this case, however, the URL includes the userid and password, and is the only input parameter.
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci8:scott/tiger@myhost);
If you want to connect with the Thin driver, you must specify the port number and SID. For example, if you want to connect to the database on host myhost
that has a TCP/IP listener up on port 1521, and the SID
(system identifier) is orcl
:
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:scott/tiger@myhost:1521:orcl);
The following signature takes a URL, together with a properties object that specifies user name 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();
"password",
info.put ("user", "scott");
info.put ("tiger"
);
info.put ("defaultRowPrefetch","15");
getConnection ("jdbc:oracle:oci8:@",info);
The table below lists the connection properties that Oracle JDBC drivers support.
Connection Properties Recognized by Oracle JDBC Drivers
Name | Short Name | Type | Description |
---|---|---|---|
user | n/a | String | the user name for logging into the database |
password | n/a | String | the password for logging into the database |
database | server | String | the connect string for the database |
internal_logon | n/a | String | a role, such as sysdba or sysoper , that allows you to log on as sys |
defaultRowPrefetch | prefetch | String (containing integer value) | the default number of rows to prefetch from the server (default value is "10") |
remarksReporting | remarks | String (containing boolean value) | "true" if getTables() and getColumns() should report TABLE_REMARKS; equivalent to using setRemarksReporting() (default value is "false") |
defaultBatchValue | batchvalue | String (containing integer value) | the default batch value that triggers an execution request (default value is "10") |
includeSynonyms | synonyms | String (containing boolean value) | "true" to include column information from predefined "synonym" SQL entities when you execute a DataBaseMetaData getColumns() call; equivalent to connection setIncludeSynonyms() call (default value is "false") |
processEscapes | n/a | String (containing boolean value) | "false" to disable escape processing for statements (Statement or PreparedStatement) created from this connection. Set this to "false" if you want to avoid many calls to Statement.setEscapeProcessing(false); . This is espcially usefull for PreparedStatement where a call to setEscapeProcessing(false) would have no effect. The default is "true". |
defaultNChar | n/a | String (containing boolean value) | "false" is the default. If set to "true", the default behavior for handling character datatypes is changed so that NCHAR/NVARCHAR2 become the default. This means that setFormOfUse() won't be needed anymore when using NCHAR/NVARCHAR2. This can also be set as a java property :java -Doracle.jdbc.defaultNChar=true myApplication |
useFetchSizeWithLongColumn | n/a | String (containing boolean value) | "false" is the default. THIS IS A THIN ONLY PROPERTY. IT SHOULD NOT BE USED WITH ANY OTHER DRIVERS. If set to "true", the performance when retrieving data in a 'SELECT' will be improved but the default behavior for handling LONG columns will be changed to fetch multiple rows (prefetch size). It means that enough memory will be allocated to read this data. So if you want to use this property, make sure that the LONG columns you are retrieving are not too big or you may run out of memory. This property can also be set as a java property : java -Doracle.jdbc.useFetchSizeWithLongColumn=true myApplication |
SetFloatAndDoubleUseBinary | n/a | String (containing boolean value) | "false" is the default. If set to "true", causes the java.sql.PreparedStatment setFloat and setDouble API's to use internal binary format as for BINARY_FLOAT and BINARY_DOUBLE parameters. See oracle.jdbc.OraclePreparedStatement setBinaryFloat and setBinaryDouble |
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.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, 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 | Description |
---|---|
OracleDriver() |
Modifier and Type | Method | Description |
---|---|---|
static String |
getBuildDate() |
Returns a String that specifies exactly when the jar file was built.
|
static String |
getDriverVersion() |
Returns a String that specifies the Oracle version number of the driver.
|
static 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 instrumentaion.
|
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(String[] args) |
Prints a description of the Oracle JDBC driver .jar file to System.out.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
acceptsURL, connect, connect, connect, defaultConnection, getCompileTime, getMajorVersion, getMinorVersion, getParentLogger, getPropertyInfo, getSystemPropertyDateZeroTime, getSystemPropertyFastConnectionFailover, jdbcCompliant, processSqlEscapes, registerMBeans, unRegisterMBeans
public static final boolean isDMS()
public static final boolean isInServer()
public static final boolean isJDK14()
public static final boolean isDebug()
public static final boolean isPrivateDebug()
public static final String getJDBCVersion()
public static final String getDriverVersion()
public static final String getBuildDate()