DatabaseMetaData in Java
The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total no of tables, version, total number of views etc.
Methods
- getDriverName():
In this method, used to get the name of database driver. - getDriverVersion():
In this method, used to get the version of database driver. - getUserName():
In this method, used to get the username of database. - getDatabaseProductName():
In this method, used to get the name of the product of database. - getDatabaseProductVersion():
In this method, used to get the product version of database.
Code 1:
// program for DatabaseMetaData
import java.sql.*;
class DbMetaData
{
public static void main(string args[])
{
String url =“jdbc:mysql://localhost//DB”;
Connection con = DriverManager.getConnection(url,”root”,”password”);
DatabaseMetaData DBMD = con.getMetaData();
System.out.println(“Driver Name”+DBMD.getDriverName());
System.out.println(“Driver Version”+DBMD.getDriverVersion());
System.out.println(“User Name”+DBMD.getUserName());
System.out.println(“Database Product Name”+DBMD.getDatabaseProductName());
System.out.println(“Database Product Version”+DBMD.getDatabaseProductVersion());
con.close();
}
}
By the Above program we can get the details of the Database Details like, Version , username, Product Name, Product Version.
Code 2:
import java.sql.*;
class DbMetaData2
{
public static void main(String args[])
{
Connection con = DriverManager.getConnection(url,”root”,”password”);
DatabaseMetaData DBMD = con.getMetaData();
String table[] = {“Table”};
ResultSet rs = DBMD.getTables(null,null,null,table);
while(rs.next())
{
System.out.println(rs.getString(3));
}
con.close();
}
}
This program is used to display Number of tables present in the database.
Code 3:
import java.sql.*;
class DbMetaData2
{
public static void main(String args[])
{
Connection con = DriverManager.getConnection(url,”root”,”password”);
DatabaseMetaData DBMD = con.getMetaData();
String table[] = {“View”};
ResultSet rs = DBMD.getTables(null,null,null,table);
while(rs.next())
{
System.out.println(rs.getString(3));
}
con.close();
}
}
This program is used to display the Views present in the database.