Solved

Columns’ names of a table using JAVA

Posted on 2006-07-23
12
219 Views
Last Modified: 2006-11-18
As usual, there are some of SQL queries cannot be executed in JAVA and can be replaced by another query to do the job, and I want the columns’ names of a table as a result from:

desc tableName ;

which cannot be executed in JAVA.

So, how I get the columns’ names of a table?
0
Comment
Question by:javamate06
12 Comments
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
Not sure but, try if TableMetaData isthr
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 30 total points
Comment Utility
>>So, how I get the columns’ names of a table?

Use

resultSet.getDatabaseMetaData();
0
 

Author Comment

by:javamate06
Comment Utility
I tried:

ResultSet rs = stmt.executQuery(“desc tableName”);
ResultSetMetaData rsmd = rs.getMetaData();
int n = rsmd.getColumnCount();

for(int a = 0 ; a < n  ; a++)
{
  System.out.println(rsmd.getColumnName(a));
}

The problem with query itself not executable in JAVA.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Sorry - i'm glad you corrected my answer ;-)

>>The problem with query itself not executable in JAVA.

Why would it not be?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
As i think i mentioned before

>>ResultSet rs = stmt.executQuery(“desc tableName”);

should be


ResultSet rs = stmt.executeQuery(“describe tableName”);
0
 

Author Comment

by:javamate06
Comment Utility
Give me the same error

ORA-00900 : invalid SQL statement

Did you execute it?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
I don't have Oracle i'm afraid. You could execute a select on system tables
0
 

Author Comment

by:javamate06
Comment Utility
Thank you...

the query is correct and it can be executed in SQL worksheet it is only in java cannot.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Try executing a select on system tables
0
 
LVL 23

Expert Comment

by:Ajay-Singh
Comment Utility
Try using DatabaseMetaData (http://java.sun.com/j2se/1.5.0/docs/api/java/sql/DatabaseMetaData.html#getColumns(java.lang.String, java.lang.String, java.lang.String, java.lang.String))



DatabaseMetaData metaData = connection.getMetaData();
ResultSet columns = getColumns(null, null, tableName, null);
0
 

Author Comment

by:javamate06
Comment Utility
I did it...


ResultSet rs = stmt.executQuery("select * from tableName");
ResultSetMetaData rsmd = rs.getMetaData();
int n = rsmd.getColumnCount();

for(int a = 0 ; a < n  ; a++)
{
  System.out.println(rsmd.getColumnName(a));
}
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Glad you got it working
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoresSpecial  challenge 13 40
count8 challlenge 13 85
base64 decode encode 12 96
github account with ecipse 1 15
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now