以下 Oracle 错误是什么意思:无效的列索引

发布于 2024-11-07 06:57:16 字数 131 浏览 2 评论 0原文

我在测试一些代码时遇到以下错误:

SQLException:无效的列索引

到底是什么意思?

有没有在线文档解释所有Oracle错误代码和语句?

I got the following error while testing some code:

SQLException: Invalid column index

What exactly does that mean?

Is there an online document explaining what all the Oracle error codes and statements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

救星 2024-11-14 06:57:17

我在使用 Spring Security 3.1.0 时遇到了完全相同的问题。和甲骨文11G。我正在使用以下查询并收到无效的列索引错误:

<security:jdbc-user-service data-source-ref="dataSource"
                users-by-username-query="SELECT A.user_name AS username, A.password AS password FROM MB_REG_USER A where A.user_name=lower(?)"

事实证明,我需要在查询中添加:“1 asenabled”:

<security:jdbc-user-service data-source-ref="dataSource" users-by-username query="SELECT A.user_name AS username, A.password AS password, 1 as enabled FROM MB_REG_USER A where A.user_name=lower(?)"

此后一切正常。我相信这可能是 Spring JDBC 核心包中的一个错误......

I had the exact same problem when using Spring Security 3.1.0. and Oracle 11G. I was using the following query and getting the invalid column index error:

<security:jdbc-user-service data-source-ref="dataSource"
                users-by-username-query="SELECT A.user_name AS username, A.password AS password FROM MB_REG_USER A where A.user_name=lower(?)"

It turns out that I needed to add: "1 as enabled" to the query:

<security:jdbc-user-service data-source-ref="dataSource" users-by-username query="SELECT A.user_name AS username, A.password AS password, 1 as enabled FROM MB_REG_USER A where A.user_name=lower(?)"

Everything worked after that. I believe this could be a bug in the Spring JDBC core package...

渔村楼浪 2024-11-14 06:57:17

最终的 sql 语句类似于:

select col_1 from table_X where col_2 = 'abcd';

我在 SQL IDE 中运行它,一切正常。

接下来,我尝试使用 java 构建此语句:

String queryString= "select col_1 from table_X where col_2 = '?';";
PreparedStatement stmt = con.prepareStatement(queryString);
stmt.setString(1, "abcd"); //raises java.sql.SQLException: Invalid column index

虽然 sql 语句(第一个针对数据库运行的语句)包含字符串值周围的引号,并且还以半列结束,但我传递给PreparedStatement 的字符串不应包含引号围绕通配符 ? ,也不应该以半栏结束。

我刚刚删除了白色背景上出现的字符

"select col_1 from table_X where col_2 = ' ? ' ; ";

获取

"select col_1 from table_X where col_2 = ?";

