I was wondering how to tell if a column with a certain name exists in a certain database table. I'm using JDBC but if it can be done with pure SQL, it's even better. The solution has to be independent of the DBMS-provider however. I guess I could do that by querying the first row of the table and getting ResultSetMetaData from it, but that assumes there is a row in a table. I would want it to work with an empty table too. Thanks in advance!
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can get them from DatabaseMetaData
.
DatabaseMetaData meta = connection.getMetaData();
ResultSet rs = meta.getColumns(...);
回答2:
It doesn't matter if the table is empty. ResultSetMetaData will still give you information about the types and properties of the columns in a ResultSet object.
回答3:
You can retrieve general information about the structure of a database with the java.sql.DatabaseMetaData interface.
DatabaseMetaData dbmeta = con.getMetaData();
call getColumns(), to get description of table columns available.