jdbc: Get the SQL Type Name from java.sql.Type code

前端 未结 8 1010
情歌与酒
情歌与酒 2021-02-01 14:56

I have an array with Field Names and jdbc Type codes. (Those int codes that you can find in

http://download.oracle.com/javase/1.4.2/docs/api/constant-values.html#java.s

8条回答
  •  囚心锁ツ
    2021-02-01 15:46

    To specifically answer "Get the SQL Type Name from java.sql.Type code", if you are using a version of java that can do reflection, here is a small utility method that pretty much does the same thing:

    public Map getAllJdbcTypeNames() {
    
        Map result = new HashMap();
    
        for (Field field : Types.class.getFields()) {
            result.put((Integer)field.get(null), field.getName());
        }
    
        return result;
    }
    

    Add import java.lang.reflect.Field; to your import declarations. Once you have that in place, simply use it as follows:

    ...
    Map jdbcMappings = getAllJdbcTypeNames();
    
    String typeName = jdbcMappings.get(-5); // now that will return BIGINT
    ...
    

提交回复
热议问题