Solved

Columns’ names of a table using JAVA

Posted on 2006-07-23
12
222 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 36
tomcat administrtor 12 48
Facing this issue for maven proxy setting 2 13
eclipse console opening separately 2 21
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

803 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