sql server 2008无效的列索引0

时间:2017-05-09 12:16:49

标签: java sql-server

  results = statement.executeQuery("SELECT * FROM " + database + ".dbo." + tablename);
  ResultSetMetaData metaData = results.getMetaData();

  if (metaData != null) {
        System.out.println(database + ".dbo." + tablename+"ColumnCount()"+metaData.getColumnCount());
            for (int i = 1; i < metaData.getColumnCount(); i++) {
                columnMap.put(metaData.getColumnName(i), metaData.getColumnTypeName(i));
            }
        }

1 个答案:

答案 0 :(得分:0)

您收到该错误是因为ResultSetMetaData是基于1的列表。

将您的代码更改为:

 results = statement.executeQuery("SELECT * FROM " + database + ".dbo." + tablename);
  ResultSetMetaData metaData = results.getMetaData();

  if (metaData != null) {
        System.out.println(database + ".dbo." + tablename+"ColumnCount()"+metaData.getColumnCount());
            for (int i = 1; i <= metaData.getColumnCount(); i++) {
                columnMap.put(metaData.getColumnName(i), metaData.getColumnTypeName(i));
            }
        }

以下是使用ResultSetMetaData的示例:

https://examples.javacodegeeks.com/core-java/sql/resultsetmetadata/java-sql-resultsetmetadata-example/