Vinda
Vinda

Reputation: 223

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

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

Upvotes: 4

Views: 37100

Answers (4)

Mohammad Badiuzzaman
Mohammad Badiuzzaman

Reputation: 748

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.

Upvotes: 3

Navin Kumar
Navin Kumar

Reputation: 1

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.

Upvotes: 0

sumon c
sumon c

Reputation: 789

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));
    }

Upvotes: 0

Balaji Sengeni
Balaji Sengeni

Reputation: 254

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)

Upvotes: 0

Related Questions