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

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

    Java 8 and later: JDBCType & SQLType

    With improvements in the API's, as of Java 8 and JDBC 4.2, we have JDBCType and SQLType, and in the same spirit as some of the other examples can be simply used as follows:

    String typeName = JDBCType.valueOf(-5).getName();
    

    But of course, why using the numeric types to begin with. Make a habit, and switch over from numeric's to the enum constants defined in JDBCType:

    String typeName = JDBCType.BIGINT.getName();
    

    et voilà!

    However, that might not be enough to have something good enough for using in a DDL ... you might need to implement vendor specific translation. As an example, you might need to consider to translate VARCHAR to VARCHAR2 in the case of Oracle.

提交回复
热议问题