Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions? | Software Development
 

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.

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.

                         

» View all related tutorials
Related Tags: c database query ide table data io column numbers number sum using this id simple tab student university if ie

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

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.

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

 
Tell A Friend
Your Friend Name

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

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 | Privacy Policy

India News

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

Copyright © 2008. All rights reserved.