Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Columns’ names of a table using JAVA

Posted on 2006-07-23
12
Medium Priority
?
227 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 90 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

721 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