Solved

Columns’ names of a table using JAVA

Posted on 2006-07-23
12
225 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
[X]
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
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

734 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