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.

Ads

Tutorials   
Change Column Name in MySQL Java Mysql Connection Example JDBC Example with MySQL Jdbc Mysql Connection String Jdbc Mysql Connection Url Getting Column Names from a database table in Java Count Rows from a Database Table JDBC Example with MySQL Write Text File to Table Inserting Image in Database Table Insert Data in Table Using Stored Procedure Copy One Database Table to Another Copy Table in a MySQL Database Set Timestamp by using the Prepared Statement Set Time by using the Prepared Statement Set Date by using the Prepared Statement Prepared Statement Set Big Decimal Set byte, short and long data types by using the Prepared Statement Set Data Types by using Prepared Statement Using the Prepared Statement Twice Deleting Records using the Prepared Statement Count Records using the Prepared Statement Inserting Records using the Prepared Statement Update Records using Prepared Statement Select Records Using Prepared Statement Prepared Statement With Batch Update Statement Batch Update Prepared Statement Set Object Cross Join Tables in a Specific Database Join tables with the NATURAL RIGHT JOIN operation Join tables with the NATURAL LEFT JOIN operation Join tables in the specific database Delete a Column from a Database Table Delete a Specific Row from a Database Table Deleting All Rows from a Database Table Sum of Column in a Database Table Arrange a Column of Database Table Arrange a Column of Database Table Remove Unique Column in Database Table Make Unique Column in Database Table

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.

Advertisements

Ads
Share on Google+Share on Google+

Getting Column Names from a database table in Java

Posted on: March 19, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Getting Column Names from a database table in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
anwesh
November 19, 2012
cs

if we want to get the contents of the tables what we have to do

Ads

 

Ads