Getting Column Names in JDBC

I was wondering how to determine if a column with a specific name exists in a specific database table. I use JDBC, but if it can be done using pure SQL, it is even better. However, the solution must be independent of the DBMS provider. I think I could do this by querying the first row of the table and getting ResultSetMetaData, but this assumes there is a row in the table. I would like him to work with an empty table. Thanks in advance!

+4
source share
3 answers

You can get them from DatabaseMetaData .

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

It doesn't matter if the table is empty. ResultSetMetaData will still provide you with information about the types and properties of the columns in the ResultSet object.

+2
source

You can get general information about the structure of the database using the java.sql.DatabaseMetaData interface.

 DatabaseMetaData dbmeta = con.getMetaData(); 

call getColumns () to get a description of the available table columns.

+2
source

All Articles