Wednesday, February 17, 2010

using DatabaseMetaData

Java provides two classes "DatabaseMetaData" and "ResultSetMetaData" which helps a lot while retrieving data about tables in SQL.
So how to use these classes?

First establish connection with database

Class.forName(driver);
Connection con=DriverManager.getConnection(url,user,password);

After establishing connection with database we can retrieve data about database

DatabaseMetaData dbmd=con.getMetaData();

After this to get the table list from database we can use following method

String types[]={
"TABLE"
};
ResultSet rs=dbmd.getTables(con.getCatalog(),null,null,types);
while(rs.next())
{
 Vector v=new Vector();
 System.out.println(rs.getString("TABLE_NAME"));
}

No comments:

Post a Comment

Ads Inside Post