Drew
Drew

Reputation: 1347

How do I write instances of the different typical Java classes?

Apologies for the confusing title. I am not experienced with Java so I lack the right terminology. Basically, I am trying to create a map between Java types (i.e. Class) and JDBC types (as strings). So I am trying to make something like this:

private static final Map<Class, String> javaToJdbcTypeMap;
static
{
    javaToJdbcTypeMap = new HashMap<Class, String>();
    javaToJdbcTypeMap.put(String, "varchar");
    javaToJdbcTypeMap.put(Integer, "int");
    //etc for other common Java types
}

However, String and Integer are giving me problems. Intellij tells me "Expression Expected". Instead of String I tried "".getClass(), and it works, but I feel like that's horrible style and that there should be a straightforward way of doing it.

Upvotes: 1

Views: 41

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201439

The Mapping of JDBC Types to Java Types should help. Also you add ".class" to get the class like javaToJdbcTypeMap.put(String.class, "varchar"); And, since Java 7, you might use the Diamond Operator <> like

javaToJdbcTypeMap = new HashMap<>();
javaToJdbcTypeMap.put(String.class, "varchar");
javaToJdbcTypeMap.put(Integer.class, "int");

Upvotes: 2

Related Questions