How to get data type data from mysql in java

So here is my question. I am creating a table that contains a SET data type column in mysql DB. I want to get the values โ€‹โ€‹of this column (SET).

I did all the connection configurations and everything works fine on my code.

How to get Set dataType using resultSet in Set java object ????

I have tried this.

Java bean code

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

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 works for identifier, but not for type Set.

thanks

+4
source share
2 answers

According to https://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html mysql column values โ€‹โ€‹are mapped to Java strings.

, , Java.

():

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

java.sql.Types , , , RDBMS '

, , , :

ResultSet.getArray(...)

ResultSet.getObject(...)

. , java.util.Set (, , getObject() a java.util.Set?)

0

Source: https://habr.com/ru/post/1617277/


All Articles