getting column names in JDBC

2019-05-07 09:20发布

问题:

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.



标签: java sql jdbc