What is best way to get metadata?

Posted on 2005-03-23
Medium Priority
Last Modified: 2010-03-31
Dear Experts,

To get metadata for a particular table within Java, I have been doing this:

    String s = "SELECT TOP 1 * FROM " + table_full_name; // just to get metadata
      Statement st = conn.createStatement();
      ResultSet rs = st.executeQuery(s);
      ResultSetMetaData rsmd = rs.getMetaData();
      int maxcol = rsmd.getColumnCount();
      for (int col = 1; col <= maxcol; col++)
        // process metadata

Is there a simpler, more direct method?  It seems awkward to have to select a row first...

Question by:BrianMc1958
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
LVL 37

Expert Comment

ID: 13611806
>> It seems awkward to have to select a row first...
Nevertheless that's the way to go I think.
Moreover, you don't call rs.next(), so I think you don't really select

LVL 37

Expert Comment

ID: 13611813
LVL 29

Expert Comment

ID: 13611821
For simple JDBC like above, not that I know of but I'd be happy for someone else to correct me...

You could investigate Connection.getMetaData() but that returns a DatabaseMetaData object.
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

LVL 15

Accepted Solution

aozarov earned 200 total points
ID: 13611909
From the Javadoc of PreparedStatement :

Retrieves a ResultSetMetaData object that contains information about the columns of the ResultSet object that will be returned when this PreparedStatement object is executed.
Because a PreparedStatement object is precompiled, it is possible to know about the ResultSet object that it will return without having to execute it. Consequently, it is possible to invoke the method getMetaData on a PreparedStatement object rather than waiting to execute it and then invoking the ResultSet.getMetaData method on the ResultSet object that is returned.


Author Comment

ID: 13612181
You folks are amazing.  Thanks!
LVL 37

Expert Comment

ID: 13612349
No split? :(

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

771 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