0
votes

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?

1
Have you tried using getColumnLabel() instead? The result set returned by getTables() is not backed by any real table, not directly anyway, so column names may not exist, though labels probably do.mustaccio
Please edit your question to add the missing fact - which platform of Db2 (Z/OS, i-series, linux/unix/windows) are you connecting to? Also, you are using V10.5 fixpack0 (GA) jdbc driver, which is out of support. Please retry with a current jdbc driver version (v11.5.4 or higher).mao
This driver (4.13.127) is from very old Db2 version - 10.1 GA (even with no fixes). I can't reproduce such a result with my 4.25.1301, for example. DB2 JDBC Driver Versions and Downloads. What's your current Db2 version? Can you try a JDBC driver which corresponds to your version instead?Mark Barinstein
That was a driver version problem, thanks!krakig

1 Answers

0
votes

See this IBM technote for information on using the Db2 specific driver property, useJDBC4ColumnNameAndLabelSemantics.