In this section we are discussing how to get information of MetaData using JDBC API.
MetaData :
DatabaseMetaData interface is mentioned in java.sql package. It is
implemented by the Driver vendors which helps user to know the capabilities of
DBMS.
DatabaseMetaData provides many methods some of them returns lists of information
in ResultSet object form and some of them takes String pattern arguments.
getMetaData() method retrieves a DatabaseMetaData
object that holds the metadata about the database for the specified connection
object.
Metadata contains information of all database tables, supported SQL grammar, its
stored procedures ,capabilities of the connection etc.
getTypeInfo() method retrieves a description of all
the data types supported by the database. They are ordered by DATA_TYPE and
then by how closely the data type maps to the corresponding JDBC SQL type.
Example : In this example we are displaying all the data types list of database students by using getTypeInfo() method.
package jdbc;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
class MetaDataExample{
public static void main(String[] args){
System.out.println("MetaData Example...");
Connection con = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "students";
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driverName);
// Connecting to the database
con = DriverManager.getConnection(url + dbName, userName, password);
try {
// Getting Type Info
DatabaseMetaData md = con.getMetaData();
rs = md.getTypeInfo();
int i=0;
System.out.println("List of data Type -");
while (rs.next()) {
System.out.println(rs.getString("TYPE_NAME"));
i++;
}
System.out.println("Total Data Type : "+i);
} catch (SQLException e) {
System.out.println(e);
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output :
MetaData Example... List of data Type - BIT BOOL TINYINT TINYINT UNSIGNED BIGINT BIGINT UNSIGNED LONG VARBINARY MEDIUMBLOB LONGBLOB BLOB TINYBLOB VARBINARY BINARY LONG VARCHAR MEDIUMTEXT LONGTEXT TEXT TINYTEXT CHAR NUMERIC DECIMAL INTEGER INTEGER UNSIGNED INT INT UNSIGNED MEDIUMINT MEDIUMINT UNSIGNED SMALLINT SMALLINT UNSIGNED FLOAT DOUBLE DOUBLE PRECISION REAL VARCHAR ENUM SET DATE TIME DATETIME TIMESTAMP Total Data Type : 40
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.