java 无法从结果集中读取列值;字符串索引超出范围:0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/161988/
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-10-29 11:14:56  来源:igfitidea点击:

could not read column value from result set ; String index out of range: 0

javahibernate

提问by Anthony Dahanne

I'm reading data from a table( from a MySQL Database) with Hibernate SQL Query. The thing is, the table contains a colum that is mapped to a char in Hibernate Model, and sometimes this column is empty. And I suppose this is where my exception comes from. How can I map a colum of char to my hibernate model without getting this error ? Thanks for your answers !

我正在使用 Hibernate SQL 查询从表(来自 MySQL 数据库)读取数据。问题是,该表包含一个映射到 Hibernate 模型中的字符的列,有时该列是空的。我想这就是我的例外的来源。如何将一列字符映射到我的休眠模型而不会出现此错误?感谢您的回答!



Thank you for your answer ! My column is not nullable (I 'm using MySQL and this column is NOT NULL) Then, I don't think that

谢谢您的回答 !我的列不可为空(我使用的是 MySQL 并且此列不是 NULL)然后,我不认为

if (str == null) {

is appropriate.

是合适的。

the error is :

错误是:

15:30:35,289  INFO CharacterType:178 - could not read column value from result set: LSFUS11_20_; String index out of range: 0

which results in the following exception :

这导致以下异常:

java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:558)

I think I may try your solution, but with :

我想我可以尝试您的解决方案,但是:

if (str == "") {

since it can't be null, it's just an empty String.

因为它不能为空,所以它只是一个空字符串。

Thanks for your piece code, I'm going to try that !

谢谢你的代码,我要试试!

回答by MetroidFan2002

I am assuming from your question that you're mapping this to a primitive character. Next time, please post the stacktrace that you receive (you may leave out where you call it, you could only include the hibernate stuff if your project is too sensitive).

我从你的问题中假设你将它映射到一个原始字符。下次,请发布您收到的堆栈跟踪(您可以省略调用它的位置,如果您的项目太敏感,您只能包含休眠的内容)。

If you do map to a primitive character, and it is null, you will get an exception, because primitives cannot have null assigned to them.

如果您确实映射到一个原始字符,并且它为空,您将得到一个异常,因为原始字符不能为它们分配空值。

This class will mitigate this, the "null" character is returned as a character representing "0". You can customize this to your liking:

此类将减轻这种情况,“空”字符作为表示“0”的字符返回。您可以根据自己的喜好自定义:

import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.type.CharacterType;

public class NullCharacterType extends CharacterType {

    /**
     * Serializable ID generated by Eclipse
     */
    private static final long serialVersionUID = 1L;

    public NullCharacterType() {
        super();
    }

    public Object get(final ResultSet rs, final String name)
            throws SQLException {
        final String str = rs.getString(name);
        if (str == null || str.length() == 0) {
            return new Character((char) 0);
        } else {
            return new Character(str.charAt(0));
        }
    }
}

To use this new type, in your hibernate mapping, before you had something like:

要使用这种新类型,在你的休眠映射中,在你有类似的东西之前:

<property name="theChar" type="character">

Now, you just specify the class name as your type:

现在,您只需将类名指定为您的类型:

<property name="theChar" type="yourpackage.NullCharacterType">

However, the best practice is to not use primitive types for database mapping. If at all possible, use Character instead of char, because that way you won't have an issue with null (null can be assigned to the wrapper types).

但是,最佳实践是不要将原始类型用于数据库映射。如果可能,请使用 Character 而不是 char,因为这样您就不会遇到 null 问题(可以将 null 分配给包装器类型)。

回答by srinivas

Search your JBoss (server) whether mysql.jar(mysql-connector-java-5.1.7-bin) is present in lib files or not. Even I faced the same problem, after adding the mysql.jarfile it is working fine.

搜索您的 JBoss(服务器)mysql.jar(mysql-connector-java-5.1.7-bin)是否存在于 lib 文件中。即使我遇到了同样的问题,在添加mysql.jar文件后它也能正常工作。

回答by jmpeace

use native function LEFT or RIGHT to change the column datatype in origin

使用本机函数 LEFT 或 RIGHT 更改原始列的数据类型

let's suppose this was the SQL query giving the error select username from Users

让我们假设这是 SQL 查询给出错误 select username from Users

change it with: select LEFT(username,100) from Users

更改为:从用户中选择LEFT(username, 100)

the number should be equal to the size of the field

该数字应等于字段的大小