Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find the column names..???

 
rejieve alexander
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example...
ResultSetMetaData metadata = yourResultset.getMetaData();
for(int column = 1; column <= columnCount; column++)
{
System.out.print(metadata.getColumnLabel(column) + "\t");
}

Bosun
 
rejieve alexander
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rejieve alexander
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic