tags:

views:

124

answers:

2

I need to resolve a bunch of column names to column indexes (so as to use 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 RSMD object without getting a ResultSet (from a potentially huge table) first?

+1  A: 

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.

Brabster
Yep, this is what I figured out as well. I ended up using a LIMIT 1 clause instead of the WHERE clause for clarity, but it seems to be doable both ways.
javanix
This is however a bit hacky. Just using the `DatabaseMetaData` is more neat.
BalusC
+3  A: 

Maybe you could use

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

It returns one row for each table column.

(Maybe as a clarification: 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.

Chris Lercher
^^^ This!!! However, with the caveat it puked out on my on Sybase running on HP. This was on 2001 mind you. But in general, this is the way to do it.
luis.espinal
You're correct - looking into this method made it clear that it works in a cleaner manner.
javanix