BEA Logo BEA WebLogic Server Release 5.0

  Corporate Info  |  News  |  Solutions  |  Products  |  Partners  |  Services  |  Events  |  Download  |  How To Buy

Using jdbcKona/Oracle

I. Introduction
What's in this document
Mapping of types between Oracle and jdbcKona/Oracle

II. Using jdbcKona/Oracle
How to connect to an Oracle DBMS with jdbcKona
Other properties you can set with jdbcKona/Oracle
Setting properties for WebLogic JDBC use
General notes
Waiting on Oracle Resources
Autocommit
Codeset support (Internationalization)
Support for Oracle array fetches
Using stored procedures
Syntax for stored procedures in jdbcKona/Oracle
Binding a parameter to an Oracle cursor
Notes on CallableStatement
Notes on DatabaseMetaData methods
About jdbcKona/Oracle and Oracle NUMBER columns

III. Other related documents
Installing WebLogic (non-Windows)
Installing WebLogic (Windows)
Developers Guides
API Reference Manual
Using the jdbcKona Type 2 JDBC drivers
Using WebLogic JDBC
Using dbKona
Performance tuning your JDBC application
Troubleshooting JDBC hangs and SEGVs
Choosing a Java Database Connectivity driver
Glossary
Code examples
jdbcKona/Oracle examples
jdbcKona examples

I. Introduction

What's in this document

Covered in this document are general guidelines for using the WebLogic jdbcKona native JDBC driver for Oracle, jdbcKona/Oracle. For general notes and an implementation guide that cover all of WebLogic's native drivers, check the Native jdbcKona driver overview and implementation guide.

If you are using WebLogic JDBC, WebLogic's multitier JDBC implementation, you should also refer to the Developers Guide Using WebLogic JDBC for more information.

Top of this section

Mapping of types between Oracle and jdbcKona/Oracle

Oracle jdbcKona/Oracle
Varchar String
Number Tinyint
Number Smallint
Number Integer
Number Long
Number Float
Number Numeric
Number Double
Long Longvarchar
RowID String
Date Timestamp
Raw (var)Binary
Long raw Longvarbinary
Char (var)Char
Boolean* Number OR Varchar
MLS label String
*Note that when PreparedStatement.setBoolean() is called, it will convert a VARCHAR type to "1" or "0" (string), and it will convert a NUMBER type to 1 or 0 (number).

Top of the page

II. Using jdbcKona/Oracle

How to connect to an Oracle DBMS with jdbcKona
Other properties you can set with jdbcKona/Oracle
Setting properties for WebLogic JDBC use
General notes
Waiting on Oracle Resources
Autocommit
Codeset support (Internationalization)
Support for Oracle array fetches
Using stored procedures
Syntax for stored procedures in jdbcKona/Oracle
Binding a parameter to an Oracle cursor
Notes on CallableStatement
Notes on DatabaseMetaData methods
About jdbcKona/Oracle and Oracle NUMBER columns

Top of the page

How to connect to an Oracle DBMS with jdbcKona

In general, connecting happens in two steps:

  1. You must load the proper JDBC driver. The most efficient way to load the JDBC driver is to call Class.forName().newInstance() with the name of the driver class, which properly loads and registers the JDBC driver, as in this example:
      Class.forName("weblogic.jdbc.oci.Driver").newInstance();
  2. You request a JDBC connection by calling the DriverManager.getConnection() method, which takes as its parameters the URL of the driver and other information about the connection.

Note that both steps describes the JDBC driver, but in a different format. The full package name is period-separated, and the URL is colon-separated. The URL must include at least jdbc:weblogic:oracle, and may include other information, including server name and database name.

There are several variations on this basic pattern which are described here for Oracle. For a full code example, check the beginning sections in the Implementation guide in the overview, Using the jdbcKona family of JDBC drivers.

Method 1

The simplest way is by passing the URL of the driver that includes the name of the server, along with a username and password, as arguments to the DriverManager.getConnection() method, as in this jdbcKona/Oracle example:
  Class.forName("weblogic.jdbc.oci.Driver").newInstance();
  Connection conn =
    DriverManager.getConnection("jdbc:weblogic:oracle:DEMO",
                                "scott",
	              	        "tiger");
where DEMO is the V2 alias of an Oracle database. Note that the calling Class.forName().newInstance() properly loads and registers the driver.

Method 2

