jdbc:从 java.sql.Type 代码中获取 SQL 类型名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6437790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:55:01  来源:igfitidea点击:

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

javajdbcjython

提问by AndreasT

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

我有一个包含字段名称和 jdbc 类型代码的数组。(您可以在其中找到的那些 int 代码

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

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

I use a level 4 Driver.

我使用 4 级驱动程序。

I can't figure out how to ask the driver for the corresponding SQL (DDL) Type names. It would be useful in jdbc and in native dialects.

我不知道如何向驱动程序询问相应的 SQL (DDL) 类型名称。它在 jdbc 和本地方言中很有用。

I have
(CustomerId, 1) (CustomerName, -8)

我有
(CustomerId, 1) (CustomerName, -8)

and I want

而且我要

(customerId, INT) (customerId, VARCHAR(200))

(customerId, INT) (customerId, VARCHAR(200))

Where can I find functions that help me with that? I am using jdbc in jython via zxJDBC, so I can use all java and python DB API 2.0 functionality.

我在哪里可以找到可以帮助我解决这个问题的函数?我通过 zxJDBC 在 jython 中使用 jdbc,所以我可以使用所有 java 和 python DB API 2.0 功能。

采纳答案by joostschouten

You seem to be using some JDBC metadata methods you have not posted. I believe what you are seeing is the name of the column along with the JDBC type constantfrom which you can derive the column type. Have a look at the java.sql APIto read more on how to get more meta data.

您似乎正在使用一些您尚未发布的 JDBC 元数据方法。我相信您看到的是列的名称以及可以从中派生列类型的 JDBC类型常量。查看java.sql API以了解有关如何获取更多元数据的更多信息。

回答by alexvetter

You need the ResultSetMetaDataobject of your current ResultSet. You can get it with getMetaData(). Iterate over the columns and call foreach column the method

您需要ResultSetMetaData当前ResultSet. 你可以用getMetaData(). 迭代列并调用 foreach 列的方法

  1. getColumnType(i)
  2. getColumnClassName(i)
  3. getColumnTypeName(i)
  1. getColumnType(i)
  2. getColumnClassName(i)
  3. getColumnTypeName(i)

of your ResultSetMetaData.

你的ResultSetMetaData.

irepresents the column number (starting by 1).

i表示列号(从 1 开始)。

回答by Gill

public static String getSqlTypeName(int type) {
    switch (type) {
    case Types.BIT:
        return "BIT";
    case Types.TINYINT:
        return "TINYINT";
    case Types.SMALLINT:
        return "SMALLINT";
    case Types.INTEGER:
        return "INTEGER";
    case Types.BIGINT:
        return "BIGINT";
    case Types.FLOAT:
        return "FLOAT";
    case Types.REAL:
        return "REAL";
    case Types.DOUBLE:
        return "DOUBLE";
    case Types.NUMERIC:
        return "NUMERIC";
    case Types.DECIMAL:
        return "DECIMAL";
    case Types.CHAR:
        return "CHAR";
    case Types.VARCHAR:
        return "VARCHAR";
    case Types.LONGVARCHAR:
        return "LONGVARCHAR";
    case Types.DATE:
        return "DATE";
    case Types.TIME:
        return "TIME";
    case Types.TIMESTAMP:
        return "TIMESTAMP";
    case Types.BINARY:
        return "BINARY";
    case Types.VARBINARY:
        return "VARBINARY";
    case Types.LONGVARBINARY:
        return "LONGVARBINARY";
    case Types.NULL:
        return "NULL";
    case Types.OTHER:
        return "OTHER";
    case Types.JAVA_OBJECT:
        return "JAVA_OBJECT";
    case Types.DISTINCT:
        return "DISTINCT";
    case Types.STRUCT:
        return "STRUCT";
    case Types.ARRAY:
        return "ARRAY";
    case Types.BLOB:
        return "BLOB";
    case Types.CLOB:
        return "CLOB";
    case Types.REF:
        return "REF";
    case Types.DATALINK:
        return "DATALINK";
    case Types.BOOLEAN:
        return "BOOLEAN";
    case Types.ROWID:
        return "ROWID";
    case Types.NCHAR:
        return "NCHAR";
    case Types.NVARCHAR:
        return "NVARCHAR";
    case Types.LONGNVARCHAR:
        return "LONGNVARCHAR";
    case Types.NCLOB:
        return "NCLOB";
    case Types.SQLXML:
        return "SQLXML";
    }

    return "?";
}

回答by Karthic Raghupathi

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:

要专门回答“从 java.sql.Type 代码获取 SQL 类型名称”,如果您使用的是可以进行反射的 java 版本,这里有一个小实用方法,它几乎可以完成相同的事情:

public Map<Integer, String> getAllJdbcTypeNames() {

    Map<Integer, String> result = new HashMap<Integer, String>();

    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:

添加import java.lang.reflect.Field;到您的导入声明。一旦你有了它,只需按如下方式使用它:

...
Map<Integer, String> jdbcMappings = getAllJdbcTypeNames();

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

回答by eckes

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

Spring 有一个方便的助手 Enum 称为JdbcTypesEnum,但确实很奇怪,这不是 JDBC 的一部分。但是,而不是使用

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 databasetable with a special VARCHAR_IGNORECASEor UUIDtype:

检索列类型时。例如,在检查具有特殊或类型的H2 数据库表时:VARCHAR_IGNORECASEUUID

                    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).

但请注意,您不能涵盖所有数据库的字符串类型范围,在这种情况下 int 会更方便一些(但它毕竟不是封闭的 Enum 类型)。

回答by YoYo

Java 8 and later: JDBCType& SQLType

Java 8 及更高版本:JDBCType&SQLType

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

随着 API 的改进,从 Java 8 和JDBC 4.2 开始,我们有了JDBCTypeSQLType,本着与其他一些示例相同的精神,可以简单地使用如下:

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 enumconstants defined in JDBCType:

但是当然,为什么要使用数字类型开始。养成习惯,从数字切换到enum定义的常量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 VARCHARto VARCHAR2in the case of Oracle.

但是,对于在 DDL 中使用足够好的东西来说,这可能还不够……您可能需要实现供应商特定的翻译。例如,在 Oracle 的情况下,您可能需要考虑转换VARCHARVARCHAR2

回答by konfusius

The Apache DdlUtils Libraryhas a convenience class for this: https://db.apache.org/ddlutils/api/org/apache/ddlutils/model/TypeMap.html

阿帕奇DdlUtils图书馆有这一个方便的类: https://db.apache.org/ddlutils/api/org/apache/ddlutils/model/TypeMap.html

String typeName = TypeMap.getJdbcTypeName(typeCode)

The library helps you also with other DDL needs, but doesn't seem to get much attention lately.

该库还可以帮助您满足其他 DDL 需求,但最近似乎没有得到太多关注。