Documentation

The Java™ Tutorials
Hide TOC
Using DISTINCT Data Type使用DISTINCT数据类型
Trail: JDBC Database Access
Lesson: JDBC Basics

Using DISTINCT Data Type使用DISTINCT数据类型

Note: MySQL and Java DB currently do not support the DISTINCT SQL data type. 注意:MySQL和Java DB目前不支持DISTINCTSQL数据类型。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 DISTINCT data type with the following SQL statement:您可以使用以下SQL语句定义新的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值相同的方法来检索它,即getStringFor 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修改其值。


Previous page: Using Array Objects
Next page: Using Structured Objects