You can also pass a java.util.Properties object with parameters for connection as an argument to the DriverManager.getConnection() method. In this example we illustrate how to connect to the DEMO database:
  Properties props = new Properties();
  props.put("user",         "scott");
  props.put("password",     "tiger");
  props.put("server",       "DEMO");

  Class.forName("weblogic.jdbc.oci.Driver").newInstance();
  Connection conn =
    DriverManager.getConnection("jdbc:weblogic:oracle",
                                props);
If you do not supply a server name ("DEMO" in the example above), the system will look for an environment variable (ORACLE_SID in the case of Oracle). You may also choose to add the server name to the URL, with this format:
"jdbc:weblogic:oracle:DEMO"
in which case you do not need to provide a "server" property.

You can also set properties in a single URL, for use with products like PowerSoft's PowerJ. For details, see Using URLs with WebLogic products.

Method 3

If you prefer, you can load the JDBC driver from the command line with the command:

 $ java -Djdbc.drivers=weblogic.jdbc.oci.Driver classname

where classname is the name of the application you want to run; and then use a Properties object to set parameters necessary for connecting to the DBMS. In this case, you will not need to call the Class.forName().newInstance() method, as shown here:

  Properties props = new Properties();
  props.put("user",         "scott");
  props.put("password",     "tiger");
  props.put("server",       "DEMO");

  Connection conn =
    DriverManager.getConnection("jdbc:weblogic:oracle",
                                props);

Other properties you can set for jdbcKona/Oracle

There are other properties that you can set for jdbcKona/Oracle that are covered later in this document.

allowMixedCaseMetaData
weblogic.oci.insertBlobChunkSize
weblogic.oci.selectBlobChunkSize

jdbcKona/Oracle also allows setting a property -- allowMixedCaseMetaData -- to the boolean true. This property sets up the Connection to use mixed case in calls to DatabaseMetaData methods. Otherwise, Oracle defaults to UPPERCASE for database meta data. Here is an example of setting up the properties to include this feature:

  Properties props = new Properties();
  props.put("user",                   "scott");
  props.put("password",               "tiger");
  props.put("server",                 "DEMO");
  props.put("allowMixedCaseMetaData", "true");

  Connection conn =
    DriverManager.getConnection("jdbc:weblogic:oracle",
                                props);

If you do not set this property, jdbcKona/Oracle defaults to the Oracle default, which uses UPPERCASE for database meta data.

Setting properties for WebLogic JDBC use

If you are using WebLogic JDBC in a multitier environment with a two-tier jdbcKona driver, you will set connection properties in a slightly different way. See the Developers Guide Using WebLogic JDBC for more details.

General notes

Always call the Connection.close()method to close the Connection when you have finished working with it. Closing objects releases resources on the remote DBMS and within your application, as well as being just good programming practice. Other jdbcKona objects that you should close() after final use:
  • Statement (PreparedStatement, CallableStatement)
  • ResultSet
Top of this section

Waiting on Oracle resources

With release 2.5, WebLogic's jdbcKona/Oracle driver supports Oracle's oopt() C functionality, which allows a client to wait until resources become available. The Oracle C function sets options in cases where requested resources are not available; for example, whether to wait for locks.

The developer can set whether a client will wait for DBMS resources, or will receive an immediate exception. Here is an example from the example examples/jdbc/oracle/waiton.java:

  java.util.Properties props = new java.util.Properties();
  props.put("user",     "scott");
  props.put("password", "tiger");
  props.put("server",   "goldengate");

  Class.forName("weblogic.jdbc.oci.Driver").newInstance();

  // You must cast the Connection as a weblogic.jdbc.oci.Connection
  // to take advantage of this extension
  Connection conn =
    (weblogic.jdbc.oci.Connection)
       DriverManager.getConnection("jdbc:weblogic:oracle", props);

  // After constructing the Connection object, immediately call
  // the waitOnResources method
  conn.waitOnResources(true);

Note that use of this method can cause several error return codes while waiting for internal resources that are locked for short durations.

To take advantage of this feature, you must first cast your Connection object as a weblogic.jdbc.oci.Connection, and then call the waitOnResources() method.

This functionality is described in section 4-97 of The OCI Functions for C.

Top of this section

Autocommit

The default transaction mode for JDBC assumes autocommit to be true. You will improve the performance of your programs by setting autocommit to false after creating a Connection object with the statement:

   Connection.setAutoCommit(false);
Top of this section

Codeset support

