Advanced Java (2160707)

BE | Semester-4   Winter-2018 | 20-11-2018

Q3) (a)

Explain DataBaseMetaData with example

DatabaseMetaData Interface

  • DatabaseMetaData interface provides methods to get meta data of a database such as
    1. database product name,
    2. database product version,
    3. driver name,
    4. name of total number of tables etc.
  1. import java.sql.*;
  2. public class DatabaseMetaDataDemo
  3. {
  4. public static void main(String[] args)
  5. {
  6. try
  7. {
  8. Class.forName("com.mysql.jdbc.Driver");
  9. Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/temp6", "root","root");
  10. DatabaseMetaData dbmd=con.getMetaData();
  11. System.out.println ("getDatabaseProductName:"+dbmd.getDatabaseProductName());
  12. System.out.println("getDatabaseProductVersion(): "+dbmd.getDatabaseProductVersion());
  13. System.out.println("getDriverName():"+dbmd.getDriverName()) ;
  14. System.out.println("getDriverVersion(): "+dbmd.getDriverVersion());
  15. System.out.println("getURL():"+dbmd.getURL());
  16. System.out.println("getUserName():"+dbmd.getUserName());
  17. }
  18. catch (Exception ex)
  19. {
  20. System.out.println("Exception:"+ex.toString());
  21. }
  22. }
  23. }

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