Explain DataBaseMetaData with example
DatabaseMetaData Interface
-
DatabaseMetaData interface provides methods to get meta data of a database such as
- database product name,
- database product version,
- driver name,
- name of total number of tables etc.
- import java.sql.*;
- public class DatabaseMetaDataDemo
- {
- public static void main(String[] args)
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/temp6", "root","root");
- DatabaseMetaData dbmd=con.getMetaData();
- System.out.println ("getDatabaseProductName:"+dbmd.getDatabaseProductName());
- System.out.println("getDatabaseProductVersion(): "+dbmd.getDatabaseProductVersion());
- System.out.println("getDriverName():"+dbmd.getDriverName()) ;
- System.out.println("getDriverVersion(): "+dbmd.getDriverVersion());
- System.out.println("getURL():"+dbmd.getURL());
- System.out.println("getUserName():"+dbmd.getUserName());
- }
- catch (Exception ex)
- {
- System.out.println("Exception:"+ex.toString());
- }
- }
- }
OUTPUT
getDatabaseProductName:MySQL
getDatabaseProductVersion():5.6.16
getDriverName():MySQL-AB JDBC Driver
getDriverVersion():mysql-connector-java-5.1.23 ( Revision: ${bzr.revision-id} )
getURL():jdbc:mysql://localhost:3306/temp6
getUserName():root@localhost