I am using the jdbc driver for IBM DB2 Z/OS(version 4.13.127) and I am trying to get table information from the metadata. I am having a different behaviour from what I was expecting : instead of retrieving the column names, I get the column indexes.
ds=new com.ibm.db2.jcc.DB2SimpleDataSource();
((com.ibm.db2.jcc.DB2BaseDataSource) ds).setServerName(ip);
((com.ibm.db2.jcc.DB2BaseDataSource) ds).setPortNumber(portNumber);
((com.ibm.db2.jcc.DB2BaseDataSource) ds).setDatabaseName(databaseName);
((com.ibm.db2.jcc.DB2BaseDataSource) ds).setDriverType(4);
con = ds.getConnection(user, password);
DatabaseMetaData metadata = con.getMetaData();
rs = metadata.getTables(null, "MY_SCHEMA", "MY_TABLE", null);
int columnsNumber = rs.getMetaData().getColumnCount();
while (rs.next()) {
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(columnValue + " " + rs.getMetaData().getColumnName(i));
}
}
The result is printed below :
null 1, MY_SCHEMA 2, MY_TABLE 3, TABLE 4, 5, null 6, null 7, null 8, null 9, null 10
I was expecting the getColumnName
method to return the string attributes (table_name, schema_name, ...).
Is it specific to the db2 jdbc driver?
getColumnLabel()
instead? The result set returned bygetTables()
is not backed by any real table, not directly anyway, so column names may not exist, though labels probably do. – mustaccio