Get Database Table Column Names using JDBC

Return a List containing all Column Names of a specified Table using JDBC.

Javadoc available at https://www.javatapas.com/docs/javatapas/sql/GetDatabaseTableColumnNames.html


public static List<String> getDatabaseTableColumnNames(Connection conn, String tableName) throws SQLException {

	List<String> columnNames = new ArrayList<>();

	String sql = "SELECT * FROM " + tableName + " WHERE 0 = 1";
	Statement stmt = conn.createStatement();
	ResultSet rset = stmt.executeQuery(sql);

	ResultSetMetaData rsmd = rset.getMetaData();

	int colCount = rsmd.getColumnCount();

	for (int i = 1; i <= colCount; i++){columnNames.add(rsmd.getColumnName(i));}

	stmt.close();
	rset.close();

	return columnNames;

}