Columns’ names of a table using JAVA

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?
javamate06Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CEHJConnect With a Mentor Commented:
>>So, how I get the columns’ names of a table?

Use

resultSet.getDatabaseMetaData();
0
 
ksivananthCommented:
Not sure but, try if TableMetaData isthr
0
 
javamate06Author Commented:
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 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
CEHJCommented:
Sorry - i'm glad you corrected my answer ;-)

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

Why would it not be?
0
 
CEHJCommented:
As i think i mentioned before

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

should be


ResultSet rs = stmt.executeQuery(“describe tableName”);
0
 
javamate06Author Commented:
Give me the same error

ORA-00900 : invalid SQL statement

Did you execute it?
0
 
CEHJCommented:
I don't have Oracle i'm afraid. You could execute a select on system tables
0
 
javamate06Author Commented:
Thank you...

the query is correct and it can be executed in SQL worksheet it is only in java cannot.
0
 
CEHJCommented:
Try executing a select on system tables
0
 
Ajay-SinghCommented:
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
 
javamate06Author Commented:
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
 
CEHJCommented:
Glad you got it working
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.