473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Mar 30 '07 #1
6 9951
joerg wrote:
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

--
Well, there's egg and bacon; egg sausage and bacon; egg and
tr**********@spamonly.de; egg bacon and spam; egg bacon sausage
and tr**********@spamonly.net; spam bacon sausage and spam; spam
egg spam spam bacon and tr**********@nixwill.de ; spam sausage
Mar 30 '07 #2
On Mar 30, 8:44 am, Bernd Hohmann <trap20070...@spamonly.netwrote:
joerg wrote:
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
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

Mar 30 '07 #3
joerg wrote:
>Before retrieving metadata you must perform a query to the database.

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

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 :(
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

--
Well, there's egg and bacon; egg sausage and bacon; egg and
tr**********@spamonly.de; egg bacon and spam; egg bacon sausage
and tr**********@spamonly.net; spam bacon sausage and spam; spam
egg spam spam bacon and tr**********@nixwill.de ; spam sausage
Mar 30 '07 #4
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

Mar 30 '07 #5
joerg wrote:
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

--
Well, there's egg and bacon; egg sausage and bacon; egg and
tr**********@spamonly.de; egg bacon and spam; egg bacon sausage
and tr**********@spamonly.net; spam bacon sausage and spam; spam
egg spam spam bacon and tr**********@nixwill.de ; spam sausage
Mar 30 '07 #6
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
joerg wrote:
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
Mar 30 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Arjen | last post by:
IE 6 does not return the value of a dynamic option list. Mozilla performs fine. How can I get the value of a choice in IE? Simple example below. Option list is filled with first element of array...
4
by: P Adhia | last post by:
Hello, If the explain shows that DB2 needs to sort the result of a cursor, does that always happen? i.e. if the resultset of the cursor is empty, does the sort have any overhead? It appears...
1
by: rafikki3 | last post by:
I have a java web application that is using the db2jcc driver to get a connection to a DB2 database that is located on an RS 6000 server. The here is a portion of the code that is giving me a...
1
by: Crazy Cat | last post by:
Hi, whenever the underlying query being called by EXEC in the following has an empty result set I get the following error -- Invalid Data for 'Numeric' when EXEC returns empty row. However if I...
1
by: dave | last post by:
I am having a problem i bleieve a number of other individuals have had before. I have tried the solutions found on the net however I still have the problem. I am hosting a web site on an...
12
by: robertino | last post by:
Hi all, I've put together a few SPs to produce a BOM (bill of materials) listing, which together use a couple of global temp tables, and return the results from a cursor. Here's the code: ...
2
by: stigwulff | last post by:
I have made a ASP.NET ver. 2 webservice that runs on an Exchange 2003 server. In that webservice I want to get the size of the mailboxes on the Exchange server. I try to use WMI and the...
6
by: unpresedented | last post by:
I have this query (that runs on Oracle 10g database): SELECT ge.*, ge.concept AS glossarypivot FROM s_glossary_entries ge WHERE (ge.glossaryid = '161' OR ge.sourceglossaryid = '161') AND...
5
by: mike j | last post by:
Hi everyone, If anyone could help me with this problem I'd appreciate it greatly. Basically I have a simple table on my database. It is linked with a php form that I created on my site. The php...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.