什么是 SQL 集合的 Java 类?

huangapple go评论75阅读模式
英文:

What Java Class are SQL Sets?

问题

当我在Java中执行SQL查询时,我要使用哪个方法从结果集中获取SQL集合?
因此,如果我有一个像这样的表格:

CREATE TABLE mytable (id int, myset set("value0","value1","value2"));

我如何在Java中获取myset列中的值?它只是一个字符串吗,还是一个列表,或者是其他什么东西?

英文:

When I do an SQL query in java what method do I use to get an SQL set from the result set?
So if I have a table like this:<br>

CREATE TABLE mytable (id int, myset set(&quot;value0&quot;,&quot;value1&quot;,&quot;value2&quot;));<br>

How would I get the value in the myset column in Java? Would it just be a string or would it be a list or something else?

答案1

得分: 2

我猜这是MySQL的SET功能?

这不是JDBC的一部分,但JDBC提供了支持非标准类型的JDBC驱动程序的功能。要设置它们:

preparedStatement.setObject(idx/name, value, targetSqlType);

其中targetSqlType是可选的(如果省略,则使用value的类型来尝试确定),是一个int。那么应该有什么值呢?¯\(ツ)/¯ - 希望JDBC驱动程序有一个int常量。value应该是什么?¯\(ツ)/¯ - mysql JDBC驱动程序的文档应该告诉你,因为JDBC规范不会提供这个信息。

要获取它们:

resultSet.getObject(idx/colname, SomeType.class);

在这里,SomeType应该是MySQL JDBC驱动程序决定的。

需要注意的是,MySQL的set list的实现是一个带有单个long(64位)值的位集。可能传递long值,或者可能是BitSet,但很可能MySQL JDBC驱动程序根本不支持这个。

根据MySQL JDBC驱动程序(因某种原因称为Connector/J)的文档,它是用一个字符串来设置和返回的。

这对我来说看起来很奇怪。试一试:创建一个set,设置一些值,然后进行查询。

编辑:另请参阅MySQL SET类型文档,似乎暗示你只需使用.setString(1, "clubs,spades"),然后可以使用.getString(1)获取它们,返回值为"clubs,spades"。所以,在这些东西中没有逗号,性能也极其缺乏,我猜是这样?哎呀。

好消息是,你可以通过JDBC与MySQL的SET类型进行交互。坏消息是,以最愚蠢的方式可能实现。

英文:

I assume this is MySQL's SET thing?

It's not part of JDBC, but JDBC has facilities for JDBC drivers to offer support for non-standard types. To set them:

preparedStatement.setObject(idx/name, value, targetSqlType);

where targetSqlType is optional (if omitted, the type of value is used to try to figure it out), and an int. What value should be there? ¯\(ツ)/¯ - Let's hope the JDBC driver has an int constant. What should value be? ¯\(ツ)/¯ - the docs of the mysql JDBC driver should tell you, because the JDBC spec won't.

To fetch them:

resultSet.getObject(idx/colname, SomeType.class);

Where, again, what SomeType is supposed to be is up to the MySQL JDBC driver.

Note that the implementation of MySQL's 'set list' is as a bitset with a single long (64-bit) value. Possibly then, pass longs, or maybe BitSet, but most likely the MySQL JDBC driver just doesn't support this at all.

According to the MySQL JDBC driver (which is called Connector/J for some reason) documentation it is set with, and returns, a String.

That seems bizarre to me. Give it a try: Make a set, set a few things, and go query it.

EDIT: See also the MySQL SET type docs, which seem to suggest you just.. .setString(1, &quot;clubs,spades&quot;) and that you get em with .getString(1) which would return &quot;clubs,spades&quot;. Sooo, no commas in those things and excruciating lack of performance, I guess? Oof.

The good news is, you can interact with MySQL SET types from JDBC. The bad news is, in the most silly way possible.

huangapple
  • 本文由 发表于 2020年9月27日 07:01:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64083261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定