java.sql.SQLException:无法转换为内部表示异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20743263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:00:55  来源:igfitidea点击:

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

javasqloraclejdbc

提问by Vinda

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 representationmy JDBC code is as follows:

如果我通过我的 jdbc 代码执行上面的查询,它给了我java.sql.SQLException: Fail to convert to internal representation我的 JDBC 代码如下:

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

表中transaction_id列的数据类型为NUMBER

回答by Balaji Sengeni

SQL JDBC/Java setXXX getXXX

SQL JDBC/Java setXXX getXXX

NUMERIC java.math.BigDecimal setBigDecimal getBigDecimal

NUMERIC java.math.BigDecimal setBigDecimal getBigDecimal

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

改变 rs.getInt(1); 到 rs.getBigDecimal(1);

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

(或)在 sql 中将数字类型转换为整数,如下面的 oracle:

CAST(id AS integer)

CAST(id AS 整数)

回答by sumon c

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

我想建议一种更传统的 ResultSet 计数检查方法。最初我尝试使用:

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

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

但是由于我的 SQL 结果集是字符串,Java 继续抛出:

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

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

然后我将代码更改为普通的 Vanilla:声明,初始化为 0:

Integer RScount = 0; //SQL result set counter

And counter code as:

计数器代码为:

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

回答by Navin Kumar

I was getting error like

我收到错误,例如

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

异常 Foundjava.sql.SQLException:无法转换为内部表示。

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.

但是我在 DB 中的第一个字段的数据类型是 varchar 类型。

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.

然后我得到了我所有的数据。

回答by Mohammad Badiuzzaman

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.

如果你使用@Enumerated 而不定义 EnumType.STRING,即 @Enumerated(EnumType.STRING) 并且你的表已经包含字符串数据 (Varchar),你会得到这个错误,cos。默认 EnumType 是 ORDINAL,即 EnumType.ORDINAL。