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.

|
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