Tuesday 8 July 2014

Java meta data

Here is the program to get the table count columns.

the program is starting from here.





/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package jmetadata;

/**
 *
 * @author jawadmahmood
 */
import java.sql.*;

public class JMetaData {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try{
            Class.forName("com.mysql.jdbc.Driver");
           
      //Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8889/jrecords", "root" , "root");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:8889/jrecords", "root" , "root");
         
            String sql = "Select * From post ";
            PreparedStatement pStmt = con.prepareStatement(sql);
            ResultSet rs = pStmt.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
           
            int numColumn = rsmd.getColumnCount();
           
            System.out.println ("The total column in the jrecods are " + numColumn);
            String cName;
           
           for (int i = 1; i <= numColumn ; i++){
         
               cName = rsmd.getColumnName(i);
               System.out.print(cName);
               System.out.print("\t");
           }
           con.close();
           
           
        }catch(Exception ex){
           
           System.out.println("There is conection error or " + ex);
       
        }
       
    }
   
}
























































when you will run the program the out put will be like as 


No comments:

Post a Comment