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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java program to get column names of a table 
 

In this example of getting the columns names we need to have connection with the database and then after we will get metadata of this table and find the columns name within the metadata.

 

Java program to get column names of a table

                         

In this example of getting the columns names we need to have connection with the database and then after we will get metadata of this table and find the columns name within the metadata.

In our example program we have created a database connection with the data table within the MySQL database. The data table view is as follows:

 


 


To have metadata related manipulation we need to have an object of ResultSetMetaData and thereafter we can do the manipulation on the table related information.

ResultSetMetaData rsmd = rs.getMetaData();
int NumOfCol = rsmd.getColumnCount();

Above lines of code creates an object of ResultSetMetaData and we can get the number of columns with the use of getColumnCount() methods. Now we can get the column names with the index value by the method getColumnName().Here is the example code of GetColumnName.java as follows:

GetColumnName.java

import java.sql.*;
public class GetColumnName {
  public static void main(String[] argsthrows Exception {
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/";
    String username = "root";
    String password = "root";
    String dbName= "any";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url+dbName, username, password);
    System.out.println("Connected");
    Statement st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM webpages");
    ResultSetMetaData rsmd = rs.getMetaData();
    int NumOfCol = rsmd.getColumnCount();
    for(int i=1;i<=NumOfCol;i++)
    {
    System.out.println("Name of ["+i+"] Column="+rsmd.getColumnName(i));
    }
    st.close();
    conn.close();
  }


Output:

C:\javaexamples>javac GetColumnName.java

C:\javaexamples>java GetColumnName
Connected
Name of [1] Column=id
Name of [2] Column=title
Name of [3] Column=url
Name of [4] Column=pageContent

 

Download Source Code

 

                         

» View all related tutorials
Related Tags: c class ant help property get tutorial name ria this for to e des pe in as sta m nt

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 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
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

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.