java.sql.SQLException:[Microsoft][ODBC 驱动程序管理器] 无效的描述符索引

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

java.sql.SQLException:[Microsoft][ODBC Driver Manager] Invalid descriptor index

javajdbc

提问by Tepken Vannkorn

I use the following code

我使用以下代码

try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:access");
    String sql = "Select * from table";
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery( sql );
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    for (int i = 1; i <= columns; i++) {
        columnNames.addElement( md.getColumnName(i) );
    }
    while (rs.next()) {
        Vector row = new Vector(columns);
        for (int i = 1; i <= columns; i++){
            row.addElement( rs.getObject(i) );
        }
        data.addElement( row );
    }
    rs.close();
    stmt.close();
}catch(Exception e){
    System.out.println(e);
}

It displays:

它显示:

java.sql.SQLException:[Microsoft][ODBC Driver Manager] Invalid descriptor index

How is this caused and how can I solve it?

这是怎么引起的,我该如何解决?

回答by Vineet Reynolds

I doubt the exception is thrown by one of the lines in the posted code. I have my reasons to state so.

我怀疑异常是由发布的代码中的一行引发的。我有理由这么说。

A SQLException with the message "Invalid descriptor index" is usually obtained when you read the result set incorrectly. There are various ways in which this scenario can manifest:

当您错误地读取结果集时,通常会获得带有消息“无效描述符索引”的 SQLException。这种情况可以通过多种方式表现出来:

  • Reading columns out of sequence. I'm afraid, some JDBC drivers will require you to read columns in order, starting at the first column. That's the way some drivers have been written; you cannot skip any columns when reading the resulting result set, as the drivers are actually reading a stream and converting objects in the stream to objects of the JDBC types.
  • You might be reading a column, whose index is invalid, or whose column name doesn't match any of the returned columns in the result set. The simple resolution is to either fix the query to return the needed column, or fix your code to not read the absent column.
  • 不按顺序读取列。恐怕有些 JDBC 驱动程序会要求您从第一列开始按顺序阅读列。这就是一些驱动程序的编写方式;在读取结果集时不能跳过任何列,因为驱动程序实际上是在读取流并将流中的对象转换为 JDBC 类型的对象。
  • 您可能正在读取一个索引无效的列,或者其列名与结果集中返回的任何列都不匹配。简单的解决方法是修复查询以返回所需的列,或者修复您的代码以不读取不存在的列。

If you need to solve it, you need to know which one of the above conditions is true in your code, and rectify accordingly.

如果您需要解决它,您需要知道您的代码中上述哪一个条件为真,并进行相应的纠正。

回答by Vineet Reynolds

That will occur if you're trying to get the resultset variable value in the index value of 0. For example: Consider a table which has 5 columns:

如果您尝试在索引值 0 中获取结果集变量值,就会发生这种情况。例如:考虑一个有 5 列的表:

ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName);
while(rs.next())
{
   for(int i=0;i<5;i++)
   //This will throw the exception
   System.out.println(rs.getString(i)); //Since the value will be returned from 1 not 0
   //The below code was the right way
   System.out.println(rs.getString(i+1));
}

回答by Remco

I have had the same exact error, this out of an ODBC Express Driver for Delphi.

我有同样的错误,这是来自 Delphi 的 ODBC Express 驱动程序。

The solution I have found is:

我找到的解决方案是:

Place your varchar(max) and or varbinary(max) fields at the end of your select Query. (Order in the table definition doesn't matter).

将您的 varchar(max) 和/或 varbinary(max) 字段放在您选择的查询的末尾。(表定义中的顺序无关紧要)。

This really fixed it for us, thought to share it with you guys.

这确实为我们解决了这个问题,想与你们分享。

回答by Shashika Fernando

I got an error

我有一个错误

SEVERE: null java.sql.SQLException: [Microsoft][SQL Server Native Client 10.0]Invalid Descriptor Index

严重:null java.sql.SQLException:[Microsoft][SQL Server Native Client 10.0]无效的描述符索引

code was

代码是

String sqlStr = "select soldItems.payment as aa, Sysuser.name as sname, Books.Name as abookName, soldItems.Qunt as qunt, soldItems.date as soldBooks from Sysuser inner join soldItems on soldItems.CustomerId=Sysuser.id inner join Books on Books.bookId=soldItems.bookId where CustomerId='" + cusId + "' and PaymentDone is NULL";
    System.out.println(sqlStr);
    DbConnection con = new DbConnection();
    con.getConnection();
    ResultSet rs = con.getData(sqlStr);
    while (rs.next()) {
        int i = 0;


        dataArry[i][0] = rs.getString("abookName");
        dataArry[i][1] = rs.getString("qunt");
         dataArry[i][2] = rs.getString("aa");
        dataArry[i][3] = rs.getString("soldBooks");

        i++;
    }

the fix is rs.getString needs to be in the same order of the SQL

修复是 rs.getString 需要与 SQL 的顺序相同

so the code needs to be

所以代码需要

       dataArry[i][2] = rs.getString("aa");
        dataArry[i][0] = rs.getString("abookName");
        dataArry[i][1] = rs.getString("qunt");

        dataArry[i][3] = rs.getString("soldBooks");

回答by Hink

I know this bug for many years by using ODBC driver with PHP. Try to place your text and image columns at the end of select list.
Dont use

通过将 ODBC 驱动程序与 PHP 一起使用,我知道这个错误很多年了。尝试将文本和图像列放在选择列表的末尾。
不要使用

select * from t

but enumerate rigidly

但要一一列举

select plain_column1, plain_column2, .... image_column from t

Unfortunately Microsoft doesn't get tired by fixing the bug. JDBC driver works OK.

不幸的是,微软并没有因为修复这个错误而感到厌烦。JDBC 驱动程序工作正常。