How do I check to see if a column name exists in a CachedRowSet in JDBC?


CachedRowSet interface does not provide any methods to determine whether a particular column exists.

Therefore, to find whether a RowSet contains a specific column, you need to compare the name of each column in the RowSet with the required name. To do so −

  • Retrieve the ResultSetMetaData object from the RowSet using the getMetaData() method.
ResultSetMetaData meta = rowSet.getMetaData();
  • Get the number of columns in the RowSet using the getColumnCount() method.
int columnCount = meta.getColumnCount();
  • The getColumnName() method returns the name of the column of the specified index. Using this method retrieve the column names of the RowSet from index 1 to column count and compare name of the each column, with the required column name.
boolean flag = false;
for (int i = 1; i <=columnCount; i++) {
   if(meta.getColumnName(i).equals("ProductName")) {
      flag = true;
   }
}

Example

Let us create a table with name sales in MySQL database, with one of the columns as auto-incremented, using CREATE statement as shown below −

CREATE TABLE Sales(
   ID INT PRIMARY KEY AUTO_INCREMENT,
   ProductName VARCHAR (20),
   CustomerName VARCHAR (20),
   DispatchDate date,
   DeliveryTime time,
   Price INT,
   Location VARCHAR(20)
);

Now, we will insert 5 records in sales table using INSERT statements −

insert into sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) values('Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'India');
insert into sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) values('Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam');
insert into sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) values('Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada');
insert into sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) values('Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai');
insert into sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) values('Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');

Following JDBC program establishes connection with the database, retrieves the contents of the Sales table into the RowSet and finds out whether the it contains the column named ProductName.

Example

import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
public class CachedRowSetExample {
   public static void main(String args[]) throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Creating the RowSet object
      RowSetFactory factory = RowSetProvider.newFactory();
      CachedRowSet rowSet = factory.createCachedRowSet();
      //Setting the URL
      String mysqlUrl = "jdbc:mysql://localhost/SampleDB";
      rowSet.setUrl(mysqlUrl);
      //Setting the user name
      rowSet.setUsername("root");
      //Setting the password
      rowSet.setPassword("password");
      //Setting the query/command
      rowSet.setCommand("select * from Sales");
      //Executing the command
      rowSet.execute();
      //Retrieving the ResultSetMetaData object
      ResultSetMetaData meta = rowSet.getMetaData();
      int columnCount = meta.getColumnCount();
      boolean flag = false;
      for (int i = 1; i <=columnCount; i++) {
         if(meta.getColumnName(i).equals("ProductName")){
               flag = true;
         }
      }
      if(flag) {
         System.out.println("Specified column exist");
      } else {
         System.out.println("Specified column does not exists");
      }
      System.out.println("Contents of the row set");
      while(rowSet.next()) {
         System.out.print("ID: "+rowSet.getInt("ID")+", ");
         System.out.print("Product Name: "+rowSet.getString("ProductName")+", ");
         System.out.print("Customer Name: "+rowSet.getString("CustomerName")+", ");
         System.out.print("Dispatch Date: "+rowSet.getDate("DispatchDate")+", ");
         System.out.print("Delivery Time: "+rowSet.getTime("DeliveryTime")+", "););
         System.out.print("Price: "+rowSet.getString("Price")+", ");
         System.out.print("Location: "+rowSet.getString("Location"));
         System.out.println("");
      }
   }
}

Output

Specified column exists
Contents of the row set
ID: 1, Product Name: Key-Board, Customer Name: Raja, Dispatch Date: 2019-09-01, Delivery Time: 05:30:00, Price: 2000, Location: Hyderabad
ID: 2, Product Name: Earphones, Customer Name: Roja, Dispatch Date: 2019-05-01, Delivery Time: 05:30:00, Price: 2000, Location: Vishakhapatnam
ID: 3, Product Name: Mouse, Customer Name: Puja, Dispatch Date: 2019-03-01, Delivery Time: 05:29:59, Price: 3000, Location: Vijayawada
ID: 4, Product Name: Mobile, Customer Name: Vanaja, Dispatch Date: 2019-03-01, Delivery Time: 04:40:52, Price: 9000, Location: Chennai
ID: 5, Product Name: Headset, Customer Name: Jalaja, Dispatch Date: 2019-04-06, Delivery Time: 18:38:59, Price: 6000, Location: Goa

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements