以下Oracle错误意味着什么:无效的列索引

测试一些代码时出现以下错误:

SQLException:列索引无效

这到底是什么意思呢?

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

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

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

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

也许您正试图通过ORDER BY不存在的列?

SQL语句中的任何拼写错误?

使用Spring的SimpleJdbcTemplate,当我尝试这样做时,我得到了它:

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

     String sqlString = "select pwy_code from approver where university_id = ?"; 

我也遇到了这种类型的错误,问题就是错误地使用了参数语句之类的,比方说你有这样的查询

 SELECT * FROM EMPLOYE E WHERE E.ID = ? 

如果设置参数,则为preparedStatement对象(JDBC)

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

然后它会导致SQLException: Invalid column index

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

使用Spring Security 3.1.0时,我遇到了完全相同的问题。 和Oracle 11G。 我使用以下查询并获取无效列索引错误:

  

事实certificate,我需要在查询中添加:“1 as enabled”:

  

之后一切都有效。 我相信这可能是Spring JDBC核心包中的一个错误......

我使用准备好的声明遇到了这个问题。 我没有添加足够的“?” 对于“价值”我的日食在我添加了适量之后崩溃了,并且丢失了这些变化。 但是在我开始梳理SQL之前,我没有想到这是错误,因为p.campbell建议。

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

 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引起的’within; 这扰乱了语法。

最后的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的字符串不应该包含通配符周围的引号?,也不应该用semicolumn。

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

"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 )

我有以下结构的oracle表

 SQL> desc demo Name Null? Type ----------------------------------------- -------- ------------ ID NUMBER(38) NAME VARCHAR2(20) SALARY NUMBER(6) **************************** 

我试图用下面的代码插入值并得到错误

 **************************** PreparedStatement stmt=con.prepareStatement("update demo set salary=? where id=?" ); stmt.setInt(3,288800); stmt.setInt(1,8); ************************ 

SQLException:列索引无效

正确的代码是

 ************************ PreparedStatement stmt=con.prepareStatement("update demo set salary=? where id=?" ); stmt.setInt(1,288800); stmt.setInt(2,8); ******************* stmt.setInt(1,288800);//1 represents salary ie first '?' stmt.setInt(2,8);//2 represents id ie second '?' 

这里1实际上表示preparestatement查询中的列号而不是数据库表中的列号

希望这可以帮助..

  [1]: http://sofzh.miximages.com/java/vXvMA.png