JDBC DatabaseMetaData interface

The metadata refers to the data about data. The DatabaseMetaData interface provides the facility to get the information like driver name, total number of tables and driver version etc. We can get the object of DatabaseMetaData by calling getMetaData() method of Connection interface.
Syntax: DatabaseMetaData dbmd=conn.getMetaData();

Commonly used methods of DatabaseMetaData:

1. getDriverName(): It is used to get thename of the JDBC driver.
Syntax: public String getDriverName()throws SQLException
2. getDriverVersion(): It is used to get the JDBC driver version.
Syntax: public String getDriverVersion()throws SQLException
3. getUserName(): It is used to get the database username.
Syntax: public String getUserName()throws SQLException
4. getDatabaseProductName(): It is used to get the database product name.
Syntax: public String getDatabaseProductName()throws SQLException
5. getDatabaseProductVersion(): It is used to get the database product version.
Syntax: public String getDatabaseProductVersion()throws SQLException
6. getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types): It is used to get the tables detail of the specified catalog.
Syntax: public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)throws SQLException

Example:

JDBCTest.java
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import com.javawithease.util.JDBCUtil;
 
/**
* This class is used to show the use of DataBaseMetaData.
* @author javawithease
*/

public class JDBCTest {
public static void main(String args[]){
Connection conn = null;
 
try{
//get connection
conn = JDBCUtil.getConnection();
 
//get DatabaseMetaData
DatabaseMetaData dbmd = conn.getMetaData();
 
//Result set meta data
System.out.println("Driver Name: "
+ dbmd.getDriverName());
System.out.println("Driver Version: "
+ dbmd.getDriverVersion());
System.out.println("DB UserName: "
+ dbmd.getUserName());
System.out.println("DB Product Name: "
+ dbmd.getDatabaseProductName());
System.out.println("DB Product Version: "
+ dbmd.getDatabaseProductVersion());
 
//close connection
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
JDBCUtil.java
import java.sql.Connection;
import java.sql.DriverManager;
 
/**
* This is a utility class for JDBC connection.
* @author jawithease
*/

public class JDBCUtil {
//JDBC and database properties.
private static final String DB_DRIVER =
"oracle.jdbc.driver.OracleDriver";
private static final String DB_URL =
"jdbc:oracle:thin:@localhost:1521:XE";
private static final String DB_USERNAME = "system";
private static final String DB_PASSWORD = "oracle";
 
public static Connection getConnection(){
Connection conn = null;
try{
//Register the JDBC driver
Class.forName(DB_DRIVER);
 
//Open the connection
conn = DriverManager.
getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
 
if(conn != null){
System.out.println("Successfully connected.");
}else{
System.out.println("Failed to connect.");
}
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
}

Output:

Successfully connected.
Driver Name: Oracle JDBC driver
Driver Version: 10.2.0.1.0XE
DB UserName: SYSTEM
DB Product Name: Oracle
DB Product Version:
Oracle Database 10g Express Edition
Release 10.2.0.1.0 - Production

No comments: