java 在 JDBC 中获取列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6896737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting column names in JDBC
提问by mikkis
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!
我想知道如何判断某个数据库表中是否存在具有某个名称的列。我正在使用 JDBC,但如果它可以用纯 SQL 完成,那就更好了。但是,该解决方案必须独立于 DBMS 提供商。我想我可以通过查询表的第一行并从中获取 ResultSetMetaData 来做到这一点,但前提是表中有一行。我也希望它与空表一起工作。提前致谢!
回答by duffymo
You can get them from DatabaseMetaData
.
您可以从DatabaseMetaData
.
DatabaseMetaData meta = connection.getMetaData();
ResultSet rs = meta.getColumns(...);
回答by dogbane
It doesn't matter if the table is empty. ResultSetMetaDatawill still give you information about the types and properties of the columns in a ResultSet object.
表是否为空都没有关系。ResultSetMetaData仍会为您提供有关 ResultSet 对象中列的类型和属性的信息。
回答by Swagatika
You can retrieve general information about the structure of a database with the java.sql.DatabaseMetaData interface.
您可以使用 java.sql.DatabaseMetaData 接口检索有关数据库结构的一般信息。
DatabaseMetaData dbmeta = con.getMetaData();
call getColumns(), to get description of table columns available.
调用 getColumns(),获取可用表列的描述。