简体   繁体   English

如何在不创建表的情况下从 mysql 列定义中获取 java.sql.Types?

[英]How to get java.sql.Types from mysql column define without creating the table?

goal目标

Get java.sql.Types without creating table.在不创建表的情况下获取 java.sql.Types。

detail细节

If I define a column like this " a JSON DEFAULT NULL"如果我定义这样的列“ a JSON DEFAULT NULL”

How can I get java.sql.Types about column 'a'?如何获得 java.sql.Types 关于“a”列?

what I have tried我试过的

I use jdbc, so I use connection.getMetaData().getTypeInfo to get all types我使用jdbc,所以我使用connection.getMetaData().getTypeInfo来获取所有类型

    public static Map<String, Integer> load(final DatabaseMetaData databaseMetaData) throws SQLException {
    Map<String, Integer> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    try (ResultSet resultSet = databaseMetaData.getTypeInfo()) {
        while (resultSet.next()) {
            result.put(resultSet.getString("TYPE_NAME"), resultSet.getInt("DATA_TYPE"));
        }
    }
    return result;
}

Then I get this map like this然后我像这样得到这个 map

BIGINT -> {Integer@5274} -5 BIGINT -> {Integer@5274} -5

BIGINT UNSIGNED -> {Integer@5274} -5 BIGINT UNSIGNED -> {Integer@5274} -5

And so on.等等。

But there is no JSON.但是没有JSON。

what's more?更重要的是?

If I define like this如果我这样定义

"id_card LONG CHAR VARYING" “id_card LONG CHAR VARYING”

Then how can I get the java.sql.Types?那我怎样才能得到 java.sql.Types?

The code that you are using looks to be the correct way to get the supported column types.您使用的代码看起来是获取支持的列类型的正确方法。

One possible explanation for your not seeing JSON in the output is that your database does not support JSON as a column type.您在 output 中看不到JSON的一个可能解释是您的数据库不支持JSON作为列类型。 For instance, although MySQL 8.x supports the JSON data type, according to the manual MySQL 5.6 does not support it.例如,虽然 MySQL 8.x 支持JSON数据类型,但根据手册MySQL 5.6 不支持它。

It could also be the database driver.它也可能是数据库驱动程序。 For instance, I don't know what would happen vis a vis JSON support if you tried to use a Connector/J 5.1 driver with a MySQL 8.x database.例如,如果您尝试将 Connector/J 5.1 驱动程序与 MySQL 8.x 数据库一起使用,我不知道vis JSON 支持会发生什么。


UPDATE更新

I had a look at the source code MySQL Connector/J 8.x (latest), and I think that the reason that you can't see JSON is... it is a bug in the JDBC driver.我查看了源代码 MySQL Connector/J 8.x(最新),我认为您看不到JSON的原因是……它是 JDBC 驱动程序中的错误。

Looking at the implementation class for DatabaseMetadata , at line 4012 (or thereabouts) we see that the getTypeInfo() has code that assembles a ResultSet for a hard-wired list of types.查看DatabaseMetadata的实现 class,在第 4012 行(或附近)我们看到getTypeInfo()具有为硬连线类型列表组装ResultSet的代码。 The JSON type is not there, and there is a comment that says: JSON类型没有,有评论说:

  // TODO add missed types (aliases)

(FWIW, I couldn't see any logic in the driver to check which data types are actually supported by the database. That is potentially another bug.) (FWIW,我在驱动程序中看不到任何逻辑来检查数据库实际支持哪些数据类型。这可能是另一个错误。)

So, my "possible explanations" (see above) were not the actual explanation in your particular case.因此,我的“可能的解释”(见上文)并不是您特定情况下的实际解释。


Solution?解决方案? I don't think there is a good one in this case.我认为在这种情况下没有一个好的。 But I recommend that you submit a bug report .但我建议您提交错误报告 (I couldn't find an existing bug report for this particular issue, though there were some old bugs about missing types in MySQL Connector/J 5.x) (我找不到针对此特定问题的现有错误报告,尽管在 MySQL Connector/J 5.x 中存在一些关于缺失类型的旧错误)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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