JDBC  «Prev  Next»
Lesson 6JDBC methods to get database content information
Objective Obtain database content information by using the various DatabaseMetaData methods.

Get Database Content Information

Before you create and populate a database table, you may need to get information about the database you are querying. A DatabaseMetaData object can help provide that information.
Question: How is a DatabaseMetaData object created from the Connection in JDBC?
In JDBC (Java Database Connectivity), a DatabaseMetaData object is created from an existing Connection object using the getMetaData() method. The DatabaseMetaData object allows you to access metadata information about the database and the JDBC driver.
Here is a step-by-step process for creating a DatabaseMetaData object from a Connection object in JDBC:

Step-by-step process for creating a DatabaseMetaData Object

  1. Load the JDBC driver (optional for JDBC 4.0 and later): Before establishing a connection to the database, you may need to load the JDBC driver, depending on the Java version you are using. In Java versions prior to JDBC 4.0, you must load the driver explicitly, while in JDBC 4.0 and later, the driver is loaded automatically if it's available in the classpath.
    For JDBC 3.0 and earlier, load the driver explicitly:
    Class.forName("com.mysql.jdbc.Driver");
    
    For JDBC 4.0 and later, you can skip this step.
  2. Establish a connection to the database: To establish a connection to the database, you need to use the DriverManager class's getConnection() method. This method takes a database URL, along with optional username and password parameters, depending on the database server's authentication requirements.
    Example:
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myDatabase", "username", "password");
    

  3. Obtain the DatabaseMetaData object from the Connection object: Use the getMetaData() method of the Connection object to create a DatabaseMetaData object.
    Example:
    DatabaseMetaData dbMetaData = conn.getMetaData();
    

Now you have a DatabaseMetaData object (dbMetaData), which you can use to query metadata information about the database and the JDBC driver, such as supported features, schema details, and driver version.
Remember to close the Connection object when you're done using it to release the database resources:
conn.close();

A DatabaseMetaData object is created from the Connection.
The int methods have the form getMaxXXX. Change the XXX to the subject being limited. This snippet of code includes the method for discovering the column length of a database (where con is a Connection object returned from some database):


DatabaseMetaData dbmd = con.getMetaData();
int max = dbmd.getMaxColumnNameLength();
String columnName = "AnExtraLongColumnName";
if(ColumnName.length() > len){ 
System.out.print("The maximum number of characters " 
+ "allowed in a column name is " + max +  "\n" +
columnName + " length = " + columnName.length());
}
An example of the output of this code is:
The maximum number of characters allowed in a column name is 29
AnExtraLongColumnName length =32

There are nearly 70 DatabaseMetaData methods of the form supportsXXX(), where XXX is the capability that you want to check.
For example:
Boolean supportsPositionedUpdate() Throws SQLException;
Most methods that return a boolean tell you whether the DBMS support the capability or does not support. For a complete list of these methods, please see the java.sql.databaseMetaData interface in your SDK documentation. You will see how the meta-data will be helpful to you as proceed through these lessons. However, let us see how well you have learned the beginning steps for creating and populating a database table.
Later in this course, you will create and populate database tables, query the database, and review the results.
The next lesson concludes this module.

JDBC Methods - Exercise

Click the Exercise link below to perform an exercise to get database metadata.
JDBC Methods - Exercise