• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Knowing about your Database

October 26, 2007 //  by Krishna Srinivasan//  Leave a Comment

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:

  • Java Tutorials
  • Java EE Tutorials
  • Design Patterns Tutorials
  • Java File IO Tutorials

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.

Category: JavaTag: Database

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.

Previous Post: « Customizing Dragging and Dropping for Swing Components
Next Post: Life Cycle Management of a Spring Bean »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact