J2EEOnline J2EE

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 that database. A DatabaseMetaData object can help provide that information. 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:
sql - Query
SQL Query

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.


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