- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java DatabaseMetaData supportsGroupBy() method with example
The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups. This GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause.
Syntax
The basic syntax of a GROUP BY clause is shown in the following code block. The GROUP BY clause must follow the conditions in the WHERE clause and must precede the ORDER BY clause if one is used.
SELECT column1, column2 FROM table_name WHERE [ conditions ] GROUP BY column1, column2 ORDER BY column1, column2
The supportsGroupBy() method of the DatabaseMetaData interface is used to determine whether the underlying database supports SQL GROUP BY clause.
This method returns a boolean value which is −
- True, when the underlying database supports SQL GROUP BY clause.
- False, when the underlying database doesn't support SQL GROUP BY clause.
To determine whether the underlying database supports SQL GROUP BY clause −
- Make sure your database is up and running.
- Register the driver using the registerDriver() method of the DriverManager class. Pass an object of the driver class corresponding to the underlying database.
- Get the connection object using the getConnection() method of the DriverManager class. Pass the URL the database and, user name, password of a user in the database, as String variables.
- Get the DatabaseMetaData object with respect to the current connection using the getMetaData() method of the Connection interface.
- Finally, invoke the supportsGroupBy() method of the ResultSetMetaData interface and save the value it returns in a boolean variable say, bool. If this value is true, underlying database supports SQL GROUP BY clause else it doesn't.
Following JDBC program establishes connection with MySQL database, and determines and prints whether it supports SQL GROUP BY clause.
Example
import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.SQLException; public class DatabaseMetadata_supportsGroupBy { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String url = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(url, "root", "password"); System.out.println("Connection established......"); //Retrieving the meta data object DatabaseMetaData metaData = con.getMetaData(); //Determining whether the underlying database support SQL GROUPBY clause boolean bool = metaData.supportsGroupBy(); if(bool) { System.out.println("Underlying database supports SQL GROUP BY clause"); } else { System.out.println("Underlying database doesn't support SQL GROUP BY clause"); } } }
Output
Connection established...... Underlying database supports SQL GROUP BY clause