The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
DISTINCT
数据类型Note: MySQL and Java DB currently do not support the 注意:MySQL和Java DB目前不支持DISTINCT
SQL data type. DISTINCT
SQL数据类型。Consequently, no JDBC tutorial example is available to demonstrate the features described in this section.因此,没有JDBC教程示例可用于演示本节中描述的功能。
The DISTINCT
data type behaves differently from the other advanced SQL data types. DISTINCT
数据类型的行为不同于其他高级SQL数据类型。Being a user-defined type that is based on one of the already existing built-in types, it has no interface as its mapping in the Java programming language. 作为一种基于现有内置类型之一的用户定义类型,它在Java编程语言中没有接口作为其映射。Instead, the standard mapping for a 相反,DISTINCT
data type is the Java type to which its underlying SQL data type maps.DISTINCT
数据类型的标准映射是其底层SQL数据类型映射到的Java类型。
To illustrate, create a 举例来说,创建一个DISTINCT
data type and then see how to retrieve, set, or update it. DISTINCT
数据类型,然后查看如何检索、设置或更新它。Suppose you always use a two-letter abbreviation for a state and want to create a 假设您总是使用两个字母的缩写来表示一个州,并希望创建一个用于这些缩写的DISTINCT
data type to be used for these abbreviations. DISTINCT
数据类型。You could define your new 您可以使用以下SQL语句定义新的DISTINCT
data type with the following SQL statement:DISTINCT
数据类型:
CREATE TYPE STATE AS CHAR(2);
Some databases use an alternate syntax for creating a 某些数据库使用另一种语法来创建DISTINCT
data type, which is shown in the following line of code:DISTINCT
的数据类型,如以下代码行所示:
CREATE DISTINCT TYPE STATE AS CHAR(2);
If one syntax does not work, you can try the other. 如果一种语法不起作用,可以尝试另一种语法。Alternatively, you can check the documentation for your driver to see the exact syntax it expects.或者,您可以查看驱动程序的文档,以查看它所需的确切语法。
These statements create a new data type, 这些语句创建一个新的数据类型STATE
, which can be used as a column value or as the value for an attribute of a SQL structured type. STATE
,它可以用作列值或SQL结构化类型的属性值。Because a value of type 因为STATE
is in reality a value that is two CHAR
types, you use the same method to retrieve it that you would use to retrieve a CHAR
value, that is, getString
. STATE
类型的值实际上是两种CHAR
类型的值,所以可以使用与检索CHAR
值相同的方法来检索它,即getString
。For example, assuming that the fourth column of 例如,假设ResultSet rs
stores values of type STATE
, the following line of code retrieves its value:ResultSet rs
的第四列存储STATE
类型的值,下面的代码行检索其值:
String state = rs.getString(4);
Similarly, you would use the method 类似地,您将使用方法setString
to store a STATE
value in the database and the method updateString
to modify its value.setString
在数据库中存储STATE
值,并使用方法updateString
修改其值。