99

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Jonas
  • 121,568
  • 97
  • 310
  • 388

5 Answers5

262

You can get columns number from ResultSetMetaData:

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

int columnsNumber = rsmd.getColumnCount();
1ac0
  • 2,875
  • 3
  • 33
  • 47
Roman
  • 64,384
  • 92
  • 238
  • 332
  • 2
    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 Apr 10 '10 at 19:06
  • @rhu That's easy. It wouldn't, because the metadata is independent of which row you're on. So presumably it is the maximum number of columns found. – user207421 May 05 '20 at 09:41
9
PreparedStatement ps=con.prepareStatement("select * from stud");

ResultSet rs=ps.executeQuery();

ResultSetMetaData rsmd=rs.getMetaData();

System.out.println("columns: "+rsmd.getColumnCount());  
System.out.println("Column Name of 1st column: "+rsmd.getColumnName(1));  
System.out.println("Column Type Name of 1st column: "+rsmd.getColumnTypeName(1)); 
user207421
  • 305,947
  • 44
  • 307
  • 483
lokesh
  • 107
  • 1
  • 1
  • 1
    Does somebody know? Is `rs.getMetaData()` method expensive? Does it query the database each time its called or not? – Fandi Susanto Jun 25 '17 at 15:08
  • The `rs.getMetaData()` call can be intensive; but once you have the object `rsmd` then usually no additional calls to the database are done when invoking methods on the metadata object; all the metadata is populated with the getMetaData call. – Mark Stewart May 02 '18 at 19:03
  • 2
    Shouldn't that be rsmd.getColumnName(1) and rsmd.getColumnTypeName(1) ? We're printing the "1st column", not the 2nd one. – DAB Feb 14 '19 at 10:20
5

Number of a columns in the result set you can get with code (as DB is used PostgreSQL):

//load the driver for PostgreSQL
Class.forName("org.postgresql.Driver");

String url = "jdbc:postgresql://localhost/test";
Properties props = new Properties();
props.setProperty("user","mydbuser");
props.setProperty("password","mydbpass");
Connection conn = DriverManager.getConnection(url, props);

//create statement
Statement stat = conn.createStatement();

//obtain a result set
ResultSet rs = stat.executeQuery("SELECT c1, c2, c3, c4, c5 FROM MY_TABLE");

//from result set give metadata
ResultSetMetaData rsmd = rs.getMetaData();

//columns count from metadata object
int numOfCols = rsmd.getColumnCount();

But you can get more meta-informations about columns:

for(int i = 1; i <= numOfCols; i++)
{
    System.out.println(rsmd.getColumnName(i));
}

And at least but not least, you can get some info not just about table but about DB too, how to do it you can find here and here.

1ac0
  • 2,875
  • 3
  • 33
  • 47
4

After establising the connection and executing the query try this:

 ResultSet resultSet;
 int columnCount = resultSet.getMetaData().getColumnCount();
 System.out.println("column count : "+columnCount);
Prabodh Hend
  • 1,321
  • 1
  • 12
  • 15
-1

This will print the data in columns and comes to new line once last column is reached.

ResultSetMetaData resultSetMetaData = res.getMetaData();
int columnCount = resultSetMetaData.getColumnCount();
for(int i =1; i<=columnCount; i++){
                if(!(i==columnCount)){

                    System.out.print(res.getString(i)+"\t");
                }
                else{
                    System.out.println(res.getString(i));
                }

            }