Write a java program that prints the meta-data of a given table.

Program Code:
import java.sql.*;
import java.util.*;
public class Tblmdata 
{
public static void main(String[] args) 
{
Connection cn;
Statement st;
ResultSet rs, rs1;
ResultSetMetaData rsmd;
try {
Scanner sc = new Scanner(System.in);
System.out.println("------connecting database-----");
System.out.println("Enter Database Name");
String dbname = sc.next();
System.out.println("Enter Password");
String pass = sc.next();
cn = DriverManager.getConnection("jdbc:mysql://localhost/" + dbname, "root", pass);
st = cn.createStatement();
DatabaseMetaData dm = cn.getMetaData();
rs = dm.getTables(cn.getCatalog(), "%", "%", null);
String s = "";
String sql = "select * from ", sql1 = "";
System.out.println("Database is " + dbname);
System.out.println("Tables are:");
while (rs.next()) 
{
sql1 = "";
System.out.println("Table Name: " + rs.getString(3));
sql1 = sql + rs.getString(3);
rs1 = st.executeQuery(sql1);
rsmd = rs1.getMetaData();
System.out.println("Columns are:");
System.out.println("Column Name\tColumn Type\tSize");
for (int i = 1; i <= rsmd.getColumnCount(); i++) 
{
System.out.println(rsmd.getColumnLabel(i) + "\t" + rsmd.getColumnTypeName(i) + "\t" +rsmd.getColumnDisplaySize(i));
}
System.out.println("----------------------------------");
}
rs.close();
cn.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}

Output:
------connecting database-----
Enter Database Name
Jdbc
Enter Password
******
Database is jdbc 
Tables are:
Table Name: 
emp
Columns are:
Column Name Column Type Size
----------------------------------
Id Int 30
Name varchar 20
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: