Knowing about your Database

We use the Jdbc APIs for accessing the data from the database system. However, the different databases from different vendors will vary a lot in their underlying model and functionalities. For example, a feature supported in one database might not be supported in another database. So, even before working with a database, it is important to know the supported features and other related information. Java provides an interface called DatabaseMetaData to achieve this.

also read:

The implementation for this interface will give the Driver vendor itself and the information obtained from this interface will help both the Tool Vendors and Application Developers. Let us see how to get a reference to this interface,

Class.forName('sun.jdbc.odbc.JdbcOdbcDriver');
Connection connection = DriverManager.getConnection("jdbc:odbcEmployeeDS");
DatabaseMetaData metadata  = connection.getMetaData();

Now, let us see how to make use of this metadata interface to get information about the database. Consider the following code snippet which prints the product name and version information related to a database.

System.out.println(metaData.getDatabaseProductName());
System.out.println(metaData.getDatabaseProductVersion());
System.out.println(metaData.getDatabaseMajorVersion());
System.out.println(metaData.getDatabaseMinorVersion());

It is important to understand that the methods may or may not support the requested operation. Say, for example, if a particular database doesn’t have the notion of minor or major version for its product, then they possibly return null or may throw UnsupportedOperationException at the run-time.

This metadata interface has a huge list of 'supports*' method to check whether a particular feature or an operation is supported by the database. For example, consider the following code snippet,

System.out.println(metaData.supportsAlterTableWithDropColumn());
System.out.println(metaData.supportsColumnAliasing());
System.out.println(metaData.supportsGroupBy());
System.out.println(metaData.supportsTransactions());
System.out.println(metaData.supportsMultipleTransactions());
System.out.println(metaData.supportsOuterJoins());
System.out.println(metaData.supportsSavepoints());

The first statement will return true if the database supports dropping of a column while making modifications to the table structure. The second statement returns true if the column names are referred through multiple identifiers, which is called as column aliasing. The third one will return true if the GROUP BY clause can be used in SQL query. The fourth and the fifth statements are related to supporting single and multiple transactions. The seventh statement will return true if outer joins can be mentioned in a query. And the final statement returns true if the database supports save points which refers to some point in a database session, after which all the statements can be committed or can be roll backed.

In this section, we have discussed only a very few methods as there are so may methods available in this interface. To know about this in detail, it is advised to have a glance over the Java documentation.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*