简体   繁体   English

如何在没有ResultSet的情况下获得等效的ResultSetMetaData

[英]How to get equivalent of ResultSetMetaData without ResultSet

I need to resolve a bunch of column names to column indexes (so as to use some of the nice ResultSetMetaData methods). 我需要将一堆列名解析为列索引(以便使用一些很好的ResultSetMetaData方法)。 However, the only way that I know how to get a ResultSetMetaData object is by calling getMetaData() on some ResultSet . 但是,我知道如何获取ResultSetMetaData对象的唯一方法是在某个ResultSet上调用getMetaData()

The problem I have with that is that grabbing a ResultSet takes up uneccesary resources in my mind - I don't really need to query the data in the table, I just want some information about the table. 我遇到的问题是抓住一个ResultSet占据了我心中的不必要的资源 - 我真的不需要查询表中的数据,我只想要一些关于表的信息。

Does anyone know of any way to get a ResultSetMetaData object without getting a ResultSet (from a potentially huge table) first? 有没有人知道如何获得ResultSetMetaData对象而不首先获得ResultSet (来自可能很大的表)?

Maybe you could use 也许你可以使用

DatabaseMetaData databaseMetaData = connection.getMetaData();
databaseMetaData.getColumns(null, null, tableName, "%");

It returns one row for each table column. 它为每个表列返回一行。

In this case you'd use the returned ResultSet itself, not its ResultSetMetaData . 在这种情况下,您将使用返回的ResultSet本身,而不是其ResultSetMetaData

One advantage of this approach is, that it doesn't interfere with database locking and transactions. 这种方法的一个优点是,它不会干扰数据库锁定和事务。

Assuming you're doing a select * from mytable you could just add a where clause that ensures no records will be returned and the ResultSet will be empty? 假设您正在select * from mytable执行select * from mytable您可以添加一个where子句,以确保不会返回任何记录并且ResultSet将为空?

That way you are still just getting the metadata for the table you are interested in instead of the entire database. 这样您仍然只是获取您感兴趣的表的元数据而不是整个数据库。

Another solution 另一种方法

select * from mytable limit 0; select * from mytable limit 0;

Then the query dont get any data. 然后查询不会得到任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM