Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JDBC Callable Statement Casting Exception

Posted on 2009-05-11
4
Medium Priority
?
824 Views
Last Modified: 2013-12-29
I am trying to execute an ORACLE Funtion using a JDBC Callable statement.  The function returns a Varchar.  When I execute, I get a ClassCastException.  The funtion statment and the method are both below.  What am I doing wrong, JDBC will handle a Varchar, correct?
Oracle Function: 
CREATE OR REPLACE FUNCTION OMSOWNER.PRC_CCA_DELETE_INMATE_FUNCTION (INJLOCAT VARCHAR2, BOOKNUM VARCHAR2, TICKETNUM VARCHAR2)
return varchar2
 
Code:
    public void execute(String jlocat, String booknum, String ticketnum)
        throws SQLException {
 
        String query = useOracleQuery ? oracleQuery : genericQuery;
        System.out.println("Query: " + query + "\n");
        CallableStatement stmt = conn.prepareCall(query);
 
        // register the type of the out param - an Oracle specific type
        stmt.registerOutParameter(1, OracleTypes.VARCHAR);
        //stmt.registerOutParameter(1, OracleTypes.CURSOR);
 
        // set the in param
        stmt.setString(2, jlocat);
        stmt.setString(3, booknum);
        stmt.setString(4, ticketnum);
 
        // execute and retrieve the result set
        stmt.execute();
        ResultSet rs = (ResultSet)stmt.getObject(1);
 
        // print the results
        while (rs.next()) {
            System.out.println(rs.getString(1));
            
        }
 
        rs.close();
        stmt.close();
    }

Open in new window

0
Comment
Question by:sf_gray
[X]
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
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24354325
What does the following give?
System.out.println(stmt.getObject(1).getClass());

Open in new window

0
 
LVL 12

Accepted Solution

by:
Gibu George earned 700 total points
ID: 24354735
You have registered out parameter as VARCHAR, then you are trying to cast the object to result set.
String rs = (String)stmt.getObject(1);
0
 
LVL 4

Assisted Solution

by:pellep
pellep earned 300 total points
ID: 24355071
What does the actualy query look like you're executing (String query = useOracleQuery ? oracleQuery : genericQuery)?

If it's something like:
"begin ? := OMSOWNER.PRC_CCA_DELETE_INMATE_FUNCTION(?,?,?); end;"
And the function returns a VARCHAR, then your code should work fine if you change
ResultSet rs = (ResultSet)stmt.getObject(1);
to
String s = stmt.getString(1);
(like gibu_george alerady suggested).

Also, if you're returning a simple VARCHAR, you shouldn't mess around with OracleTypes, just specify it as java.sql.Types.VARCHAR and let the JDBC driver worry about type marshalling.

If, on the other hand, you want a resultset to work with, the Oracle-function will have to return a REFCURSOR.
0
 

Author Closing Comment

by:sf_gray
ID: 31580134
Thank you, so much.  I really appreciate it.  I hope my point split was fair.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

704 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