JDBC and your jdbcKona/Oracle driver handle character strings in Java as Unicode strings. Since the Oracle DBMS uses a different codeset, the driver must convert character strings from Unicode to the codeset used by Oracle. The jdbcKona/Oracle driver will examine the value stored in the Oracle environment variable NLS_LANG and select a codeset for the JDKWhat's JDK? to use for the conversion, using the mapping shown in the table below. If the NLS_LANG variable is not set, or is set to a codeset not recognized by the JDK, the driver will not be able to determine the correct codeset. (Consult your Oracle documentation for information on the correct syntax for setting NLS_LANG.)

jdbcKona/Oracle provides a way to set the codeset from within your Java code. By setting a connection property, weblogic.codeset, you can override the value stored in the NLS_LANG environment variable. For example, to use the cp932 codeset, create a Properties object and set the weblogic.codeset property before calling DriverManager.getConnection(), as in this example:

  java.util.Properties props = new java.util.Properties();
  props.put("weblogic.codeset", "cp932");
  props.put("user", "scott");
  props.put("password", "tiger");

  String connectUrl = "jdbc:weblogic:oracle";

  Class.forName("weblogic.jdbc.oci.Driver").newInstance();
  Connection conn = 
     DriverManager.getConnection(connectUrl, props);

Note that codeset support can vary with different JVMsWhat's JVM?. Check the documentation for the JDK you are using to determine if a particular codeset is supported. You can find the List of supported encodings for JDK 1.1 at the JavaSoft website.

Mapping of NLS_LANG settings to JDK codesets

NLS_LANG JDK codeset

us7ascii

ASCII

ja16sjis

SJIS

us8pc437

Cp437

we8ebcdic37

Cp1046

we8ebcdic500

Cp500

we8pc850

Cp850

we8iso8859p1

ISO8859_1

ee8iso8859p2

ISO8859_2

se8iso8859p3

ISO8859_3

nee8iso8859p4

ISO8859_4

cl8iso8859p5

ISO8859_5

ar8iso8859p6

ISO8859_6

el8iso8859p7

ISO8859_7

iw8iso8859p8

ISO8859_8

we8iso8859p9

ISO8859_9

ne8iso8859p10

ISO8859_10

ru8pc866

Cp866

ee8pc852

Cp852

ru8pc855

Cp855

tr8pc857

Cp857

cl8maccyrillic

MacCyrillic

we8pc860

Cp860

is8pc861

Cp861

ee8mswin1250

Cp1250

cl8mswin1251

Cp1251

el8mswin1253

Cp1253

n8pc865

Cp865

ee8macce

MacCentralEurope

ee8maccroatian

MacCroatian

tr8macturkish

MacTurkish

is8macicelandic

MacIceland

el8macgreek

MacGreek

we8macroman8

MacRoman

th8macthai

MacThai

el8pc737

Cp737

lt8pc772

Cp772

lt8pc774

Cp774

cdn8pc863

Cp863

ja16euc

EUC_JP

ko16ksc5601

EUC_KR

zht16big5

Big5

al24utffss

UTF8

utf8

UTF8

Top of this section

Using Oracle Blobs

With release 3.1, two new properties were added for use with jdbcKona/Oracle in support of Oracle's Blob chunking:
weblogic.oci.insertBlobChunkSize. This property affects the buffer size of input streams bound to a PreparedStatement. Blob chunking requires an Oracle 7.3.x or higher Oracle Server; to use this property, you must be connected to an Oracle Server that supports this feature.

Set this property to a positive integer to insert Blobs into an Oracle DBMS with the Blob chunking feature. By default, this property is set to zero (0), which means that BLOB chunking is turned off.

weblogic.oci.selectBlobChunkSize. This property sets the size of output streams associated with a JDBC ResultSet. The mechanism for piecewise selects does not have the same use restrictions as that for Blob inserts, so this property is set to 65534 by default. It is not necessary to turn this property off.

Set this property to the size of the desired output stream in bytes.

There is a matching property for use in a multitier environment when using jdbcKona/Oracle with WebLogic JDBC: weblogic.t3.blobChunkSize. This defines the buffer size used for streaming blobs between WebLogic and the WebLogic JDBC client

Top of this section

Support for Oracle array fetches

With release 2.5, jdbcKona/Oracle supports Oracle array fetches. With this change, calling ResultSet.next() the first time will get an array of rows and store it in memory, rather than retrieving a single row. Each subsequent call to next() will read a row from the rows in memory until they are exhausted, and only then will next() go back to the database.

