Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / March 2007

Tip: Looking for answers? Try searching our database.

jdbc on db2: getMetadate().getColumns() returns empty resultset

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
joerg - 30 Mar 2007 07:30 GMT
Hello world,

my program connects to a db2 database and needs to find out the column
names and data types for a specific table.
The code works fine with mysql and derby, but returns an empty
resultset
for the table's metadata with db2.
SELECTs on the connection object work, so there is no problem with the
database connection.

Here's the code:

/* dbxnc is the Connection object */

ResultSet oResults = null;
oResults = dbcnx.getMetaData().getColumns(null, dbName, tableName,
"%");

if (null != oResults)
{
  /* just to find out what's going on ... */
  if (oResults.getType() != ResultSet.TYPE_FORWARD_ONLY)
  {
      if (!oResults.first())
      {
         throw new SQLException("Cannot get first result!");
      }
  }
  else
  {
      /* the next() call fails! */
      if (!oResults.next())
      {
         throw new SQLException("Cannot forward in result!");
      }
  }

  for (;;)
  {
     int iType = oResults.getInt("DATA_TYPE");

     String sThisColName = oResults.getString("COLUMN_NAME");
     System.out.println(sThisColName + " => " + iType);

     if (!oResults.next())
     {
        break;
     }
  }

  oResults.close();
}

Has anyone a idea what I am doing wrong?

Thanks for any help!
Joerg
Bernd Hohmann - 30 Mar 2007 07:44 GMT
> my program connects to a db2 database and needs to find out the column
> names and data types for a specific table.
> The code works fine with mysql and derby, but returns an empty
> resultset for the table's metadata with db2.

Before retrieving metadata you must perform a query to the database.

"select * from mytable fetch first row only" would do the job.

Bernd

Signature

Well, there's egg and bacon; egg sausage and bacon; egg and
trap20070330@spamonly.de; egg bacon and spam; egg bacon sausage
and trap20070330@spamonly.net; spam bacon sausage and spam; spam
egg spam spam bacon and trap20070330@nixwill.de ; spam sausage

joerg - 30 Mar 2007 08:15 GMT
> > my program connects to a db2 database and needs to find out the column
> > names and data types for a specific table.
[quoted text clipped - 6 lines]
>
> Bernd

Hi Bernd,

thanks for the fast reply.

I added a select statement before requesting the metadata.
The select request succeeded and returns the expected data, but
requesting the metadata still fails :(

Any more ideas?

Joerg
Bernd Hohmann - 30 Mar 2007 11:47 GMT
>> Before retrieving metadata you must perform a query to the database.
>>
[quoted text clipped - 5 lines]
> The select request succeeded and returns the expected data, but
> requesting the metadata still fails :(

Hm... You must do a .next() on the ResultSet and then get the Metadata
from the same Resultset. From my code:

stmt = con.createStatement();
res = stmt.executeQuery("select * from "
         + strTableName
         + " fetch first row only");
res.next();
ResultSetMetaData rsmd = res.getMetaData();
for (int i = 0; i < rsmd.getColumnCount(); i++) {
 [...]

If the table is empty, it will fail (as far as I can remember). In DB2
the columnnames for the ResultSetMetaData depends on the query because
you can rename the columname in the query itself like "select id as
'customer_id' from ..." and create additional return values.

I don't know of .getColumns(...) works as expected in your environment.
Try to iterate 0..getColumnCount() and use .getColumnName(i+1) (and the
others) then. Don't forget the fact that the columns start counting from
1 and not from 0.

Bernd

Signature

Well, there's egg and bacon; egg sausage and bacon; egg and
trap20070330@spamonly.de; egg bacon and spam; egg bacon sausage
and trap20070330@spamonly.net; spam bacon sausage and spam; spam
egg spam spam bacon and trap20070330@nixwill.de ; spam sausage

joerg - 30 Mar 2007 11:56 GMT
Thanks Bernd,

using the resultsets metadata works. I always tried to get the column
names from the databases' metadata
which seems not to work (maybe for the reason you described).

Joerg
Bernd Hohmann - 30 Mar 2007 12:21 GMT
> using the resultsets metadata works. I always tried to get the column
> names from the databases' metadata
> which seems not to work (maybe for the reason you described).

Ah... DatabaseMetaData... Thats broken since the JDBC-Driver exists. Or
at least: doesn't work as expected.

Bernd

Signature

Well, there's egg and bacon; egg sausage and bacon; egg and
trap20070330@spamonly.de; egg bacon and spam; egg bacon sausage
and trap20070330@spamonly.net; spam bacon sausage and spam; spam
egg spam spam bacon and trap20070330@nixwill.de ; spam sausage

Phil Sherman - 30 Mar 2007 10:39 GMT
An alternative should be to execute the following query:
    describe select * from tablename

You'll get four columns back:
sqltype        A numeric code and descriptive name for the data type
sqllen        The (maximum) physical length of the stored data
sqlname.data    The column name
sqlname.length    The length of the column name (sqlname.data)

Phil Sherman

> Hello world,
>
[quoted text clipped - 53 lines]
> Thanks for any help!
> Joerg
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.