Tutorialspoint

1 Answer
Arushi

The SQL UNION clause/operator is used to combine the results of two or more SELECT statements without returning any duplicate rows.

To use this UNION clause, each SELECT statement must have

  • The same number of columns selected
  • The same number of column expressions
  • The same data type and
  • Have them in the same order

But they need not have to be in the same length.

The basic syntax of a UNION clause is as follows −

Syntax

SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
UNION
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]

The supportsUnion() method of the DatabaseMetaData interface is used to determine whether the underlying database supports SQL UNION clause/operator.

This method returns a boolean value which is −

  • True, when the underlying database supports SQL UNION clause/operator.
  • False, when the underlying database doesn't support SQL UNION clause/operator.

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 supportsUnion() 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 UNION clause/operator else it doesn't.

Following JDBC program establishes connection with MySQL database, and determines and prints whether it supports SQL UNION clause/operator.

Example

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseMetadata_supportsUnion {
   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 supports SQL UNION clause
      boolean bool = metaData.supportsUnion();
      if(bool) {
         System.out.println("Underlying database supports SQL UNION clause");
      } else {
         System.out.println("Underlying database doesnot support SQL UNION clause");
      }
   }
}

Output

Connection established......
Underlying database supports SQL UNION clause

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.