my dog learned polymorphism
The moose likes JDBC and Relational Databases and the fly likes getting column type info from db Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "getting column type info from db" Watch "getting column type info from db" New topic

getting column type info from db

Bobby Anderson
Ranch Hand

Joined: Oct 28, 2008
Posts: 114
I want to get the java.sql.Types info from the DB for a given column name.

is this really the best way to do this:

DatabaseMetaData dbMeta = conn.getMetaData();
ResultSet cols = dbMeta.getColumns(null, null, tablename, columnname);
ResultSetMetaData rsMeta = cols.getMetaData();
int colType = rsMeta.getColumnType(0); // Hopefully only have one row

This seems very ugly! Is there an easier way. I am also not checking for null objects along the way which I should probably do which would make this more ugly.

Any ideas?

Vinod K Singh
Ranch Hand

Joined: Sep 30, 2008
Posts: 198
ResultSetMetaData is the standard way to get such information. BTW indexes start from 1 not from 0.

My Blog
I agree. Here's the link:
subject: getting column type info from db
It's not a secret anymore!