views:

155

answers:

1

I am using CsvJdbc (it is a JDBC-driver for csv-files) to access a csv-file. I don't know how many columns the csv-file contains. How can I get the number of columns? Is there any JDBC-function for this? I can not find any methods for this in java.sql.Resultset.

For acessing the file, I use code similar to the example on the CsvJdbc website.

+4  A: 

You can get columns number from ResultSetMetaData:

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();

int columnsNumber = rsmd.getColumnCount();
Roman
It would be interesting to understand how the CSV JDBC driver and it's `ResultSetMetaData` implementation handles variable length CSV records. e.g. If you specified `SELECT * FROM sample` and each row contained a different number of fields, would the column count get re-evaluated for each row that were iterated over?
rhu
@rhu: I tested and the column count is not re-evaluated. @Roman: thanks for a good answer!
Jonas