简体   繁体   English

在JDBC中获取列名

[英]getting column names in JDBC

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. 我正在使用JDBC,但如果可以使用纯SQL,那就更好了。 The solution has to be independent of the DBMS-provider however. 但是,解决方案必须独立于DBMS提供者。 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. 我想我可以通过查询表的第一行并从中获取ResultSetMetaData来做到这一点,但这假设表中有一行。 I would want it to work with an empty table too. 我希望它也能用于空表。 Thanks in advance! 提前致谢!

You can get them from DatabaseMetaData . 您可以从DatabaseMetaData获取它们。

DatabaseMetaData meta = connection.getMetaData();
ResultSet rs = meta.getColumns(...);

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. ResultSetMetaData仍将为您提供有关ResultSet对象中列的类型和属性的信息。

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(),以获取可用的表列的描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM