File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
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
subject: getting column type info from db