4

Please find my below query,

select nvl(max(transaction_id),0) as  transaction_id from exception_details;

If I execute the above query through my jdbc code, it is giving me java.sql.SQLException: Fail to convert to internal representation my JDBC code is as follows:

public int fetchColumnVal(String query) throws SQLException, IllegalAccessException, 
    InvocationTargetException, NoSuchMethodException, ClassNotFoundException, InstantiationException {

    PreparedStatement pstmt = null;
    Connection con = null;
    try {
        con = getConnection(true);
        pstmt = con.prepareStatement(query);
        ResultSet rs = pstmt.executeQuery();
        rs.next();
        int count=rs.getInt(1);
        return count;
    } finally {
        if (isBatchMode) {
            this.cleanResources(null, pstmt);
        }
        else {
            this.cleanResources(con, pstmt);
        }
    }
}

and the data type for the column transaction_id in the table is NUMBER

4
  • Try using getBigDecimal or getLong instead of getInt. Commented Dec 23, 2013 at 12:22
  • I tried using getBigDecimal() its still giving me the same error and the output of the query is 0 so should i still try using getLong() Commented Dec 23, 2013 at 12:43
  • Could you please post the complete error trace? Commented Dec 23, 2013 at 14:32
  • 1
    The error suggests you're selecting a varchar2; can you double-check that the transaction_id is actually defined as number (I know you already said that, but still...), and that this is actually the query that's causing the error? Can you at least log/display the value of query before you execute it to verify what you're running? (It can't be exactly that anyway - the trailing semicolon would cause an error). Commented Dec 23, 2013 at 14:57

4 Answers 4

3

if you use @Enumerated without define EnumType.STRING, i.e @Enumerated(EnumType.STRING) and your table already contain String data (Varchar), you shall get this error, cos. default EnumType is ORDINAL, i.e EnumType.ORDINAL.

Sign up to request clarification or add additional context in comments.

Comments

0

SQL JDBC/Java setXXX getXXX

NUMERIC java.math.BigDecimal setBigDecimal getBigDecimal

change rs.getInt(1); to rs.getBigDecimal(1);

(or) type cast number to integer in sql like below for oracle:

CAST(id AS integer)

3 Comments

Hi, as per your above solution i tried implementing the same in my code as BigDecimal count=rs.getBigDecimal(1); but it is still giving me the same exception
Can you type cast the column in select query to integer and leave the code rs.getInt(1); and try
Can you check the syntax of the nvl function, please refer below link for oracle :techonthenet.com/oracle/functions/nvl.php select cast(nvl(max(transaction_id),0) as integer) as transaction_id from exception_details; [also check the 1-st parameter of nvl function it expects string]
0

I would like to suggest a more traditional approach for ResultSet count checking. Initially I tried using:

       while (RS.next()) {
            RScount = RS.getInt(1);
            Set.add(RS.getString(1));
        }

But as my SQL result-set was String, Java kept on throwing:

java.sql.SQLException: Fail to convert to internal representation

Then I changed my code to to plain Vanilla: Declaration, initialized at 0:

Integer RScount = 0; //SQL result set counter

And counter code as:

    while (RS.next()) {
        RScount++;
        Set.add(RS.getString(1));
    }

Comments

0

I was getting error like

Exception Foundjava.sql.SQLException: Fail to convert to internal representation.

I have written my code as :

 while(res.next())  
        System.out.println(  res.getInt(1) + "  "
                           + res.getString(2) + "  "
                           + res.getString(3) + "  "
                           + res.getString(4));

but my datatype of 1st field in DB was of varchar type.

Then I changed my code to:

while(res.next())  
        System.out.println(  res.getString(1) + "  "
                           + res.getString(2) + "  "
                           + res.getString(3) + "  "
                           + res.getString(4));

then I got all my data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.