Java – how to check whether column names exist in cachedrowset?
I am viewing data in views that may change I need to know before I make a CRS Whether the column exists before get ****************** I found that I can query such metadata to see if a column exists before requesting data
ResultSetMetaData Meta = crs.getMetaData(); int numCol = Meta.getColumnCount(); for (int i = 1; i < numCol+1; i++) if(Meta.getColumnName(i).equals("name")) return true;
Is there a simpler way to check whether columns exist?
Editor: it must be database agnostic This is why I refer to the cachedrowset instead of the database
Solution
The general JDBC API is not a simple method (at least not what I know, or can find... There is exactly the same code in my local tool set.)
(your code is incomplete):
ResultSetMetaData Meta = crs.getMetaData(); int numCol = Meta.getColumnCount(); for (int i = 1; i < numCol+1; i++) { if(Meta.getColumnName(i).equals("name")) {return true;} } return false;
That said, if you use proprietary, database specific APIs and / or SQL queries, I'm sure you can find a more elegant way to do the same... But you have to write custom code for each database If I were you, I would insist on using JDBC API
Is there anything about your proposed solution that makes you think it is incorrect? It seems simple to me