如何将数据类型从mysql设置为java [英] how to get Set data type from mysql to java

查看:132
本文介绍了如何将数据类型从mysql设置为java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我的问题。我创建了一个表,其中包含mysql DB中的SET数据类型列。
我想得到这个(SET)列的值。

So here is my question. I create a table which contains a SET data type column in mysql DB. I want to get the values of the of this (SET) column.

我已经完成了所有的连接配置,一切都在我的代码上运行良好。

I have done all the connection configurations and everything is working well on my code.

如何在Set java对象中使用resultSet获取Set dataType ????

How to get the Set dataType with resultSet in Set java object????

我试过了这是。

Java bean代码

Java bean code

public class Valeur {
private Long id;
private Set categoriesValues = new HashSet();
\\getters and setters for the id and the categoriesValues
}

ReultSet代码

ReultSet Code

 private static Valeur map(ResultSet resultSet) throws SQLException {

        Valeur valeur = new Valeur();
        valeur.setId(resultSet.getLong("id"));
    valeur.setCategoriesValues(resultSet.getString("categoriesValues"));

        return valeur;
    }

ResultSet适用于id但不适用于Set类型。

ResultSet is working for the id but not for the Set type.

谢谢

推荐答案

根据 https://dev.mysql.com/doc/connector-j/en/connector -j-reference-type-conversions.html mysql set列映射到Java字符串。

According to https://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html mysql set columns are mapped to Java strings.

似乎您需要自己拆分返回值以转向它是一个Java集。

Seems that you need to split the returned value yourself to turn it into a Java set.

在你的例子中(未经测试):

In your example (untested):

String values = resultSet.getString("categoriesValues");
HashSet<String> valuesSet = new HashSet<>();
Collections.addAll(valuesSet , values.split(","));
valuer.setCategoriesValues(valuesSet );

这篇关于如何将数据类型从mysql设置为java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