Java generic method to get value of type by using switch on type

Viewed 12823

I want to have something like this below (example how I would do this in C#), to get typed value from SQLiteDB:

private T GetValueFromDB<T>(String colName) {
    object returnValue = null;

    switch (typeof(T)) {
        case Boolean:
            returnValue = dbData.getInt(colName) == 1;
            break;

        case Int32:
            returnValue = dbData.getInt(colName);
            break;

        case Int64:
            returnValue = dbData.getLong(colName);
            break;

        case String:
            returnValue = dbData.getString(colName);
            break;
    }
    return (T)returnValue;
}

Is there a possibility (with switch case or if else) to implement it in Java?

3 Answers
Related