Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using metadata to get COLUMN_SIZE

Status
Not open for further replies.

adrianjohnson

Programmer
May 16, 2002
145
GB
I've got some code which displays a list of database tables in a tree, and when a node is selected, the fields are listed underneath the table name.

I'd like to add the data type and size to the list of fields, so that it would read: field type size

Here's my code:

Code:
// Get all the columns for the current table
ResultSet columnNames = metadata.getColumns(null, null, tableName, null);

// Add nodes for the columns as children of the table node
while(columnNames.next())
{
  colName = columnNames.getString("COLUMN_NAME");
  colType = columnNames.getString("DATA_TYPE");
  colSize = columnNames.getInt("COLUMN_SIZE");
  colData = colName + " " + colType + " " + colSize;
  tableNode.add(new DefaultMutableTreeNode(colData));
}

Variables colName, colType and colData are declared as strings, whereas colSize is an int.

When I run the code I get a "Column not found." If I remove all references to colSize (and COLUMN_SIZE) then it works and I get a list of tables.

Where am I going wrong?

(If I need to provide further code, let me know).

Thanks,

Adrian Johnson

I'm using Eclipse 3.1, with JRE 5.0 as my default, but also have 1.4.2 installed.

Adrian Johnson
Assystance - i.t. solutions
 
Only the line which accesses the COLUMN_SIZE field. I mean, we all agree the code looks correct and also can access SOME of the metadata recordset fields. So
1) The JDBC driver is duff (an old version or just doesn't support metadata properly).
2) You aren't looking for the correct field. Since the Javadoc says it exists and I've run code which CAN access this field, then you must have misnamed it in your code. It LOOKS okay but it's possible for a non-visible control code to be stuck within your field name (I think - I may be being paranoid!).

So, just retype that one line to eliminate 2). Then try a different database through a tried-and-trusted native JDBC driver.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
I've got a solution. I replaced:

Code:
columnSize = columnNames.getString("COLUMN_SIZE");

with

Code:
columnSize = columnNames.getInt(7);

and it works!

Thanks for all the help and comments.

Adrian

Adrian Johnson
Assystance - i.t. solutions
 
I told you that some time before. You should read the answers to your posts :p

Cheers,
Dian
 
[bigsmile]

Of course, that solution will rely on the COLUMN_SIZE field always being the 7th in the MetaData ResultSet. I'm not sure if the JDBC Spec. mandates any absolute positioning of these fields. I suppose it could be possible for different JDBC drivers to have them in different orders. Just a thought.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Well, proved that his JDBC driver doesn't even maintain the column name, who knows what can happen with the number.

Anyway, I'm not sure the metadata is driver independent. I use to rely just on SQL.

Generally (at least me) you design an application for a system, and that includes the DB, and no one expects to migrate without doing some adaptations, and that can include some DB access modification.

I've done that query to column names just a few days ago, but I knew I was using DB2 and wnte directly to SYSTABLES, more reliable than metadata.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top