Reputation: 320
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.
How to get the Set dataType with resultSet in Set java object????
I've 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 is working for the id but not for the Set type.
Thanks
Upvotes: 4
Views: 2362
Reputation: 32980
According to https://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html mysql set columns are mapped to Java strings.
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 );
Upvotes: 4
Reputation: 27986
Since it's not represented in java.sql.Types I'm guessing it's a little used, custom type that's not supported in many RDBMS'
That being said, to retrieve the value it's likely either:
ResultSet.getArray(...)
Or
ResultSet.getObject(...)
To get the object. I'm guessing you'll have to pack it into a java.util.Set
manually (or maybe getObject() returns a java.util.Set
?)
Upvotes: 0