如何在JDBC的CachedRowSet中檢查列名是否存在?


CachedRowSet介面沒有提供任何方法來確定特定列是否存在。

因此,要查詢**RowSet**是否包含特定列,需要將RowSet中每一列的名稱與所需名稱進行比較。為此,需要:

  • 使用**getMetaData()**方法從RowSet檢索**ResultSetMetaData**物件。
ResultSetMetaData meta = rowSet.getMetaData();
  • 使用**getColumnCount()**方法獲取**RowSet**中的列數。
int columnCount = meta.getColumnCount();
  • **getColumnName()**方法返回指定索引的列名。使用此方法從索引1到列數檢索RowSet的列名,並將每一列的名稱與所需的列名進行比較。
boolean flag = false;
for (int i = 1; i <=columnCount; i++) {
   if(meta.getColumnName(i).equals("ProductName")) {
      flag = true;
   }
}

示例

讓我們在MySQL資料庫中建立一個名為**sales**的表,其中一列為自動遞增列,使用如下所示的CREATE語句:

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

現在,我們將使用INSERT語句在**sales**表中插入5條記錄:

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');

下面的JDBC程式建立與資料庫的連線,將Sales表的內容檢索到**RowSet**中,並找出它是否包含名為ProductName的列。

示例

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:///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("");
      }
   }
}

輸出

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

更新於:2019年7月30日

3K+ 瀏覽量

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.