/*program for implementing Desc command of oracle database in java*/
}import java.sql.*; public class jdbc2 { public static void main(String arg[])throws ClassNotFoundException,SQLException { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger"); if(con==null) { System.out.println("sory unable to connect to DataBase " + "Try again after sometime"); return; } else System.out.println("connected successfully"); String table=arg[0]; Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from "+ table); ResultSetMetaData rsmd=rs.getMetaData(); int n=rsmd.getColumnCount(); String s; System.out.println("Name"+"\t\t\t"+" Null?"+"\t\t\t"+"Type"); System.out.println("-------------------- ----------------- --------------"); for(int i=1;i<=n;i++) { if(rsmd.isNullable(i)==ResultSetMetaData.columnNoNulls) s=" NOT NULL"; else s=" "; if(rsmd.getColumnTypeName(i)=="DATE") System.out.println(rsmd.getColumnName(i)+"\t"+"\t"+"\t"+"\t"+"\t"+rsmd.getColumnTypeName(i)); else { if(rsmd.getScale(i)==0) System.out.println(rsmd.getColumnName(i)+"\t"+"\t"+"\t"+ s+"\t"+"\t"+rsmd.getColumnTypeName(i)+ "("+rsmd.getPrecision(i) +")"); else System.out.println(rsmd.getColumnName(i)+"\t"+"\t"+"\t"+ s+"\t"+"\t"+rsmd.getColumnTypeName(i)+ "("+rsmd.getPrecision(i)+"," +rsmd.getScale(i)+")"); }} rs.close(); rs=null; stmt.close(); stmt=null; con.close(); con=null;
}
compiling and executing of this program
--------------------------------------------
1. compile as usual
2 execute this program by providing table name as command line argument