Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 938

DatabaseMetaData Interface in Java-JDBC

$
0
0

DatabaseMetaData interface, which resides in java.sql package, provides information about the database (DB meta data) you are connected to.

Using the methods provided by this interface you can get information about the Database like DB name and version, about the JDBC driver like the driver’s name and version, names of schemas, name of tables, names of views, information about the procedures.

In this post we’ll see examples of some of the commonly used methods. You can get the list of full methods here - https://docs.oracle.com/javase/9/docs/api/java/sql/DatabaseMetaData.html

How to get DatabaseMetaData object

You can get the DatabaseMetaData instance by calling the getMetaData() method of the Connection class.


DatabaseMetaData dbMetaData = connection.getMetaData();

Example to get DB product and version information


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBMetaData {

public static void main(String[] args) {
Connection connection = null;
try {
// Loading driver
Class.forName("com.mysql.jdbc.Driver");

// Creating connection
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/netjs",
"root", "admin");

DatabaseMetaData dbMetaData = connection.getMetaData();

System.out.println("Database Name - " + dbMetaData.getDatabaseProductName());
System.out.println("Database Version - " + dbMetaData.getDatabaseProductVersion());
System.out.println("Database Major Version - " + dbMetaData.getDatabaseMajorVersion());
System.out.println("Database Minor Version - " + dbMetaData.getDatabaseMinorVersion());
System.out.println("Database User - " + dbMetaData.getUserName());

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection != null){
//closing connection
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // if condition
}// finally

}

}

Example to get driver information


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBMetaData {
public static void main(String[] args) {
Connection connection = null;
try {
// Loading driver
Class.forName("com.mysql.jdbc.Driver");

// Creating connection
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/netjs",
"root", "admin");

DatabaseMetaData dbMetaData = connection.getMetaData();

System.out.println("Driver Name - " + dbMetaData.getDriverName());
System.out.println("Driver Version - " + dbMetaData.getDriverVersion());
System.out.println("Driver Major Version - " + dbMetaData.getDriverMajorVersion());
System.out.println("Driver Minor Version - " + dbMetaData.getDriverMinorVersion());
System.out.println("JDBC Major Version - " + dbMetaData.getJDBCMajorVersion());
System.out.println("JDBC Minor Version - " + dbMetaData.getJDBCMinorVersion());

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection != null){
//closing connection
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // if condition
}// finally
}
}

Example to get tables

For getting tables you can use getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) method. You can provide null as value for all the parameters, that way you don’t narrow the search and all the tables are returned. If you want to narrow your search to get specific tables then you can provide values for these parameters.


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBMetaData {

public static void main(String[] args) {
Connection connection = null;
try {
// Loading driver
Class.forName("com.mysql.jdbc.Driver");

// Creating connection
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/world",
"root", "admin");

DatabaseMetaData dbMetaData = connection.getMetaData();

ResultSet rs = dbMetaData.getTables(null, null, null, null);
while (rs.next()){
System.out.println("Table name " + rs.getString(3));
}

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection != null){
//closing connection
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // if condition
}// finally
}
}

Output


Table name city
Table name country
Table name countrylanguage

Here I am connecting to “world” schema in MySQL and getting all the tables.

Each table description in the returned ResultSet has the following columns:

Table Descrition Columns
Column NameTypeDescription
TABLE_CATStringtable catalog (may be null)
TABLE_SCHEMStringtable schema (may be null)
TABLE_NAMEStringtable name
TABLE_TYPEStringtable type. Typical types are "TABLE", "VIEW" etc.
REMARKSStringexplanatory comment on the table (may be null)
TYPE_CATStringthe types catalog (may be null)
TYPE_SCHEMStringthe types schema (may be null)
TYPE_NAMEStringtype name (may be null)
SELF_REFERENCING_COL_NAMEStringname of the designated "identifier" column of a typed table (may be null)
REF_GENERATIONStringspecifies how values in SELF_REFERENCING_COL_NAME are created.

That’s why column index is 3 while getting result from ResultSet as TABLE_NAME is at number 3.

Example to get Procedures

For getting procedures you can use getProcedures(String catalog, String schemaPattern, String procedureNamePattern) method. Again you can pass null as value for all the parameters if you don’t want to narrow the search.


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBMetaData {

public static void main(String[] args) {
Connection connection = null;
try {
// Loading driver
Class.forName("com.mysql.jdbc.Driver");

// Creating connection
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/netjs",
"root", "admin");

DatabaseMetaData dbMetaData = connection.getMetaData();

ResultSet rs = dbMetaData.getProcedures(null, null, null);

while (rs.next()){
System.out.println("Procedure name " + rs.getString(3));
}

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(connection != null){
//closing connection
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // if condition
}// finally

}
}

Each procedure description in the returned ResultSet has the following columns:

Procedure Descrition Columns
Column NameTypeDescription
PROCEDURE_CATStringprocedure catalog (may be null)
PROCEDURE_SCHEMStringprocedure schema (may be null)
PROCEDURE_NAMEStringprocedure name
reserved for future use
reserved for future use
reserved for future use
REMARKSStringexplanatory comment on the procedure
PROCEDURE_TYPEshorttype name (may be null)
SPECIFIC_NAMEStringThe name which uniquely identifies this procedure within its schema.

That’s why column index is 3 while getting result from ResultSet as PROCEDURE_NAME is at number 3.

That's all for this topic DatabaseMetaData Interface in Java-JDBC. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. DataSource in Java-JDBC
  2. Java JDBC Steps to Connect to DB
  3. Statement Interface in Java-JDBC
  4. ResultSet Interface in Java-JDBC
  5. Data access in Spring framework

You may also like -

>>>Go to Java advance topics page


Viewing all articles
Browse latest Browse all 938

Trending Articles