Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Columns’ names of a table using JAVA

Posted on 2006-07-23
12
223 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
ID: 17165789
Not sure but, try if TableMetaData isthr
0
 
LVL 86

Accepted Solution

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

Use

resultSet.getDatabaseMetaData();
0
 

Author Comment

by:javamate06
ID: 17165822
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 17165833
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
ID: 17165840
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
ID: 17165866
Give me the same error

ORA-00900 : invalid SQL statement

Did you execute it?
0
 
LVL 86

Expert Comment

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

Author Comment

by:javamate06
ID: 17165906
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
ID: 17165915
Try executing a select on system tables
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17165916
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
ID: 17165943
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
ID: 17165948
Glad you got it working
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 29
web services creation SOAP vs REST 5 56
What's wrong with this code? 4 26
Chrome and Firefox Java 5 50
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)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

860 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