Java data access

Page 136

Chapter 7: Understanding JDBC Data Types the SQL REF object and materialize it within your application using the ResultSet.getRef() method. Note You need to fully understand how SQL REF data types work within your DBMS before implementing them. Complete implementation details, however, are beyond the scope of this book. JAVA_OBJECT The JDBC JAVA_OBJECT type enables you to store Java classes directly in a database. Sun has a vision for Java — relational database systems that can store Java objects natively. Database vendors are not required to provide support for this data type. The Java database is part of a new breed of database called Object Database Management Systems (ODBMS). These databases are intended to work directly with object−oriented programming languages by combining the elements of object orientation and object−oriented programming within a database system. The advantage of object−relational databases is that they conceptually eliminate the need to translate between Java data types and SQL data types. You simply store and retrieve your objects from the ODBMS as you need them. SQL3 user−defined types SQL3 UDTs enable database developers to create their own type definitions within the database. The types are defined with SQL statements and have nothing to do with Java. However, JDBC provides data types that enable you to access database UDTs from your Java application. Unlike the predefined data types, these types are materialized on the client as your application accesses them. You work with these values directly and do not use anSQL LOCATOR that references the values in the databases. Be aware that some UDTs may store large amounts of data. This section provides an overview of the different UDTs and how to use them in your programs. DISTINCT A DISTINCT data type enables you to assign a custom name to a new data type based on another data type. This is analogous to extending a Java class in that the new class, or type, is based on an existing type. These data types enable you to give data type a name that makes sense to you and other developers. It also ensures that the data type will always have certain attributes, such as character length for character data or precision for numeric data types. Here are two examples of the SQL syntax used to create DISTINCT data types: CREATE TYPE Salary NUMERIC(9,2) CREATE TYPE EmployeeName CHAR(20)

In the first example, a data type Salary is defined as a NUMERIC type with a precision (total number of digits) of nine and a scale of (number to the right of the decimal point) of two. In the second example, a data type called EmployeeName is defined as a CHAR type that is always 20 characters long. If you store the name Paige in the type EmployeeName, letters occupy five of the 20 characters and the remaining characters are empty. As with the other data types, you can use the setXXX() and getXXX() methods from the ResultSet object to retrieve the data stored in these variables. Refer to Table 7−3, which provides the default data type mappings to use. 124


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.