aspose file tools*
The moose likes JDBC and the fly likes How to find the column names..??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to find the column names..???" Watch "How to find the column names..???" New topic
Author

How to find the column names..???

rejieve alexander
Greenhorn

Joined: Dec 10, 2000
Posts: 22
Hi,
Could someone help me with a code in retrieving the names of
the columns, of the tables in a database...
I found out how to get the table names ...but not the column names..
Please help...thanks in advance
Dharmesh Chheda
Ranch Hand

Joined: May 08, 2001
Posts: 204
hi rejieve
In order to get the column info n stuff use the ResultSetMetaData object
we obtain the ResultSetMetaData object from the ResultSet using the getMetaData() method. We can use the object to discover the number and type of columns and the names of each column.
the methods which can be used for column info are:
getColumnCount(), getColumnName(int), getColumnLabel(int), getColumnType(int)

------------------
IBM Certified WebSphere Application Server V3.5 Specialist
[This message has been edited by Dharmesh Chheda (edited October 24, 2001).]


regards,<br />Dharmesh Chheda
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
Here is an example...
ResultSetMetaData metadata = yourResultset.getMetaData();
for(int column = 1; column <= columnCount; column++)
{
System.out.print(metadata.getColumnLabel(column) + "\t");
}

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
rejieve alexander
Greenhorn

Joined: Dec 10, 2000
Posts: 22
Thanks dharmesh and bosun,
i understood the fact of getting a Resultmetadata object,but
what should i declare my ResultSet as..
If possible could u write me a code as from ....
ResulSet rs; onwards....
Thanks again..
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
Rajieve, I think it will be beneficial if you check out the JDBC turorial at Sun's site, or get a book that has a something on JDBC. Here you go...

sqltxt = "put your select statement here"

Statement stmt = YourConnection.createStatement();
ResultSet rs = stmt.executeQuery(sqltxt);
I hope you get it from there.
Bosun
A Hillman
Greenhorn

Joined: Oct 25, 2001
Posts: 2
You should be able to use DatabaseMetaData.getColumns() which is defined in the JDBC Spec. The documentation can be seen at this link. java.lang.String," TARGET=_blank rel="nofollow">http://java.sun.com/j2se/1.3/docs/api/java/sql/DatabaseMetaData.html#getColumns(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
Is this enough info, or do you want a functional code sample?
Originally posted by rejieve alexander:
Hi,
Could someone help me with a code in retrieving the names of
the columns, of the tables in a database...
I found out how to get the table names ...but not the column names..
Please help...thanks in advance

rejieve alexander
Greenhorn

Joined: Dec 10, 2000
Posts: 22
Hi again,
This is the code which i've written to get the column name...
try{
DatabaseMetaData myMT = con.getMetaData();
String[] myTables = {"TABLE"};
ResultSet tables = myMT.getTables(null,
null, "%", myTables);
String tableName = null;
while (tables.next()){
tableName = tables.getString("TABLE_NAME");
ResultSetMetaData metadata = tables.getMetaData();
columnCount = metadata.getColumnCount();
for(int column = 1; column <= columnCount;column++){
System.out.println("ColumnName = " +(metadata.getColumnName(column)));
}
}

}
catch(Exception e){}
...and the output i get is
ColumnName = TABLE_CAT
ColumnName = TABLE_SCHEM
ColumnName = TABLE_NAME
ColumnName = REMARKS

where as i want the column names corresponding to each table...plz help
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
For the column names you need to get the metadata for your resultset.

Statement statement = databaseConnection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM yourtable");
ResultSetMetaData metadata = rs.getMetaData();
int columns = metadata.getColumnCount
for(int i = 1 ; i<= columns ; i++)
System.out.println(metadata.getColumnName(i));

Bosun
Nasser Aboobaker
Ranch Hand

Joined: Dec 06, 2000
Posts: 104
Rajiv,
Here is full program .. you can go through it and get an idea.
HTML IS ON SO THE BOTTOM PART MAY NOT DISPLAY PROPERLY.
Nasser

