有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

下面的代码片段

    String query = " SELECT * FROM accessdb.user;";
    try
    {
        ResultSet rs = statement.executeQuery(query);
        rs.first();
        String s = "";
        do
        {

            s = s + rs.getInt(0) + "\t\t" + rs.getString(1) + "\t\t" 
                + rs.getString(2) + "\n";
            rs.next();
        }
        while (!rs.isLast());
        System.out.println(s);
    }
    catch (Exception ex)
    {
        System.out.println("\nError for firing query");
    }

生成以下异常

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

这是如何造成的,我如何解决


共 (2) 个答案

  1. # 1 楼答案

    rs.getInt(0)

    JDBC列索引从1开始。因此,您应该按如下方式检索数据:

    s = s + rs.getInt(1) + "\t\t" + rs.getString(2) + "\t\t" + rs.getString(3) + "\n";
    

    rs.get*()中使用列名要好得多,一般来说,它可以保护代码不受返回列顺序的任何更改

  2. # 2 楼答案

    Using the column names in rs.get*() is much better and shields your code from any change in the order of the columns returned, in general.

    我发现没有双引号的列名也会引发同样的错误