The Code here takes a Table name as
the String Parameter and Displays the Table's MetaData in two ways
Solution 1 : Through ResultSetMetaData (java.sql.ResultSetMetaData)
Solution 2 : Through DatabaseMetaDataSet (java.sql.DatabaseMetaData)
The Code snippet has comments for easy understanding and use.
Solution 1 : Through ResultSetMetaData (java.sql.ResultSetMetaData)
Solution 2 : Through DatabaseMetaDataSet (java.sql.DatabaseMetaData)
The Code snippet has comments for easy understanding and use.
import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; public class MetaDataReader1 { public static void main(String[] args) throws SQLException { System.out.println("Connecting.."); Connection conn = null; String url = "jdbc:oracle:thin:@192.168.5.43:1521:userdb"; String driver = "oracle.jdbc.OracleDriver"; String userName = "rajdb"; String password = "password"; // Making Connection...... try { Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url, userName, password); } catch (Exception e) { System.out.println("Error in Connection"); } System.out.println("Connected to the database"); System.out.println("***************Method 1*******************"); /*** Method 1 ***/ String query=""; /* The Query String */ if (args.length == 0) { query = "Select * from TABLE1"; // in case no parameters are passed some default value } else if (args.length ==1){ query = "Select * from " + args[0]; } else { System.out.println("Invalid parameters"); System.exit(0); } Statement stmt = conn.createStatement(); ResultSet rs = null; try { rs = stmt.executeQuery(query); } catch (Exception e) { System.out.println("Unable to Execute Query"); System.exit(0); } ResultSetMetaData rsMd = rs.getMetaData(); System.out.print("The Number of Columns in the Table -> "); System.out.println(rsMd.getColumnCount() + "\n"); System.out.println("*************Table MetaData***************"); for (int i = 1; i <= rsMd.getColumnCount(); i++) { System.out.print(rsMd.getColumnName(i) + "\t"); // Column Name System.out.print(rsMd.getColumnClassName(i) + "\t"); // Column Class Type System.out.print(rsMd.getColumnTypeName(i) + "\t"); // Column Type in DB System.out.print(rsMd.getPrecision(i) + "\t"); // Column Precision System.out.println(rsMd.getColumnDisplaySize(i) + "\t"); // Column Size } System.out.println("\n***************Method 2*******************"); System.out.println("*************Table MetaData***************"); /*** Method 2 ***/ DatabaseMetaData dbm = conn.getMetaData(); ResultSet rs1 = dbm.getColumns("rajdb", "%", "WI_CONTROL_TBL", "%"); while (rs1.next()) { String col_name = rs1.getString("COLUMN_NAME"); // Column name String data_type = rs1.getString("TYPE_NAME"); // Column Type in DB int data_size = rs1.getInt("COLUMN_SIZE"); // Column Size int nullable = rs1.getInt("NULLABLE"); // Column is Nullable or Not System.out.print(col_name + "\t" + data_type + "(" + data_size + ")" + "\t"); if (nullable == 1) { System.out.print("YES NULLABLE\t"); } else { System.out.print("NOT NULLABLE\t"); } System.out.println(); } System.out.println("\n***************Table Data*****************"); int rowNumber = 0; int totColumns = rs.getMetaData().getColumnCount(); while (rs.next()) { // ROW Iterator rowNumber++; int colNumber = 1; for (int i = 0; i < totColumns; i++) { // Column Iterator // Reading type of Column No. = colNumber String s = rs.getMetaData().getColumnClassName(colNumber); int j = 0; /* * For Example I have taken only 3 type of data ... For any * different type of Column Class name one needs to add to the * code here and print accordingly */ // Reading type of the Column if (s.equalsIgnoreCase("java.lang.String")) { j = 1; // java.lang.String } else if (s.equalsIgnoreCase("java.sql.Timestamp")) { j = 2; // java.sql.Timestamp } else if (s.equalsIgnoreCase("java.math.BigDecimal")) { j = 3; // java.math.BigDecimal } // Printing Output of the Column switch (j) { case 1: System.out.print(rs.getString(colNumber++) + "\t"); break; case 2: System.out.print(rs.getDate(colNumber++) + "\t"); break; case 3: System.out.println(rs.getLong(colNumber++) + "\t"); } } System.out.println(); } System.out.println("\n******************************************"); System.out.println("Total Number of Rows - >" + rowNumber); System.out.println("******************************************"); System.out.println("Disconnecting..."); conn.close(); System.out.println("Disconnected from database"); } }