简体   繁体   中英

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 (ie 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.

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");

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM