I'm trying to format a decimal before saving it within a hashmap of strings (compared to table names).
Can anyone tell me how I would compare the results set column name datatype with the datatype String.
The closet I can get is:
if(metaData.getColumnClassName(i).equals(String.class.getClass()))
My Code:
while(mResults.next())
{
for (int i = 1; i <= colCount; i++)
{
//ERROR HERE. Try to see if it's string for decimal format on float.
if(mResults.getObject(metaData.getColumnName(i)).equals(String.class))
{
System.out.println("Should be 3.");
}
SQLResults.put(metaData.getColumnName(i), mResults.getString(metaData.getColumnName(i)));
}
}
Use the ResultSet.getMetadata() method to obtain ResultSetMetaData which provides the column Type (and other information, such as numeric precision). You can then convert as necessary. The column type is returned as a java.sql.Types int constant, not a Class object.
if(mResults.getObject(metaData.getColumnName(i)).equals(String.class))
How could your object equal String
class. you can get Object#getClass#getName
what will return class Name of actaul Object Type
that can be String
or Integer
. Something like -
if(mResults.getObject(metaData.getColumnName(i)).getClass().getName.equals("java.lang.String")){}
But still it does not make any sence, where it will be useful.
have you tried like:
if(metaData.getColumnType(i) == Types.VARCHAR)
where metadata
is of type: ResultSetMetaData
if(metaData.getColumnClassName(i).equals("java.lang.Float"))
{
SQLResults.put(metaData.getColumnName(i),
mDF.format(mResults.getFloat(metaData.getColumnName(i))));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.