将int数组中的每个值插入数据库 - Java

时间:2022-09-11 13:42:09

What i have is a multi-select Jlist box which the users selects several features. I grab the ID of these and store them into an int[] array.

我所拥有的是一个多选Jlist框,用户可以选择多个功能。我抓住这些ID并将它们存储到int []数组中。

What i am trying to do with these is insert them into my database base as below. But this is causing a

我想用这些做的是将它们插入到我的数据库库中,如下所示。但这导致了

 java.sql.SQLException: ORA-01722: invalid number

exception to appear. The line in question is the point at which the statement is executed. Ive checked the array isn't null and produces the correct values. I am unsure what would be causing this error.

出现异常。有问题的行是执行语句的点。我检查过数组不是null并产生正确的值。我不确定会导致此错误的原因。

 for (int i = 0; i < features.length; i++) {
        try {
            String strQuery = "INSERT INTO home_feature(home_id, feature_id) VALUES (?, ?)";
            PreparedStatement stmt = conn.prepareStatement(strQuery);//prepare the SQL Query
            stmt.setString(1, homeID);//insert homeid
            stmt.setInt(2, features[i]);//insert featureid.
            stmt.executeQuery();//execute query

            dataAdded = true;//data successfully inserted

        } catch (Exception e) {
            e.printStackTrace();
            dataAdded = false;//there was a problem, data not inserted
        }//end try
    }

Am I inserting the list of values correctly? Or should I be approaching this from a different angle?

我正确插入值列表吗?或者我应该从另一个角度接近这个?

2 个解决方案

#1


0  

Looks like you are passing a non valid number in query. Check the values of homeID and features[i].

看起来您在查询中传递了无效的数字。检查homeID和features [i]的值。

ORA-01722 Cause:

The attempted conversion of a character string to a number failed because the character string was not a valid numeric literal. Only numeric fields or character fields containing numeric data may be used in arithmetic functions or expressions. Only numeric fields may be added to or subtracted from dates.

尝试将字符串转换为数字失败,因为字符串不是有效的数字文字。只有包含数字数据的数字字段或字符字段可用于算术函数或表达式。只有数字字段可以添加到日期或从日期中减去。

Action:

Check the character strings in the function or expression. Check that they contain only numbers, a sign, a decimal point, and the character "E" or "e" and retry the operation.

检查函数或表达式中的字符串。检查它们是否仅包含数字,符号,小数点以及字符“E”或“e”,然后重试该操作。

#2


0  

One flaw I see is:

我看到的一个缺陷是:

stmt.executeQuery();    // execute query

should be

stmt.executeUpdate();   // execute query

When executing DML (Data Manipulation queries) you should use PreparedStatement#executeUpdate()

执行DML(数据操作查询)时,您应该使用PreparedStatement #cuteUpdate()

#1


0  

Looks like you are passing a non valid number in query. Check the values of homeID and features[i].

看起来您在查询中传递了无效的数字。检查homeID和features [i]的值。

ORA-01722 Cause:

The attempted conversion of a character string to a number failed because the character string was not a valid numeric literal. Only numeric fields or character fields containing numeric data may be used in arithmetic functions or expressions. Only numeric fields may be added to or subtracted from dates.

尝试将字符串转换为数字失败,因为字符串不是有效的数字文字。只有包含数字数据的数字字段或字符字段可用于算术函数或表达式。只有数字字段可以添加到日期或从日期中减去。

Action:

Check the character strings in the function or expression. Check that they contain only numbers, a sign, a decimal point, and the character "E" or "e" and retry the operation.

检查函数或表达式中的字符串。检查它们是否仅包含数字,符号,小数点以及字符“E”或“e”,然后重试该操作。

#2


0  

One flaw I see is:

我看到的一个缺陷是:

stmt.executeQuery();    // execute query

should be

stmt.executeUpdate();   // execute query

When executing DML (Data Manipulation queries) you should use PreparedStatement#executeUpdate()

执行DML(数据操作查询)时,您应该使用PreparedStatement #cuteUpdate()