// this program opens a new data table NNRD3
import java.sql.*;

public class trial2 {
	public static void main(String args[]) {
    String url = "jdbc:odbc:trial";
    Connection con;
		Statement stmt;
    ResultSet rs,rs1;
    ResultSetMetaData rsm;
    String query = "select * from LLNRD";
		try
    {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
    catch(java.lang.ClassNotFoundException e)
    {
      System.err.print("ClassNotFoundException: ");
      System.err.println(e.getMessage());
    }
		try
    {
      con = DriverManager.getConnection(url,"myLogin", "myPassword");
			stmt = con.createStatement();
      rs=stmt.executeQuery(query);
      rsm=rs.getMetaData();
      int j=rsm.getColumnCount();
      String[] heading=new String[j];
      String[] coltype=new String[j];
      int[] col=new int[j];
      for(int h=1;h<=j;h++)
      {
      heading[h-1]=rsm.getColumnName(h);
      coltype[h-1]=rsm.getColumnTypeName(h);
      col[h-1]=rsm.getColumnType(h);
      if(coltype[h-1].equals("VARCHAR"))
      {col[h-1]=255;}
      System.out.println(heading[h-1]+"*"+coltype[h-1]+"*"+col[h-1]);
      }
      //create a new table
      String q1="create table NNRD3(";
      for(int h=1;h<j;h++)
      {
      if(coltype[h-1].equals("VARCHAR"))
      {
      q1+="["+heading[h-1]+"] "+coltype[h-1]+"("+col[h-1]+"), ";
      }
      else
      {
      q1+="["+heading[h-1]+"] "+coltype[h-1]+", ";
      }
      } //end of for
      if(coltype[j-1].equals("VARCHAR"))
      {
      q1+="["+heading[j-1]+"] "+coltype[j-1]+"("+col[j-1]+") ";
      }
      else
      {
      q1+="["+heading[j-1]+"] "+coltype[j-1]+" ";
      }
      q1+=")";
      System.out.println(q1);
      stmt.executeUpdate(q1);

			stmt.close();
			con.close();
		}
    catch(SQLException ex)
    {
			System.err.println("SQLException: " + ex.getMessage());
		}
	}
}
