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

前端 未结 8 1052
情歌与酒
情歌与酒 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:56

    Spring has a handy helper Enum called JdbcTypesEnum, but it is indeed quite strange, that this is not part of JDBC proper. However, instead of using

    rs = connection.getMetaData().getColumns();
    ...
    int dataType = rs.getInt("DATA_TYPE");
    

    I would use

    String typeName = rs.getString("TYPE_NAME");
    

    when retrieving the column type. For example when inspecting a H2 database table with a special VARCHAR_IGNORECASE or UUID type:

                        dataType   vs. typeName
    UUID:               -2=BINARY  vs. "UUID"
    VARCHAR_IGNORECASE: 12=VARCHAR vs. "VARCHAR_IGNORECASE"
    

    But note, that you cannot cover the type range of the string for all databases, in this case the int would be somewhat more handy (but it is after all not a closed Enum type).

提交回复
热议问题