1 2 Previous Next 16 Replies Latest reply on Sep 23, 2009 12:18 PM by 843859

    java.lang.ClassCastException while creating array descriptor

    843859

      Hi everyone, i used to pass string array from java to plsql. I wrote a java source, then i load db with loadjava. And i wrote java spec. Then i run the function but i am getting this error :

      java.lang.ClassCastException
      at oracle.jdbc.driver.PhysicalConnection.putDescriptor(PhysicalConnection.java:4921)
      at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:208)
      at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:175)
      at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:158)
      at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:125)
      at SplitterOracle3.tokens2(SplitterOracle3.java:29)

      My Java Source is :

      import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import oracle.sql.*; import oracle.jdbc.driver.OracleConnection; import oracle.jdbc.driver.OracleDriver; public class SplitterOracle3 { public static oracle.sql.ARRAY tokens2(String str,String delim) { try { //Class.forName("oracle.jdbc.driver.OracleDriver"); //DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); //Connection conn = new OracleDriver().defaultConnection( ); OracleDriver ora = new OracleDriver(); OracleConnection conn = (OracleConnection) ora.defaultConnection(); //ArrayDescriptor arrayDesc = ArrayDescriptor.createDescriptor("MY_ARRAY", ((conn).getRealConnection()); //Connection conn = DriverManager.getConnection("jdbc:default:connection:"); //Connection conn = ((DelegatingConnection) getDataSource().getConnection()).getInnermostDelegate(); // get an initial context //OracleConnection oracleConnection = (OracleConnection)WSJdbcUtil.getNativeConnection((WSJdbcConnection) wsConn); ArrayDescriptor arraydesc = ArrayDescriptor.createDescriptor ("ARR_VARCHAR_100", conn); /* String strarr[] = new String47; strarr[0]="ahmet"; strarr[1]="mehmet"; int curIndex = 0; int nextIndex = 0; boolean nextIsLastToken = false; int i=0; while (true) { nextIndex = str.indexOf(delim, curIndex); if (nextIsLastToken) //return false; break; if (nextIndex == -1) { nextIsLastToken=true; nextIndex = str.length(); } strarr = str.substring(curIndex, nextIndex); curIndex = nextIndex + 1; i++; } ARRAY dirArray = new ARRAY(arraydesc, conn, strarr); return dirArray;*/ } catch(Exception ex) { System.err.println(ex.getMessage()); ex.printStackTrace(); } return null; } /* public static void main(String[] args) { String str="2000,2,123553168,1,10,64895,65535,27662,64860,64895,65535,27662,64860,0,,,,,,0,0,2491039806,,,,,,,,,0,0,1,,2491039106,,,,,,,,,,,,"; String strarr[] = new String47; long l1,l2; int j=0; l1 = System.currentTimeMillis(); for ( int i=0; i<20000000; i++) { strarr = tokens2(str,","); } l2 = System.currentTimeMillis(); System.out.println("Fark :"+ (l2-l1)); }*/ }

      The line has "ArrayDescriptor.createDescriptor ("ARR_VARCHAR_100", conn);" causes this error.

      java.lang.ClassCastException
      at oracle.jdbc.driver.PhysicalConnection.putDescriptor(PhysicalConnection.java:4921)
      at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:208)
      at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:175)
      at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:158)
      at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:125)
      at SplitterOracle3.tokens2(SplitterOracle3.java:29)

      But i could not find the solution. Can you help me?

      Thanks for responses.

      Edited by: futi on Sep 21, 2009 1:44 AM

        • 1. Re: java.lang.ClassCastException while creating array descriptor
          masijade
          Post the code again using code tags (the button in the window where you post labeled "CODE", and make sure you post the real code, as, maybe it was just do to the formatting, but this line (from your post) definately would not even compile.
          String strarr[] = new String47;
          Edit: Arrgh, and you did it while I was replying, and now it is recognizable that line was commented out. Let me look again.
          • 2. Re: java.lang.ClassCastException while creating array descriptor
            masijade
            Okay, which line throws the Exception. (I am assuming th defaultConnection line as that is the only explicit cast I see, at first blush.)

            Edit: P.S. it nearly completely defeats the purpose of JDBC to be directly accessing the Oracle classes in this manner. In fact, since the drivers should, as much as possible, and it is almost always possible, only be accessed through JDBC, I don't believe there is any guarantee that the classes you are using will not change their interfaces from driver version to driver version.
            • 3. Re: java.lang.ClassCastException while creating array descriptor
              nneeraj
              can you remove all the commented lines and post in code tag?
              Preview before you post message.

              Edited: OK, seems like you have update. Ignore this. But can remove comments anyways.
              • 4. Re: java.lang.ClassCastException while creating array descriptor
                843859
                Thanks for response but how can i fix this?

                I tried to use these as connection object but they did produce same error again :
                               
                OracleDriver ora = new OracleDriver();
                OracleConnection conn = (OracleConnection) ora.defaultConnection(); 
                Connection conn = DriverManager.getConnection("jdbc:default:connection:");
                Connection conn = new OracleDriver().defaultConnection(  );
                I am using Oracle 11.1.0.7 and ojdbc5.jar on Java 1.5 environment.

                Edited by: futi on Sep 21, 2009 3:03 PM
                • 5. Re: java.lang.ClassCastException while creating array descriptor
                  masijade
                  [Establishing a Connection|http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html]
                  • 6. Re: java.lang.ClassCastException while creating array descriptor
                    843859
                    Thanks masijade, but it did not help me. I am getting same error again.

                    Edited :

                    Likely, ArrayDescriptor.createDescriptor method could not take that connection parameter. I think It internally casts a type and this ClassCastException takes place. ( I used OracleDataSource connection but i got same error again ).

                    Edited by: futi on Sep 22, 2009 1:19 AM
                    • 8. Re: java.lang.ClassCastException while creating array descriptor
                      Tolls
                      Out of curiosity, what's the definition of "ARR_VARCHAR_100" in the database?
                      • 9. Re: java.lang.ClassCastException while creating array descriptor
                        843859
                        ARR_VARCHAR_100 code is:
                        CREATE OR REPLACE TYPE ARR_VARCHAR_100 IS TABLE OF VARCHAR2(100)
                        By the way my new code :
                        import java.sql.Connection;
                        import java.sql.DriverManager;
                        import java.sql.SQLException;
                        
                        import javax.naming.Context;
                        import javax.naming.InitialContext;
                        import javax.sql.DataSource;
                        
                        import oracle.sql.*;
                        import oracle.jdbc.driver.OracleConnection;
                        import oracle.jdbc.driver.OracleDriver;
                        import oracle.jdbc.pool.OracleDataSource;
                        
                        public class SplitterOracle3 {
                        
                        
                             private  static OracleDataSource ods = null;
                             public static oracle.sql.ARRAY tokens2(String str,String delim)
                             {
                                  
                                  try
                                  {
                                       ods = new OracleDataSource();
                                    ods.setURL("jdbc:oracle:thin:@//localhost:1521/STREAMDB");
                                    ods.setUser("AQ_ADM_PRTP_V204");
                                    ods.setPassword("admin");
                                    OracleConnection conn = (OracleConnection)ods.getConnection();
                                    
                        
                                       ArrayDescriptor arraydesc =
                                         ArrayDescriptor.createDescriptor ("ARR_VARCHAR_100", conn);
                                     
                                     
                                  catch(Exception ex)
                                  {
                                       System.err.println(ex.getMessage());
                                       ex.printStackTrace();
                                  }
                                  return null;
                        
                        
                             }
                             
                        }
                        • 10. Re: java.lang.ClassCastException while creating array descriptor
                          843859
                          I did not say that, i wrote this java code and load this java code to oracle database ( loadjava command ). Then i will use this java class in plsql package. If i remove the lines that related to sql connection, and i put a System.out.println method, i can run this java class in plsql package, there was no problem about it. As i said, the problem is connection parameter of ArrayDescriptor.createDescriptor method.
                          • 11. Re: java.lang.ClassCastException while creating array descriptor
                            Tolls
                            Um, that's quite possibly a very pertinent bit of information.
                            It's not a Java environment most people work in...I know I haven't written anything of any consequence in Oracles Java environment (I tried with some XML transformations, but got annoyed by the fact the environment was so locked down I couldn't install a preferable parser).

                            Can you do a simple "select SYSDATE from dual" using that connection, and avoid doing the whole array thing?
                            • 12. Re: java.lang.ClassCastException while creating array descriptor
                              masijade
                              If this is to be loaded into the DB for it's use, then you should probably be asking at an Oracle forum, since it is the Oracle classes and methods that are giving you problems.
                              • 13. Re: java.lang.ClassCastException while creating array descriptor
                                jschellSomeoneStoleMyAlias
                                futi wrote:
                                I did not say that, i wrote this java code and load this java code to oracle database ( loadjava command ). Then i will use this java class in plsql package. If i remove the lines that related to sql connection, and i put a System.out.println method, i can run this java class in plsql package, there was no problem about it. As i said, the problem is connection parameter of ArrayDescriptor.createDescriptor method.
                                JDBC in an PL/SQL code is not the same as JDBC in a regular java application.

                                Code in a PL/SQL proc is probably only ever going to be specific to Oracle so do whatever makes it run - if that is your question.
                                • 14. Re: java.lang.ClassCastException while creating array descriptor
                                  843859
                                  All of you are right but may be some of you experienced with this error. I tried to establish lots of connections ( dataSource, defaultConnection ) but none of them were connected. As you said, i started a thread in Oracle Technology Forum and if a right answer will shared i would share it here too.
                                  1 2 Previous Next