简体   繁体   English

java.sql.SQLSyntaxErrorException: ORA-01722

[英]java.sql.SQLSyntaxErrorException: ORA-01722

I have the below JDBC code:我有以下 JDBC 代码:

public int addItem(String name, String price, String count, String isPR, String img) {
    int result = 0;
    try {
        String query = "INSERT INTO T001_ITEM"
                + "(ITEM_NO,ITEM_NM,UNIT_PRICE,STOCK_COUNT,RECORD_DATE,IS_PR,ITEM_IMAGE_FILE_PATH)"
                + "VALUES(TABLE_SEQ.NEXTVAL,'" + name + "','" + price + "','" + count + "', sysdate,'" + isPR
                + "','" + img + "')";

        pstmt = conn.prepareStatement(query);
        result = pstmt.executeUpdate();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

It throws the below error:它抛出以下错误:

java.sql.SQLSyntaxErrorException: ORA-01722. java.sql.SQLSyntaxErrorException: ORA-01722。

How is this caused and how can I solve it?这是怎么引起的,我该如何解决?

Do not concatenate your values into your query like you do now with '" + name + "' , and to avoid SQL injection, I suggest you use a prepared statement with parameters:不要像现在使用'" + name + "'那样将您的值连接到您的查询中,并且为了避免 SQL 注入,我建议您使用带参数的准备好的语句:

String query = "INSERT INTO T001_ITEM "
        + "(ITEM_NO, ITEM_NM, UNIT_PRICE, STOCK_COUNT, RECORD_DATE, IS_PR, ITEM_IMAGE_FILE_PATH) "
        + "VALUES(TABLE_SEQ.NEXTVAL, ?, ?, ?, sysdate, ?, ?)";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, name);
pstmt.setString(2, price);
pstmt.setString(3, count);
pstmt.setString(4, isPR);
pstmt.setString(5, img);

Note笔记

Make sure the type of your attributes is same in your table, you set all your values as String.确保表中的属性类型相同,将所有值设置为字符串。 I don't think that the price can be a String or isPR seems to be a Boolean not a String , and the count can be a number not a String so check your types carefully.我不认为price可以是 String 或isPR似乎是Boolean而不是String ,并且count可以是数字而不是String所以请仔细检查您的类型。

If you can't change the type outside your method then you can cast it to the right type but you should to test them , this can make another problem.如果您无法在方法之外更改类型,那么您可以其转换为正确的类型,但您应该对其进行测试,这可能会产生另一个问题。

So for example所以例如

int countN = Integer.parseInt(count);

or或者

pstmt.setInt(3, Integer.parseInt(count));

and so on for all the other type.对于所有其他类型,依此类推。

Like what this documentation says here :就像这个文档在这里说的一样:

What causes this error?是什么导致了这个错误?

An ORA-01722 ("invalid number") error occurs when an attempt is made to convert a character string into a number, and the string cannot be converted into a valid number.尝试将字符串转换为数字时,会出现 ORA-01722(“无效数字”)错误,但无法将字符串转换为有效数字。 Valid numbers contain the digits '0' through '9', with possibly one decimal point, a sign (+ or -) at the beginning or end of the string, or an 'E' or 'e' (if it is a floating point number in scientific notation).有效数字包含数字“0”到“9”,可能有一个小数点,字符串开头或结尾的符号(+ 或 -),或者“E”或“e”(如果它是浮点数)以科学记数法表示的点数)。 All other characters are forbidden.禁止使用所有其他字符。

So like I said before, check if the type is exact in your table, you can't set a String in the place of Number所以就像我之前说的,检查你的表中的类型是否准确,你不能在Number的位置设置一个 String

You can learn more about this error here and here您可以在此处此处了解有关此错误的更多信息

Sorry i don't have enough reputations to add this as comment,抱歉,我没有足够的声誉将其添加为评论,

ORA-01722: Simply means it is unable to convert the String values to the integer. ORA-01722:仅表示它无法将字符串值转换为整数。

By looking at your what i can understand is you are passing price and count as string to the DB, can you check what is the declaration of the fields in DB.通过查看您的内容,我可以理解您正在将价格和计数作为字符串传递给数据库,您能否检查数据库中字段的声明是什么。 You need to convert them to int and then you can try insertion.您需要将它们转换为 int 然后您可以尝试插入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 --> java.sql.SQLSyntaxErrorException: ORA-01722: 无效号码 - ERROR --> java.sql.SQLSyntaxErrorException: ORA-01722: invalid number 问题休眠(java.sql.SQLSyntaxErrorException:ORA-01722:无效的数字) - Issue hibernate (java.sql.SQLSyntaxErrorException: ORA-01722: invalid number) java.sql.SQLSyntaxErrorException:ORA-01722:number_where IN无效 - java.sql.SQLSyntaxErrorException: ORA-01722: invalid number_where IN java.sql.SQLSyntaxErrorException:ORA-01722:refcursor上的resultSet.next()时数字无效,我不明白为什么? - java.sql.SQLSyntaxErrorException: ORA-01722: invalid number while resultSet.next() on refcursor and I don't understand why? java.sql.SQLSyntaxErrorException:ORA-01747 - java.sql.SQLSyntaxErrorException: ORA-01747 java.sql.SQLSyntaxErrorException: - java.sql.SQLSyntaxErrorException: java.sql.SQLSyntaxErrorException:ORA-00911:无效字符 - java.sql.SQLSyntaxErrorException: ORA-00911: invalid character java.sql.SQLSyntaxErrorException:ORA-00903:表名无效 - java.sql.SQLSyntaxErrorException: ORA-00903: invalid table name java.sql.SQLSyntaxErrorException:ORA-00936:缺少表达式 - java.sql.SQLSyntaxErrorException: ORA-00936: missing expression java.sql.SQLSyntaxErrorException:ORA-00904:“columnName”:无效标识符 - java.sql.SQLSyntaxErrorException: ORA-00904: “columnName”: invalid identifier
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM