Developer's Daily | Java Education |
front page | java | perl | unix | DevDirectory |
Front
Page Java Education Pure Java Articles |
|
Question: | How do I determine the column type when querying a database? | ||||||||||||||||||||||||||||||||||||||||||
Recipe: | Here's how to get the column
type:
//-------------------------------------------------// // Here's how to determine a column's data type. // // This example returns an integer, and the // // integer indicates the column's data type. // //-------------------------------------------------// Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * from Customer"); int colType = rs.getColumnType(1); System.out.println("Column 1 is type " + colType); |
||||||||||||||||||||||||||||||||||||||||||
Discussion: | You should use the ResultSetMetaData getColumnType()
and getColumnTypeName() methods to determine the data type of
a column of information returned in a ResultSet.
Be aware that getColumnType just returns an integer, so you'll
need to know what that integer means. According to the JDK docs, the integer
returned corresponds to generic SQL data types as follows:
|
||||||||||||||||||||||||||||||||||||||||||
Notes: | <none> | ||||||||||||||||||||||||||||||||||||||||||
Keywords: | Java, JDBC, ResultSet, ResultSetMetaData, getColumnType, getColumnTypeName |
Copyright 1998-2009 Alvin Alexander, devdaily.com
All Rights Reserved.