You set a property (java.util.Property) to control the size of the array fetch. The property is weblogic.oci.cacheRows; it is set by default to 100. Here is an example of setting this property to 300, which means that calls to next() only hit the database once for each 300 rows retrieved by the client.

  Properties props = new Properties();
  props.put("user",                   "scott");
  props.put("password",               "tiger");
  props.put("server",                 "DEMO");
  props.put("weblogic.oci.cacheRows", "300");

  Class.forName("weblogic.jdbc.oci.Driver").newInstance();
  Connection conn =
    DriverManager.getConnection("jdbc:weblogic:oracle",
                                props);

You can improve client performance and lower the load on the database server by taking advantage of this JDBC extension. Caching rows in the client, however, requires client resources. You should tune your application for the best balance between performance and client resources, depending upon your network configuration and your application.

If any columns in a SELECT are of type LONG, the cache size will be temporarily reset to one (1) for the ResultSet associated with that select statement.

Top of this section

Using stored procedures

weblogic.jdbc.oci.CallableStatement

Syntax for stored procedures in jdbcKona/Oracle

With release 2.5, the syntax for stored procedures in Oracle was altered in the jdbcKona/Oracle examples to match the JDBC specification. (All of the examples also show native Oracle SQL, commented out, just above the correct usage; the native Oracle syntax works as it did in the past.) You can read up on general notes on stored procedures for the jdbcKona drivers in the Overview of the jdbcKona family of JDBC drivers.

Note that Oracle does not natively support binding to "?" values in a SQL statement. Instead it uses ":1", ":2", etc. We allow you to use either in your SQL with jdbcKona/Oracle.

Binding a parameter to an Oracle cursor

WebLogic has created an extension to JDBC (weblogic.jdbc.oci.CallableStatement) that allows you to bind a parameter for a stored procedure to an Oracle cursor. You can create a JDBC ResultSet object with the results of the stored procedure. This allows you to return multiple ResultSets in an organized way. The ResultSets are determined at run time in the stored procedure.

Here is an example. First define the stored procedures as follows:

create or replace package 
curs_types as 
type EmpCurType is REF CURSOR RETURN emp%ROWTYPE;
end curs_types;
/

create or replace procedure 
single_cursor(curs1 IN OUT curs_types.EmpCurType,
ctype in number) AS BEGIN 
  if ctype = 1 then
    OPEN curs1 FOR SELECT * FROM emp;
  elsif ctype = 2 then
    OPEN curs1 FOR SELECT * FROM emp where sal > 2000;
  elsif ctype = 3 then
    OPEN curs1 FOR SELECT * FROM emp where deptno = 20;
  end if;
END single_cursor;
/

create or replace procedure 
multi_cursor(curs1 IN OUT curs_types.EmpCurType,
             curs2 IN OUT curs_types.EmpCurType,
             curs3 IN OUT curs_types.EmpCurType) AS
BEGIN 
    OPEN curs1 FOR SELECT * FROM emp;
    OPEN curs2 FOR SELECT * FROM emp where sal > 2000;
    OPEN curs3 FOR SELECT * FROM emp where deptno = 20;
END multi_cursor;
/
In your Java code, you will construct CallableStatements with the stored procedures and register the output parameter as data type java.sql.Types.OTHER. When you retrieve the data into a ResultSet, use the output parameter index as an argument for the getResultSet() method.
  weblogic.jdbc.oci.CallableStatement cstmt = 
    (weblogic.jdbc.oci.CallableStatement)conn.prepareCall(
                       "BEGIN OPEN ? " +
                       "FOR select * from emp; END;");
  cstmt.registerOutParameter(1, java.sql.Types.OTHER);

  cstmt.execute();
  ResultSet rs = cstmt.getResultSet(1);
  printResultSet(rs);
  rs.close();
  cstmt.close();

  weblogic.jdbc.oci.CallableStatement cstmt2 = 
    (weblogic.jdbc.oci.CallableStatement)conn.prepareCall(
                       "BEGIN single_cursor(?, ?); END;");
  cstmt2.registerOutParameter(1, java.sql.Types.OTHER);

  cstmt2.setInt(2, 1);  
  cstmt2.execute();
  rs = cstmt2.getResultSet(1);
  printResultSet(rs);

  cstmt2.setInt(2, 2);  
  cstmt2.execute();
  rs = cstmt2.getResultSet(1);
  printResultSet(rs);

  cstmt2.setInt(2, 3);  
  cstmt2.execute();
  rs = cstmt2.getResultSet(1);
  printResultSet(rs);

  cstmt2.close();

  weblogic.jdbc.oci.CallableStatement cstmt3 = 
    (weblogic.jdbc.oci.CallableStatement)conn.prepareCall(
                         "BEGIN multi_cursor(?, ?, ?); END;");
  cstmt3.registerOutParameter(1, java.sql.Types.OTHER);
  cstmt3.registerOutParameter(2, java.sql.Types.OTHER);
  cstmt3.registerOutParameter(3, java.sql.Types.OTHER);

  cstmt3.execute();

  ResultSet rs1 = cstmt3.getResultSet(1);
  ResultSet rs2 = cstmt3.getResultSet(2);
  ResultSet rs3 = cstmt3.getResultSet(3);
