This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

how can i get column name of a table  RSS feed

 
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
in one of my project,i want to get the column names of a unknown table,i can get it by the ResultSetMetaData.getColumnCount(int col) method,but i must execute a query first,because i know nothing about the table,the only choice is "select * from myTable",then navigate the resultset.but maybe this table have thousands of records,i only want to get the COLUMN NAME,not the record,so this method is not effective,who can give me a more effective code snippet?such as retrieve the column names of the emp table(in oracle sample database-----scott/tiger).
thank you!
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you may concentrate on the part of ResultSetMetaData instance:
public class odbctest
{
public static void main(String[] argu)
{
try
{
Connection conn;
Statement st;
ResultSet rs;
ResultSetMetaData rsmd;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
conn = DriverManager.getConnection("jdbc dbc:book");
st = conn.createStatement();
rs = st.executeQuery("select * from [Sheet1$]");
rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
System.out.println(count);
String[] field = new String[count];
if(count != 0)
{
for(int i=0; i<count; i++)
{
field[i] = rsmd.getColumnName(i+1);
//System.out.println(s);
}
}
rs = st.executeQuery("select * from [Sheet1$]");
while(rs.next())
{
String s = rs.getString(1);
System.out.println(s);
}
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Zhebin,
Have you seen the answers that I (and Elango) have provided to this very same question that you posted at the Oracle (OTN) forums?
Good Luck,
Avi.
 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to Calvin:
i don't want to execute such operation:
rs = st.executeQuery("select * from [Sheet1$]");
because i only want to get the column name,not any record in the table,such a operation maybe more resource-consuming,there may be millions of record in the table.

to Avi:
could you give me the CONCRETE link to the post,the link you have provided is pointed to the main page of the otn forum.
 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Avi
i have got it,that is just me,it is funny.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One possible way to "cheat"--and I've tested this against several different databases--is to force the query to return no rows, something like:
rs = st.executeQuery("select * from [Sheet1$] where 1 = 2");
I've tested this against tables with over one million records and it returned almost immediately. But I still got the ResultSetMetaData and was able to determine the column names and types.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!