(我在这里找到了解决方案:https://coderanch.com/t/424689/databases/java-sql-SQLException-Invalid-column

the final sql statement is something like:

select col_1 from table_X where col_2 = 'abcd';

i run this inside my SQL IDE and everything is ok.

Next, i try to build this statement with java:

String queryString= "select col_1 from table_X where col_2 = '?';";
PreparedStatement stmt = con.prepareStatement(queryString);
stmt.setString(1, "abcd"); //raises java.sql.SQLException: Invalid column index

Although the sql statement (the first one, ran against the database) contains quotes around string values, and also finishes with a semicolumn, the string that i pass to the PreparedStatement should not contain quotes around the wildcard character ?, nor should it finish with semicolumn.

i just removed the characters that appear on white background

"select col_1 from table_X where col_2 = ' ? ' ; ";

to obtain

"select col_1 from table_X where col_2 = ?";

(i found the solution here: https://coderanch.com/t/424689/databases/java-sql-SQLException-Invalid-column)

公布 2024-11-14 06:57:17

我在一个动态创建准备好的语句的遗留应用程序中遇到了这个问题。

String firstName;
StringBuilder query =new StringBuilder("select id, name from employee where country_Code=1");
query.append("and  name like '");
query.append(firstName + "' ");
query.append("and ssn=?");
PreparedStatement preparedStatement =new prepareStatement(query.toString());

当它尝试为ssn设置值时,它给出了无效的列索引错误,最后发现这是由于firstName中包含'引起的;扰乱语法。

I had this problem in one legacy application that create prepared statement dynamically.

String firstName;
StringBuilder query =new StringBuilder("select id, name from employee where country_Code=1");
query.append("and  name like '");
query.append(firstName + "' ");
query.append("and ssn=?");
PreparedStatement preparedStatement =new prepareStatement(query.toString());

when it try to set value for ssn, it was giving invalid column index error, and finally found out that it is caused by firstName having ' within; that disturb the syntax.

凤舞天涯 2024-11-14 06:57:16

如果这是 Java 抛出的 SQLException,则很可能是因为您尝试从 ResultSet 获取或设置值,但您使用的索引不在范围内。

例如,您可能尝试从结果集中获取索引 3 处的列,但 SQL 查询只返回两列。

If that's a SQLException thrown by Java, it's most likely because you are trying to get or set a value from a ResultSet, but the index you are using isn't within the range.

For example, you might be trying to get the column at index 3 from the result set, but you only have two columns being returned from the SQL query.

梦罢 2024-11-14 06:57:16

听起来您正在尝试 SELECT 一个不存在的列。

也许您正在尝试ORDER BY一个不存在的列?

您的 SQL 语句中是否有拼写错误?

It sounds like you're trying to SELECT a column that doesn't exist.

Perhaps you're trying to ORDER BY a column that doesn't exist?

Any typos in your SQL statement?

柠檬 2024-11-14 06:57:16

使用 Spring 的 SimpleJdbcTemplate,当我尝试执行此操作时,我得到了它:

String sqlString = "select pwy_code from approver where university_id = '123'";
List<Map<String, Object>> rows = getSimpleJdbcTemplate().queryForList(sqlString, uniId);
  • 我有一个 queryForList 的参数,该参数与 SQL 中的问号不对应。第一行应该是:

    String sqlString = "从审批者中选择 pwy_code,其中 University_id = ?";
    

Using Spring's SimpleJdbcTemplate, I got it when I tried to do this:

String sqlString = "select pwy_code from approver where university_id = '123'";
List<Map<String, Object>> rows = getSimpleJdbcTemplate().queryForList(sqlString, uniId);
  • I had an argument to queryForList that didn't correspond to a question mark in the SQL. The first line should have been:

    String sqlString = "select pwy_code from approver where university_id = ?";
    
时光与爱终年不遇 2024-11-14 06:57:16

我也遇到了这种类型错误,问题是对语句的参数使用错误,例如,假设您有一个这样的查询,

SELECT * FROM EMPLOYE E WHERE E.ID = ?

并且对于准备好的语句对象(JDBC),如果您设置像这样的参数

preparedStatement.setXXX(1,value);
preparedStatement.setXXX(2,value)

,那么它会导致 SQLException:无效列因此

,我删除了准备好的语句的第二个参数设置,然后问题就解决了

I also got this type error, problem is wrong usage of parameters to statement like, Let's say you have a query like this

SELECT * FROM EMPLOYE E WHERE E.ID = ?

and for the preparedStatement object (JDBC) if you set the parameters like

preparedStatement.setXXX(1,value);
preparedStatement.setXXX(2,value)

then it results in SQLException: Invalid column index

So, I removed that second parameter setting to prepared statement then problem solved

沧桑㈠ 2024-11-14 06:57:16

只需尝试此修复,因为我遇到了您的错误:

删除问号周围的单引号,这意味着,如果您使用了保留参数,例如 ('?','?','?' )你应该让它看起来像这样:

(?,?,?)

Just try this fix, as I faced your error:

Remove the single quotation marks around your question mark, which means, if you used your reserved parameters like ('?','?','?') you should make it look like this:

(?,?,?)
别再吹冷风 2024-11-14 06:57:16

我使用准备好的声明时遇到了这个问题。我没有添加足够的“?”对于“VALUES”,在我添加适当的数量后,我的日食崩溃了,并丢失了这些更改。但直到我按照 p.campbell 的建议开始梳理 SQL 时,我才意识到这是错误。

I had this problem using a prepared statement. I didn't add enough "?" for the "VALUES" My eclipse had crashed after I did add the proper amount, and lost those changes. But that didn't occur to me to be the error until I started combing through the SQL as p.campbell suggested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文