Download View this document as PDF

Transcript
Using Java classes as datatypes
Selecting, inserting, updating, and deleting Java objects
After you specify Java-SQL columns, the values that you assign to those data
items must be Java instances. Such instances are generated initially by calls to
Java constructors using the new operator. You can generate Java instances for
both columns and variables.
Constructor methods are pseudo instance methods. They create instances.
Constructor methods have the same name as the class, and have no declared
datatype. If you do not include a constructor method in your class definition, a
default method is provided by the Java base class object. You can supply more
than one constructor for each class, with different numbers and types of
arguments. When a constructor is invoked, the one with the proper number and
type of arguments is used.
In the following example, Java instances are generated for both columns and
variables:
declare @A Address, @AA Address, @A2 Address2Line,
@AA2 Address2Line
select @A = new Address( )
select @AA = new Address('123 Main Street', '99123')
select @A2 = new Address2Line( )
select @AA2 = new Address2Line('987 Front Street',
'Unit 2', '99543')
insert into emps values('John Doe', new Address( ),
new Address2Line( ))
insert into emps values('Bob Smith',
new Address('432 ElmStreet', ‘99654’),
new Address2Line('PO Box 99', 'attn: Bob Smith', '99678') )
Values assigned to Java-SQL columns and variables can then be assigned to
other Java-SQL columns and variables. For example:
declare @A Address, @AA Address, @A2 Address2Line,
@AA2 Address2Line
select @A = home_addr, @A2 = mailing_addr from emps
where name = 'John Doe'
insert into emps values ('George Baker', @A, @A2)
select @AA2 = @A2
update emps
set home_addr = new Address('456 Shoreline Drive', '99321'),
mailing_addr = @AA2
where name = 'Bob Smith'
40
Adaptive Server Enterprise