如何在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://127.0.0.1/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
广告