Accessing data of various SQL types
Before I ask my question, here's an exerpt of a program I'm using to create a JTable that shows the result of my query:
private Vector getNextRow( ResultSet rs,
ResultSetMetaData rsmd )
throws SQLException
{
Vector currentRow = new Vector();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
switch( rsmd.getColumnType( i ) ) {
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
currentRow.addElement( rs.getString( i ) );
break;
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
currentRow.addElement(
new Long ( rs.getLong( i ) ) );
break;
case Types.BIGINT:
currentRow.addElement(
new Long ( rs.getLong( i ) ) );
break;
case Types.FLOAT:
case Types.DOUBLE:
currentRow.addElement(
new Double( rs.getDouble( i ) ) );
break;
default:
System.out.println( "Type was: " +
rsmd.getColumnTypeName( i ) );
}
return currentRow;
}
Now, my question is: What if the data source contains data of type CURRENCY or DATETIME? What is the code I need to add to handle the other data types not listed above?
For instance, I need to add "case Types.CURRENCY:
currentRow.addElement ( ..."something"...)What is that "something". The same goes for Types.DATETIME.
Thanks in advance.
[1436 byte] By [
ham17151] at [2007-9-26 2:35:09]

hello,
here hope this mehtod helps
private Class getColumnClass(int column,String dummy) {
//This is an overriden function for getting the real column class
// because some datatypes returned from the original method dont allow editing by the renderer
// ex. datetime
int type;
try {
type = i_metaData.getColumnType(column+1);
}
catch (SQLException e) {
return super.getColumnClass(column);
}
switch(type) {
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
return String.class;
case Types.BIT:
return Boolean.class;
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
return Integer.class;
case Types.BIGINT:
return Long.class;
case Types.FLOAT:
case Types.DOUBLE:
return Double.class;
case Types.DATE:
return java.sql.Date.class;
case Types.TIMESTAMP: //Date Time
return java.sql.Date.class;
case Types.DECIMAL: // Currency,Decimal
return Float.class;
default:
return Object.class;
}
}
here i did this to your code
private Vector getNextRow( ResultSet rs, ResultSetMetaData rsmd ) throws SQLException {
Vector currentRow = new Vector();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
switch(rsmd.getColumnType(i)) {
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
currentRow.addElement(rs.getString(i));
break;
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
currentRow.addElement(new Long(rs.getLong(i)));
break;
case Types.BIGINT:
currentRow.addElement(new Long(rs.getLong(i)));
break;
case Types.FLOAT:
case Types.DOUBLE:
currentRow.addElement(new Double( rs.getDouble(i)));
break;
//START: Added this..types
case Types.TIMESTAMP:
currentRow.addElement(new java.sql.Date.class(rs.getTimestamp));
break;
case Types.DECIMAL:
currentRow.addElement(new Float.class(rs.getDecimal));
break;
//END:
default:
System.out.println("Type was: " + rsmd.getColumnTypeName(i));
}
return currentRow;
}