For the full code for this example, including the printResultSet() method, see the examples in the examples/jdbc/oracle/ directory.

Note that the default size of an Oracle stored procedure string is 256K.

Notes on using CallableStatement

The default length of a string bound to an OUTPUT parameter of a CallableStatement is 128 characters. If the value you assign to the bound parameter exceeds that length, you will get the following error:

ORA-6502: value or numeric error

You can adjust the length of the value of the bound parameter by passing an explicit length with the scale argument to the CallableStatement.registerOutputParameter() method. Here is a code example that binds a VARCHAR that will never be larger than 256 characters:

  CallableStatement cstmt =
    conn.prepareCall("BEGIN testproc(?); END;");

  cstmt.registerOutputParameter(1, Types.VARCHAR, 256);
  cstmt.execute();
  System.out.println(cstmt.getString());
  cstmt.close();

Top of this section

Notes on DatabaseMetaData methods

With release version 2.1, all of DatabaseMetaData is implemented in jdbcKona/Oracle. There are some variations that are specific to Oracle that are detailed here:
  • As a general rule, the String catalog argument is ignored in all DatabaseMetaData methods.
  • In the DatabaseMetaData.getProcedureColumns() method:
    • The String catalog argument is ignored.
    • The String schemaPattern argument accepts only exact matches (no pattern matching).
    • The String procedureNamePattern argument accepts only exact matches (no pattern matching).
    • The String columnNamePattern argument is ignored.
  • In release 2.5, changes corrected some of the getTypeInfo() metadata.
  • In release 2.5, changes added a missing column to getColumns.
  • In release 2.5, changes renamed a column in getPrimaryKeys().
  • In release 2.5, changes made supportsMixedCaseIdentifiers() return false, which conforms to the JDBC specification.

Top of this section

About jdbcKona/Oracle and Oracle's NUMBER column

Oracle provides a column type called NUMBER, which can be optionally specified with a precision and a scale, in the forms NUMBER(P) and NUMBER(P,S). Even in the simple unqualified NUMBER form, this column can hold all number types from small integer values to very large floating point numbers, with high precision.

WebLogic's jdbcKona/Oracle reliably converts the values in a column to the Java type requested when a jdbcKona/Oracle application asks for a value from such a column. Of course, if a value of 123.456 is asked for with getInt(), the value will be rounded.

The method getObject(), however, poses a little more complexity. jdbcKona/Oracle guarantees to return a Java object which will represent any value in a NUMBER column with no loss in precision. This means that a value of 1 can be returned in an Integer, but a value like 123434567890.123456789 can only be returned in a BigDecimal.

There is no metadata from Oracle to report the maximum precision of the values in the column, so jdbcKona/Oracle must decide what sort of object to return based on each value. This means that one ResultSet may return multiple Java types from getObject() for a given NUMBER column. A table full of integer values may all be returned as Integer from getObject(), whereas a table of floating point measurements may be returned primarily as Double, with some Integer if any value happens to be something like "123.00". Oracle does not provide any information to distinguish between a NUMBER value of "1" and a NUMBER of "1.0000000000".

There is some more reliable behavior with qualified NUMBER columns, that is, those defined with a specific precision. Oracle's metadata provides these parameters to the driver so jdbcKona/Oracle will always return a Java object appropriate for the given precision and scale, regardless of the values in the table.
Column definition Returned by getObject()
NUMBER(P <= 9) Integer
NUMBER(P <= 18) Long
NUMBER(P >= 19) BigDecimal
NUMBER(P <=16, S > 0) Double
NUMBER(P >= 17, S > 0) BigDecimal

Top of this section

 

Copyright © 2000 BEA Systems, Inc. All rights reserved.
Required browser: Netscape 4.0 or higher, or Microsoft Internet Explorer 4.0 or higher.
Last updated 04/05/1999