Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java: How to get Column Detail Information

Java: How to get Column Detail Information
{filelink=7056}

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MetaData_Column
 {
  public static void main(String[] args) throws Exception
  	{
    Connection  conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cours",
        "root", "");
    DatabaseMetaData meta = conn.getMetaData();
    ResultSet rsColumn = meta.getColumns(null, "%", "Test", "%");
    while (rsColumn.next())
    {
      // Column name
      String nameColumn = rsColumn.getString("COLUMN_NAME");
      // Column type name
      String typeColumn = rsColumn.getString("TYPE_NAME");
      // Column size
      int taille = rsColumn.getInt("COLUMN_SIZE");
      // Is nullable?
      int nullable = rsColumn.getInt("NULLABLE");
      // column ordinal position
      int position = rsColumn.getInt("ORDINAL_POSITION");
      System.out.println ("Column info: ");
      System.out.println("Name: =" + nameColumn);
      System.out.println("Type=" + typeColumn);
      System.out.println("Size=" + taille);
      System.out.println ((nullable == DatabaseMetaData.columnNullable?"Yes":"No"));
      System.out.println("Position" + position);
    }
    conn.close();
  }
}

No comments yet.

Leave a Comment