Solved

oracle.jdbc.driver.OracleConnection  ClassCastException

Posted on 2001-08-10
9
2,788 Views
Last Modified: 2008-03-04
Hi Experts!

When i Tried to Pass Arrays from Java to Oracle Procedure
I'm getting a runtime Exception as oracle.jdbc.driver.OracleConnection  ClassCastException

I'm using Weblogic Server and it's Pool Connection. But the ArrayDescriptor Class Takes a OracleConnection as a Parameter. If anybody can tell me How to Cast the Weblogic Connection Object to OralceConnection Objects.

----Code-------
import java.sql.*;
import oracle.sql.*;
import oracle.jdbc.oracore.Util;
import oracle.jdbc.driver.*;

public Class
{
private DBConnectionManager connMgr;    
     public void init(ServletConfig conf) throws ServletException {
          super.init(conf);        
          connMgr = DBConnectionManager.getInstance();
     }
Connecntion conn1=null;
conn1=connMgr.getConnection("oracle");
String arr1[]=new String[40];
;
;
;
//fill the arrays with values;

ArrayDescriptor desc = ArrayDescriptor.createDescriptor("NUM_VARRAY", (oracle.jdbc.driver.OracleConnection) conn1);
ARRAY newArray1 = new ARRAY(desc, conn1,arr1);
ARRAY newArray2 = new ARRAY(desc, conn1,arr2);
ARRAY newArray3 = new ARRAY(desc, conn1,arr3);
PreparedStatement ps = conn1.prepareStatement (" BEGIN  TEST_VARRAY (:1,:2,:3); END;");
((OraclePreparedStatement)ps).setARRAY (1, newArray1);
((OraclePreparedStatement)ps).setARRAY (2, newArray2);
((OraclePreparedStatement)ps).setARRAY (3, newArray3);
ps.execute ();
ps.close();
}
--------------

Sudharsun
0
Comment
Question by:sudhagowshi
9 Comments
 
LVL 3

Expert Comment

by:rjackman
Comment Utility
hi
pls post ur code
cheers
RJ
0
 
LVL 3

Expert Comment

by:black
Comment Utility
Remember when you are using connection pooling the actual connection object you get is not the real connection to the database, it's simply a wrapper, so when you call close() it just returns it to the pool instead of closing it. You would have to look at the weblogic api for a weblogic connection object to see if you can get access to the real underlying oracle connection. Generally speaking you can't do that because this effectively shortcuts connection pooling.
0
 
LVL 1

Expert Comment

by:dviji
Comment Utility
"You can assign any subclass object to its parent class i.e OracleConnection to Connection... Because OracleConnection is a subclass of Connection. "

 If the weblogicConnection is a subclass of OracleConnection then you can achieve it... instead if the WeblogicConnection uses OracleConnection (i.e as a member variable) then it is not possible to cast.

 In your code you no need to cast it OracleConnection and PreparedStatement to OraclePreparedStatement. Because what are all the methods available in java.sql.XXXXX interface that is visible for you(not all the thrid party driver methods i.e oracle.jdbc.OracleConnection.xxx() methods).
i.e  What are all the features available in your java.sql package you can gain it. All the drivers will give access only those functionality to the users.

I hope now you might have some clear picture. Till if you want to cast to OracleConnection let me know your requierment.(If you would like). I will try to give some solutions.

Best of luck...

...dviji


0
 

Author Comment

by:sudhagowshi
Comment Utility
Hi

I went through the Weblogic API and foud these

Class weblogic.jdbc.t3.Connection
    (implements java.sql.Connection)
Class weblogic.jdbc.pool.Connection
    (implements java.sql.Connection)

in this case can I cast the connection object to OracleConnection Object.


0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Accepted Solution

by:
black earned 300 total points
Comment Utility
no because they do not extent OracleConnection, it implements java.sql.Connection which the OracleConnection does implement as well but I assume that the weblogic.jdbc.t3.Connection class is a wrapper for the OracleConnection (i.e. the OracleConnection is a private variable in the weblogic Connection class). If the weblogic api does not return you the underlying Oracle class (i.e. maybe a get method??) then you're are going to have to use DriverManager.getConnection and get an Oracle connection directly.
0
 
LVL 3

Expert Comment

by:black
Comment Utility
Can't you simply use the void setArray(int i, Array x) method on the java.sql.PreparedStatement class?
It would be best if you didn't refer to vendor specific classes directly in your code but simply referred to the java classes which they inherit off. Is it possible to implement your logic using only the java.sql api?
0
 

Author Comment

by:sudhagowshi
Comment Utility
Hi

Can anyone tell me How to use the setArray method in Java.Sql Class

cheers
Sudha
0
 
LVL 3

Expert Comment

by:black
Comment Utility
This implementation is depended on the jdbc driver vendor. That's due to the fact that the Array class is an interface and each vendor implements it differently. According to the Oracle docs for 8.1.5:
Passing an Array to a Prepared Statement
Pass an array to a prepared statement as follows (use similar steps to pass an array to a callable statement):

Construct an ArrayDescriptor object for the SQL type that the array will contain (unless one has already been created for this SQL type). See "Class oracle.sql.ARRAY" for information about creating ArrayDescriptor objects.

ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor(sql_type_name,
connection);


where sql_type_name is a Java string specifying the user-defined SQL type name of the array, and connection is your Connection object. See "Working with Arrays" for information about SQL typenames.

Define the array that you want to pass to the prepared statement as an oracle.sql.ARRAY object.

ARRAY array = new ARRAY(descriptor, elements);


where descriptor is the ArrayDescriptor object previously constructed and elements is a java.lang.Object containing a Java array of the elements. These objects are converted to raw bytes of the appropriate SQL type.

Create a java.sql.PreparedStatement object containing the SQL statement to execute.

Cast your prepared statement to an OraclePreparedStatement and use the setARRAY() method of the OraclePreparedStatement object to pass the array to the prepared statement.

(OraclePreparedStatement)stmt.setARRAY(parameterIndex, array);


where parameterIndex is the parameter index, and array is the oracle.sql.ARRAY object you constructed previously.

Execute the prepared statement.

To retrieve it:
stmt.execute ("CREATE TYPE num_varray AS VARRAY(10) OF NUMBER(12, 2)");
stmt.execute ("CREATE TABLE varray_table (col1 num_varray)");
stmt.execute ("INSERT INTO varray_table VALUES (num_varray(100, 200))");

ResultSet rs = stmt.executeQuery("SELECT * FROM varray_table");
ARRAY my_array = ((OracleResultSet)rs).getARRAY(1);

// return the SQL type names, integer codes,
// and lengths of the columns
System.out.println ("Array is of type " + array.getSQLTypeName());
System.out.println ("Array element is of type code " + array.getBaseType());
System.out.println ("Array is of length " + array.length());

// get Array elements
      BigDecimal[] values = (BigDecimal[]) my_array.getArray();

      for (int i=0; i<values.length; i++)
      {
        BigDecimal out_value = (BigDecimal) values[i];
        System.out.println(">> index " + i + " = " + out_value.intValue());
      }

The above is a cut and paste from the oracle docs for Server 8.1.5. Read the docs under jdbc for more details

0
 

Author Comment

by:sudhagowshi
Comment Utility
But i can't use Oracle Connection

if u could tell me how to work with the java.sql.preparedStatement's setArray() method

Sudharsun
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now