File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Recordset Get Fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Recordset Get Fields" Watch "Recordset Get Fields" New topic

Recordset Get Fields

Danie Van Eeden

Joined: Sep 03, 2003
Posts: 13
Hi, im using a microsoft Connection package to connect to MS SQL Server and im using MS J++ compiler.
After I declare a recordset and populate it with info from a database table, I would like to be able to get the field names from that recordset.
why? because im selecting all records from the database table and I dont know the field names. Ive seen the getField() method, but it only seems to get the value from that specific field, and I have no idea what the getFields() method (note the "s") does.
Im writing a small search engine thingy for a personal project, and
any help would be appreciated.

Regards<br />-dve83-
Arulkumar Gopalan
Ranch Hand

Joined: Oct 13, 2003
Posts: 104
If you want to get the column names you can use the interface ResultSetMetaData which has the required methods.getColumnName(int column), getColumnType(int column) .
Sample code:
ResultSet rsObj = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmdObj = rsObj.getMetaData();
int intNumberOfColumns = rsmdObj.getColumnCount();

for(int i=0;i<intNumberOfColumns;i++)
String strCoulmnName = rsmdObj.getcolumnName(i);

Is that what are you looking for?

Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
Jason Menard

Joined: Nov 09, 2000
Posts: 6450
I'm going to move this to the JDBC forum.
Danie Van Eeden

Joined: Sep 03, 2003
Posts: 13
hi and thanks, ill try that out and let know if im still struggling.
Danie Van Eeden

Joined: Sep 03, 2003
Posts: 13
hi , im back again.
what do i have to imoprt to get ResultSetMetaData as available class/type.
Im currently using* (i think that correctly spelled!?)
and does this resultsetmetadata idea support getting table names from the database as well. Ill most probably start messing about with and figure it out myself, but i just thought id ask.
thanks in advance
I agree. Here's the link:
subject: Recordset Get Fields
It's not a secret anymore!