import java.io.*;
import java.sql.*;
import org.w3c.dom.*;
import com.ibm.xml.parser.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class CreateXMLTree extends HttpServlet {

TXDocument doc;
TXElement EleClientDetRt, EleChildName, EleChildAddress, EleChildTel, EleChildFax, EleChildEmail;
TXText ChildTextName, ChildTextAddress, ChildTextTel, ChildTextFax, ChildTextEmail;
TXElement EleClient;
Writer wr;
CreateXMLTree(ServletOutputStream out, ResultSet rs){
try{
TXDocument doc = new TXDocument();
EleClientDetRt = (TXElement)doc.createElement("People");
doc.appendChild(EleClientDetRt);

while(rs.next()){
EleChildName = (TXElement)doc.createElement(rs.getMetaData().getColumnName(1));
EleChildAddress = (TXElement)doc.createElement(rs.getMetaData().getColumnName(2));
EleChildTel = (TXElement)doc.createElement(rs.getMetaData().getColumnName(3));
EleChildFax = (TXElement)doc.createElement(rs.getMetaData().getColumnName(4));
EleChildEmail = (TXElement)doc.createElement(rs.getMetaData().getColumnName(5));

ChildTextName = (TXText)doc.createTextNode(rs.getString("Name"));
ChildTextAddress = (TXText)doc.createTextNode(rs.getString("Address"));
ChildTextTel = (TXText)doc.createTextNode(rs.getString("Tel"));
ChildTextFax = (TXText)doc.createTextNode(rs.getString("Fax"));
ChildTextEmail = (TXText)doc.createTextNode(rs.getString("Email"));

EleChildName.appendChild(ChildTextName);
EleChildAddress.appendChild(ChildTextAddress);
EleChildTel.appendChild(ChildTextTel);
EleChildFax.appendChild(ChildTextFax);
EleChildEmail.appendChild(ChildTextEmail);

EleClient = (TXElement)doc.createElement("Person");
EleClient.appendChild(EleChildName);
EleClient.appendChild(EleChildAddress);
EleClient.appendChild(EleChildTel);
EleClient.appendChild(EleChildFax);
EleClient.appendChild(EleChildEmail);

EleClientDetRt.appendChild(EleClient);
}
((TXDocument)doc).printWithFormat(new PrintWriter(System.out));

wr = new OutputStreamWriter(new FileOutputStream("c:/test/ClientData.xml"));

doc.printWithFormat(wr);
wr.close();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Storage of manipulated information </TITLE>");
out.println("</HEAD>");

out.println("<BODY>");
out.println("The data required has been assembled and will be dispatched immediately");
out.println("
");
out.println("");
out.println("Click her to invoke the Data Manipulation Form");
out.println("
");
out.println("
");
out.println("...Please wait a moment.");
out.println("</BODY>");
out.println("</HTML>");
}
catch(Exception e){
System.out.println("Exception: " + e.getMessage());
}
}


}

[This message has been edited by Nasser Aboobaker (edited October 27, 2001).]
[This message has been edited by Nasser Aboobaker (edited October 27, 2001).]
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3879
This article may help relieve some confusion about how to use JDBC metadata.
http://hometown.aol.com/kgb1001001/Articles/JDBCMetadata/JDBC_Metadata.htm
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
rejieve alexander
Greenhorn

Joined: Dec 10, 2000
Posts: 22
Thanks a lot guys...it's working ..
Just one more doubt ...can i have two resultset objects
working at the same time..??? 'cause i'd want to get the table names
and at the same time get it's coulumn names while passing thro' a loop.
..but thanks any way..
Praveen Balaji
Ranch Hand

Joined: Jun 17, 2001
Posts: 60
You can.. run it on two threads..
But why whould you want to do that? I dont think it would improve performance in anyway..
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
To get column names, you use a ResultsetMetaData object, while for table names, you will use a DatabaseMetadata object.
Rejieve, as posted earlier, a good book or the links provided in this thread will go a long way in helping you clarify all these things.

Bosun
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find the column names..???
 
Similar Threads
Column names in Excel with JasperReports
Multi Column List
Retrieve MySQL table column names
How Can I Get the Column Names and Widths (in pixels) from a JTable
excel