Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
JDBC
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Getting Column Names from a database table in Java

                         

Here we are providing you an example with code that retrieves all columns name in a specific database table. Sometimes, you need to know the number of columns and the names of the columns of the table, so you can retrieve it with the help of this example. See detail information given below:

Description of program:

Create a class ColumnName. The name of the class should be such that the other person can easily understand what this program is going to do. Strictly follow the naming conventions of java. Now declare a static method inside which creates the connection with MySQL database through the JDBC driver. After establishing the connection then you get all columns name and number of columns of specified table with the help of some APIs and methods.

Description of code:

ResultSetMetaData:
This is an interface of java.sql package that can be used for getting information about types and properties of columns in a ResultSet object.

getColumnCount():
Above method retrieves number of columns (integer types data) in the ResultSet object.

getColumnName(int column):
This method returns columns name (string type data) from ResultSetMetaData object and takes integer type value.

Here is the code of program:

import java.sql.*;

public class ColumnName{
  public static void main(String[] args) {
    System.out.println("Getting Column Names Example!");
    Connection con = null;
    String url = "jdbc:mysql://localhost:3306/";
    String db = "jdbctutorial";
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";
    try{
      Class.forName(driver);
      con = DriverManager.getConnection(url+db, user, pass);
      try{
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM employee6");
        ResultSetMetaData md = rs.getMetaData();
        int col = md.getColumnCount();
        System.out.println("Number of Column : "+ col);
        System.out.println("Columns Name: ");
        for (int i = 1; i <= col; i++){
          String col_name = md.getColumnName(i);
          System.out.println(col_name);
        }
      }
      catch (SQLException s){
        System.out.println("SQL statement is not executed!");
      }
    }
    catch (Exception e){
      e.printStackTrace();
    }
  }
}

Output of program:

C:\vinod\jdbc\jdbc-mysql>javac ColumnName.java

C:\vinod\jdbc\jdbc-mysql>java ColumnName
Getting Column Names Example!
Number of Column : 2
Columns Name:
Emp_code
Emp_name

Download this example.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (post your own) View All Comments Latest 10 Comments:

Hi there,
you can get the metaData object from preparedStatement, so there is no need to query database, for example:
Connection connection = ...
PreparedStatement ps = connection.prepareStatement("SELECT * FROM someTable");
ResultSetMetaData rsmd = ps.getMetaData();

As JavaDoc says:
Retrieves a ResultSetMetaData object that contains information about the columns of the ResultSet object that will be returned when this PreparedStatement object is executed.

Posted by Witold Szczerba on Thursday, 06.12.08 @ 16:27pm | #63108

Just wanted to say I found this page quite useful, a quick and complete reference.

Thanks heaps,
Shuku

Posted by Shuku on Monday, 05.12.08 @ 11:12am | #59425

hi experts...
i was facing problem in collecting table column names....this program has solved my problem a great deal.........thanks to roseindia....also to experts...

Posted by debadarshi mishra on Tuesday, 03.18.08 @ 17:53pm | #53193

pls get database value in table format.

to give java codings.

Posted by pallavi on Monday, 10.8.07 @ 15:52pm | #32478

is there a more efficient way of doing, for example, without having to select the whole table?
Thanks,
Dennis

Posted by Dennis on Saturday, 03.31.07 @ 16:35pm | #13081

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.