Introduction to Metadata in Java
Metadata in Java defined as the data about the data is called “Metadata”. Metadata is also said to be documentation about the information which is required by the users. This is one of the essential aspects in the case of data warehousing.
Real Time Examples: A library catalog, the table of content, data item about person data (person weight, a person walking, etc.), etc.
Metadata Consisting of the below things:
- The description and location of the system and its components.
- It also has the Names, definitions, content, and structures of data and end-user views.
- Identification of authoritative data.
- Integration and transformation rules are used to populate data.
- Subscription information of subscribers.
- Used to analyze data usage and performance.
Why is Metadata Necessary?
It gives information about the contents like table data, library catalog, etc., and structures to the Java developers.
Types of Metadata
There are 3 types of metadata
- Operational Metadata
- Extraction and Transformation Metadata
- End-User Metadata
1. Operational Metadata: Operational metadata having all the information of the operational data sources. While selecting information from the source system for Datawarehouse then we will divide the records, combine the factors of documents from various sources, and deal with multiple coding schemes and field lengths. While we deliver to the information for end-users, then we must able to get back to source data sets.
2. Extraction and Transformation Metadata: Extraction and Transformation Metadata include data about the removal of data from the source systems. Those are extraction methods, extraction frequencies, and business rules for the data extraction are belong to Extraction and Transformation Metadata.
3. End-User Metadata: The end-user metadata is the navigational map of the data house. Actually, it enables the end-users to find the data from the data warehouse.
How does Metadata work in Java?
Java Metadata is working based on data provided to it. It gives information of data about the data.
Syntax:
class Metadata{
public static void main(String args[]){
try{
//load required database class
//creating database metadata class
DatabaseMetaData metaData=con.getMetaData();
//display the metadata of the table content
System.out.println(metaData.getDriverName());
System.out.println(metaData.getDriverVersion());
System.out.println(metaData.getUserName());
System.out.println(metaData.getDatabaseProductName());
System.out.println(metaData.getDatabaseProductVersion());
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
Examples to Implement Metadata in Java
Below are the examples of Metadata in Java:
Example #1 – Result Set Metadata
Code:
import java.sql.*;//importing sql package
public class A {//Creating class
//main method for run the application
public static void main(String args[]) {
try {
//loading my sql driver
Class.forName("com.mysql.jdbc.Driver");
//get the connection by providing database, user name and password
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
//select the all from employee table
PreparedStatement preparedStatement = connection.prepareStatement("select * from employee");
//executing the query
ResultSet resultSet = preparedStatement.executeQuery();
//Create result meta data for get the meta data of table
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
//Displaying meta data of employee table
System.out.println("Total Number of columns: " + resultSetMetaData.getColumnCount());
System.out.println("1st Column name : " + resultSetMetaData.getColumnName(1));
System.out.println("2nd Column name : " + resultSetMetaData.getColumnName(2));
System.out.println("3rd Column name : " + resultSetMetaData.getColumnName(3));
System.out.println("Column Type Name of 1st column: " + resultSetMetaData.getColumnTypeName(1));
System.out.println("Column Type Name of 2nd column: " + resultSetMetaData.getColumnTypeName(2));
System.out.println("Column Type Name of 3rd column: " + resultSetMetaData.getColumnTypeName(3));
connection.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Output:
Example #2 – Database Metadata
Code:
import java.sql.*;//importing sql package
public class A {//Creating class
//main method for run the application
public static void main(String args[]) {
try {
//loading my sql driver
Class.forName("com.mysql.jdbc.Driver");
//get the connection by providing database, user name and password
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
//select the all from employee table
PreparedStatement preparedStatement = connection.prepareStatement("select * from employee");
//executing the query
preparedStatement.executeQuery();
//Create databse result set meta data for get the meta data of databse of mysql
DatabaseMetaData databaseMetaData=connection.getMetaData();
//Displaying meta data of mysql table
System.out.println("MYSQL Driver Name: "+databaseMetaData.getDriverName());
System.out.println("MYSQL Driver Version: "+databaseMetaData.getDriverVersion());
System.out.println("MYSQL UserName: "+databaseMetaData.getUserName());
System.out.println("MYSQL Database Product Name:"+databaseMetaData.getDatabaseProductName());
System.out.println("MYSQL Database Product Version: "+databaseMetaData.getDatabaseProductVersion());
connection.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Output:
Example #3 – Database Metadata for Extracting Table Names
Code:
import java.sql.*;//importing sql package
public class A {// Creating class
// main method for run the application
public static void main(String args[]) {
try {
// loading my sql driver
Class.forName("com.mysql.jdbc.Driver");
// get the connection by providing database, user name and password
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
// Create databse result set meta data for get the meta data of
// databse of mysql
DatabaseMetaData dbmd = connection.getMetaData();
String table[] = { "VIEW" };
ResultSet resultSet = dbmd.getTables(null, null, null, table);
// iterating number table names from database of mysql
while (resultSet.next()) {
System.out.println("Table name is: "+resultSet.getString(3));
}
connection.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Output:
Conclusion
Metadata in Java is used to know the data about data. It means for example table field names, field data type, field data type length, database table names, number of databases that existed in the specific database, etc.
Recommended Articles
This is a guide to Metadata in Java. Here we discuss the Introduction to Metadata in Java and its different types along with Examples and Code Implementation. You can also go through our other suggested articles to learn more –
- Java 8 Collectors With Characteristics
- How Does Object Class Works in Java?
- Java Directories (Examples)
- Introduction to repaint in Java
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses