3 Replies Latest reply: Jun 12, 2013 4:55 PM by rp0428 RSS

    need help on type casting

    vxwo0owxv

      I try to pass a number array to java from pl/sql and I got some code example from

      http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:3696816290928

       

      the example uses array of string but what I need is an array of number, I get this error in run time please help. and below is the source code and testcase.

      ERROR at line 1:

      ORA-29532: Java call terminated by uncaught Java exception: java.lang.ClassCastException

      ORA-06512: at "TK1.PRIME_PKG", line 20

      ORA-06512: at line 20

       

      CREATE OR REPLACE AND COMPILE

      JAVA SOURCE NAMED "Sieve"

      AS

      import java.util.*;

      import java.io.*;

      import java.sql.*;

      import java.math.*;

      import oracle.sql.*;

      import oracle.jdbc.driver.*;

       

      public class Sieve

      {

        private final static int testsize = 10000;

          private BitSet sieve;

       

          public Sieve() {}

       

          private Sieve(int size) {

              sieve = new BitSet((size+1)/2);

          }

       

          private boolean is_composite(int k)

          {

              assert k >= 3 && (k % 2) == 1;

              return sieve.get((k-3)/2);

          }

       

          private void set_composite(int k)

          {

              assert k >= 3 && (k % 2) == 1;

              sieve.set((k-3)/2);

          }

       

          public static List<Integer> sieve_of_eratosthenes(int max)

          {

              Sieve sieve = new Sieve(max + 1); // +1 to include max itself

              for (int i = 3; i*i <= max; i += 2) {

                  if (sieve.is_composite(i))

                      continue;

       

                  // We increment by 2*i to skip even multiples of i

                  for (int multiple_i = i*i; multiple_i <= max; multiple_i += 2*i)

                      sieve.set_composite(multiple_i);

       

              }

              List<Integer> primes = new ArrayList<Integer>();

              primes.add(2);

              for (int i = 3; i <= max; i += 2)

                  if (!sieve.is_composite(i))

                      primes.add(i);

       

              return primes;

          }

         

        public static void main(String[] args) {

        // test the Sieve prime work.

       

                  Sieve s2 = new Sieve();

                  List<Integer> t2 = s2.sieve_of_eratosthenes(testsize);

                  int[] intArray = new int[testsize];

                 

                 

                  for ( int j=0;j<intArray.length;j++){

               System.out.println(intArray[j]);         

                  }

       

              int k=0;

                       for (ListIterator<Integer> it = t2.listIterator(); it.hasNext(); ) {

           intArray[k]=it.next();

           System.out.println(intArray[k]);

           k++;

        }

                 

                 

       

       

              } //end main   

       

      //code from asktom

        public static void pass( java.lang.String p_in,

                  java.lang.String[] p_out ){

       

       

      /*

      * the simplest of datatypes -- the String.  If you remember

      * the C version with 6 formal parameters, null indicators,

      * strlen's, strcpy's and so on -- this is trivial in

      * comparision

      */

       

       

      if ( p_in != null ){

       

       

      System.out.println

      ( "The first parameter is " + p_in.toString() );

       

       

      p_out[0] = p_in.toUpperCase();

       

       

      System.out.println

      ( "Set out parameter to " + p_out[0].toString() );

      }

      }

       

       

      private static void show_array_info( oracle.sql.ARRAY p_in )

      throws SQLException{

       

       

      System.out.println( "Array is of type      " +

                  p_in.getSQLTypeName() );

      System.out.println( "Array is of type code " +

                  p_in.getBaseType() );

      System.out.println( "Array is of length    " +

                  p_in.length() );

      }

       

      //code form asktom

      public static void

      pass_str_array( oracle.sql.ARRAY p_in, oracle.sql.ARRAY[] p_out )

      throws java.sql.SQLException,IOException{

       

       

      show_array_info( p_in );

      String[] values = (String[])p_in.getArray();

       

       

      for( int i = 0; i < p_in.length(); i++ )

      System.out.println( "p_in["+i+"] = " + values[i] );

       

       

      Connection conn = new OracleDriver().defaultConnection();

      ArrayDescriptor descriptor =

      ArrayDescriptor.createDescriptor( p_in.getSQLTypeName(), conn );

       

       

      p_out[0] = new ARRAY( descriptor, conn, values );

       

       

      }

       

       

      //code form mod of asktom

      //use array of number instead of string

      //it does not work

      public static void

      prime_array( oracle.sql.ARRAY p_in, oracle.sql.ARRAY[] p_out )

      throws java.sql.SQLException,IOException{

       

       

      show_array_info( p_in );

      int[] values = (int[])p_in.getArray();

       

       

      //for( int i = 0; i < p_in.length(); i++ )

      //System.out.println( "p_in["+i+"] = " + values[i] );

       

       

      Connection conn = new OracleDriver().defaultConnection();

      ArrayDescriptor descriptor =

      ArrayDescriptor.createDescriptor( p_in.getSQLTypeName(), conn );

       

       

      Sieve s2 = new Sieve();

      List<Integer> t2 = s2.sieve_of_eratosthenes(p_in.length());

       

       

      Integer[] intArray = new Integer[p_in.length()];

       

       

      int k=0;

               for (ListIterator<Integer> it = t2.listIterator(); it.hasNext(); ) {

           intArray[k]=it.next();

           System.out.println(intArray[k]);

           k++;

       

       

       

       

        }

      p_out[0] = new ARRAY( descriptor, conn, intArray );

       

       

      }//end_prime_array

       

       

      public static String return_string(){

      return "Hello World";

      }

       

       

       

      }//end_Sieve

       

      --wraper for the java

       

      CREATE OR REPLACE TYPE NUMARRAY AS TABLE OF number

      /

       

      CREATE OR REPLACE TYPE STRARRAY AS TABLE OF VARCHAR2 (255)

      /

       

      CREATE OR REPLACE PACKAGE PRIME_PKG

      AS

         -- Varchar2's are most easily mapped to the java String type

         PROCEDURE PASS (

            P_IN IN VARCHAR2,

            P_OUT OUT VARCHAR2)

         AS

            LANGUAGE JAVA

               NAME 'Sieve.pass( java.lang.String,

                                            java.lang.String[] )';

       

           PROCEDURE PASS (

              P_IN IN STRARRAY,

              P_OUT OUT STRARRAY)

           AS

              LANGUAGE JAVA

                 NAME 'Sieve.pass_str_array( oracle.sql.ARRAY,

                                                     oracle.sql.ARRAY[] )';

                                                    

         PROCEDURE Prime_array (

              P_IN IN NUMARRAY,

              P_OUT OUT NUMARRAY)

           AS

              LANGUAGE JAVA

                 NAME 'Sieve.prime_array( oracle.sql.ARRAY,

                                                     oracle.sql.ARRAY[] )';

       

       

         FUNCTION RETURN_STRING

            RETURN VARCHAR2

         AS

            LANGUAGE JAVA

               NAME 'Sieve.return_string() return 

                 java.lang.String';

      END PRIME_PKG;

      /

       

       

      --test case that works using string

      SET serveroutput on size 1000000                                               

      EXEC dbms_java.set_output( 1000000 )

       

       

      DECLARE                                                                        

         L_IN                          STRARRAY := STRARRAY ();

         L_OUT                         STRARRAY := STRARRAY ();

      BEGIN                                                                          

         FOR I IN 1 .. 5

         LOOP                                                                        

            L_IN.EXTEND;                                                             

            L_IN (I) := 'Element ' || I;

         END LOOP;                                                                   

                                                                                     

         prime_PKG.PASS (L_IN, L_OUT);

                                                                                     

         FOR I IN 1 .. L_OUT.COUNT                                                   

         LOOP

            DBMS_OUTPUT.PUT_LINE ('l_out('|| I || ') = ' || L_OUT (I) );

         END LOOP;                                                                   

      END;                                                                           

      /   

       

      --testcase does not work with integer/number

      SET serveroutput on size 1000000

      EXEC dbms_java.set_output( 1000000 )

       

       

      DECLARE

         L_IN                          numARRAY := numARRAY ();

         L_OUT                         numARRAY := numARRAY ();

         maxnum number := 1000;

      BEGIN

         --maxnum := 1000;

         --FOR I IN 1 .. 1000

         FOR I IN 1 ..maxnum

         LOOP

            L_IN.EXTEND;

            L_IN (I) := i; DBMS_OUTPUT.PUT_LINE ('l_in('|| I || ') = ' || L_in (I) );

       

       

            l_out.extend;

            l_out(i):=0;

            DBMS_OUTPUT.PUT_LINE ('l_out('|| I || ') = ' || L_OUT (I) );

       

       

       

       

         END LOOP;

       

      prime_pkg.prime_array(l_in,l_out);

       

       

         FOR I IN 1 ..maxnum

         LOOP

            DBMS_OUTPUT.PUT_LINE ('pass2 l_in('|| I || ') = ' || L_in (I) );

            DBMS_OUTPUT.PUT_LINE ('pass2 l_out('|| I || ') = ' || L_OUT (I) );

       

       

       

       

         END LOOP;

       

       

       

       

      END;

      /