开发者

How to get equivalent of ResultSetMetaData without ResultSet

开发者 https://www.devze.com 2022-12-25 04:18 出处:网络
I need to resolve a bunch of column names to column indexes (so as to us开发者_StackOverflowe some of the nice ResultSetMetaData methods). However, the only way that I know how to get a ResultSetMetaD

I need to resolve a bunch of column names to column indexes (so as to us开发者_StackOverflowe some of the nice ResultSetMetaData methods). However, the only way that I know how to get a ResultSetMetaData object is by calling getMetaData() on some ResultSet.

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.

Does anyone know of any way to get a ResultSetMetaData object without getting a ResultSet (from a potentially huge table) first?


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.

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?

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;

Then the query dont get any data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号