如何检查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; } }
示例
让我们使用CREATE语句在MySQL数据库中创建一个名称为sales的表,其中一列自动递增,如下所示-
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 { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //创建RowSet对象 RowSetFactory factory = RowSetProvider.newFactory(); CachedRowSet rowSet = factory.createCachedRowSet(); //设置URL- String mysqlUrl = "jdbc:mysql://localhost/SampleDB"; rowSet.setUrl(mysqlUrl); //设置用户名 rowSet.setUsername("root"); //设置密码 rowSet.setPassword("password"); //设置查询/命令 rowSet.setCommand("select * from Sales"); //执行命令 rowSet.execute(); //检索ResultSetMetaData对象 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