import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

public class GetColumnName extends HttpServlet
{
	public void doGet(HttpServletRequest req, HttpServletResponse res)
		throws IOException, ServletException
		{
			res.setContentType("text/html");
			PrintWriter pw = res.getWriter();
			Connection con;
			PreparedStatement ps;
			ResultSet rs;
			ResultSetMetaData rsmd;
			try
			{
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				con= DriverManager.getConnection("jdbc:odbc:formRecord");
				ps = con.prepareStatement("select * from dataformat");
				rs= ps.executeQuery();
				rsmd = rs.getMetaData();
				int clNo = rsmd.getColumnCount();
				pw.println("Table has "+clNo+" columns<br>");
				pw.println("Name of columns are ");
				for(int i=1; i<= clNo; i++)
				{
					String clNm = rsmd.getColumnName(i);
					pw.println("<br>"+clNm);
				}
			}
			catch(Exception e)
			{
				pw.println(e.getMessage());
			}
		}
	}
