String quer="SELECT * FROM sample2 where 1=2";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(quer);
ResultSetMetaData rsmd = rs.getMetaData();
int NumOfCol=0;
NumOfCol=rsmd.getColumnCount();
System.out.println("Query Executed!! No of Colm="+NumOfCol);
//assumes you've already created a connection, opened it,
//and executed a query to a reader
while(reader.Read())
{
Object[] values = new Object[reader.FieldCount];
int fieldCount = reader.GetValues(values);
Console.WriteLine("\nreader.GetValues retrieved {0} columns.", fieldCount);
for (int i = 0; i < fieldCount; i++)
Console.WriteLine(values[i]);
}
SELECT MAX(ORDINAL_POSITION) NumberOfColumnsInTable
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'YourDatabaseNameHere'
AND
TABLE_SCHEMA = 'YourSchemaNameHere'
AND
TABLE_NAME = 'YourTableNameHere';