Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

oracle.jdbc.driver.OracleConnection  ClassCastException

Posted on 2001-08-10
9
2,923 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
ID: 6371778
hi
pls post ur code
cheers
RJ
0
 
LVL 3

Expert Comment

by:black
ID: 6372853
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
ID: 6373402
"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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:sudhagowshi
ID: 6376617
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
 
LVL 3

Accepted Solution

by:
black earned 300 total points
ID: 6378315
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
ID: 6378323
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
ID: 6387732
Hi

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

cheers
Sudha
0
 
LVL 3

Expert Comment

by:black
ID: 6395681
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
ID: 6395692
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to install java on RHEL image on EC2 4 41
Android development question 2 54
pagenation logic how it is working in my code 1 54
Java basic valueOf question 1 29
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

860 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