Simple getColumnName(0) call throws Invalid column index: getValidColumnIndex

徘徊边缘 提交于 2020-04-06 04:47:06

问题


I'm trying to write a JTable that takes the data from a ResultSet and uses that to create a dynamic sized table with appropriate column names and row data values from the ResultSet but I can't get JDBC to get the column names for me dynamically.

I know my select statement is good! I can print the results out easily with my ResultPrinter class that I wrote but I can't seem to get the column names for some reason.

The code: http://pastebin.com/SSNdCkNu

The output:

Connected to DB!  
SNUM, SNAME, STATUS, CITY, SUPPLIERS_ID_SEQ // printed by static Suppliers class
Columns: 5 // result set shows there are 5 valid columns as expected
Exception in thread "main" java.sql.SQLException: Invalid column index: getValidColumnIndex
at oracle.jdbc.driver.OracleResultSetMetaData.getValidColumnIndex(OracleResultSetMetaData.java:138)
at oracle.jdbc.driver.OracleResultSetMetaData.getColumnName(OracleResultSetMetaData.java:306)
at Main.main(Main.java:15)

回答1:


JDBC column indexes start from 1 and not 0. As far as possible, it is better to retrieve data using column names to avoid hard dependency on the order of columns in the results.




回答2:


Column index starts by 1. So increase your variable pointing column variable by 1.



来源:https://stackoverflow.com/questions/12787909/simple-getcolumnname0-call-throws-invalid-column-index-getvalidcolumnindex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!