Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the number of columns from a JDBC ResultSet?

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 accessing the file, I use code similar to the example on the CsvJdbc website.

like image 329
Jonas Avatar asked Apr 10 '10 18:04

Jonas


People also ask

Which function will return number of columns in ResultSet?

Use PDOStatement::columnCount() to return the number of columns in the result set represented by the PDOStatement object. If the PDOStatement object was returned from PDO::query(), the column count is immediately available.

How do I get a list of column names in ResultSet?

You can get the name of a particular column using the getColumnName() method of the ResultSetMetadata interface. This method accepts an integer value representing the index of a column and returns a String value representing the name of the specified column.

How do I find the number of records in ResultSet?

Getting the number of rows using methodsThe last() method of the ResultSet interface moves the cursor to the last row of the ResultSet and, the getRow() method returns the index/position of the current row.

How do you determine the size of a ResultSet?

Simply iterate on ResultSet object and increment rowCount to obtain size of ResultSet object in java. rowCount = rs. getRow();


1 Answers

You can get columns number from ResultSetMetaData:

Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(query); ResultSetMetaData rsmd = rs.getMetaData();  int columnsNumber = rsmd.getColumnCount(); 
like image 178
Roman Avatar answered Oct 18 '22 09:10

Roman