[top]
[prev]
[next]

Road Map
Glossary
Table of Contents
Index
Documentation Top
Global Index
|
Accessing Classes and Class Aliases
All the examples shown so far use methods associated with objects stored in the database.
You can invoke public methods and access public fields of any Java class that is visible to your JVM, provided that it is visible to the JVM in which Cloudscape is running. Instances of those classes do not have to be stored in the database.
For example, you can use a VALUES expression to construct a value, then execute a method on that value.
A class is visible when:
- It is public.
- It is installed on the local computer.
- It is in a library in the current class path.
When specifying a class name rather than referencing an instance of the class, you have to specify the full package name for the class unless you create a class alias for it. (Classes provided by Cloudscape come with built-in class aliases).
You will learn more about class aliases later on.
Execute a Method in a VALUES Expression Using an Instance
Each built-in SQL-J type has a corresponding Java type. An SQL-J INT's corresponding Java type is java.lang.Integer; an SQL-J CHAR's corresponding Java type is java.lang.String. You can use a VALUES expression to construct an INT or CHAR value, then execute a method off its corresponding Java type.
For example:
- 1+3
Evaluates to an SQL-J INT, and is thus an instance of java.lang.Integer
- 'Santiago'
Evaluates to an SQL-J CHAR, and is thus an instance of java.lang.String.
- Execute the following VALUES expressions in ij:
VALUES((1+3).toString());
VALUES('Santiago'.toUpperCase());
toString() is a method in java.lang.Integer. toUpperCase is a method in java.lang.String.
- You can also invoke static methods associated with an instance of a Java class.
For example, invoke the static method toString(Integer) of the java.lang.Integer class on the expression 1+3:
VALUES((1+3).toString(35));
The value returned (the string "35") doesn't have anything to do with the expression 1+3. Using an expression simply allowed you to create an instance of the class java.lang.Integer from which to execute the static method.
Execute a Method Using a Class, Not an Instance
- Finally, invoke a static method not associated with an instance of a Java class.
In a Java program, you invoke a static method by specifying the class name.
In an SQL-J statement, you may have to use the keyword CLASS before the class name to invoke a static method. You must also specify the full package and class name. (The names of java classes and packages are case-sensitive). Use the VALUES expression to call such methods when you want to retrieve its value.
Execute the following statement:
VALUES ((CLASS java.lang.Integer).toString(35));
Quiz: What data type is 35 in the above method call?
|
Answer: This is a trick question. As you know, the literal 35 evaluates to a java.lang.Integer. However, there is no method with the signature toString(java.lang.Integer) in the java.lang.Integer class. If Cloudscape cannot find a method with a matching signature, it tries again, assuming that the literal maps to the corresponding primitive data type (in this case, an int). There is a method of the signature toString(int) in the java.lang.Integer class, and the call executes successfully.
|
Work with Class Aliases
You may have noticed that in the last query you invoked, you had to type the full package name for the class java.lang.Integer in addition to using the keyword CLASS. Cloudscape allows you to avoid both of those requirements if you define an alias for a class within your database.
Typically, the alias for a class name is just the class name (minus all the package names). This is similar to how the import statement works within a Java program. Class aliases are case-insensitive, however.
- Create a class alias for java.lang.Integer.
For this class, we can't use the default alias, Integer, because Integer is a reserved word. (Cloudscape already uses it for a built-in data type). We'll have to specify an alternate alias name. Let's use the same word, but delimit it with double quotes.
- Execute the following SQL-J statement:
CREATE CLASS ALIAS "Integer" FOR java.lang.Integer;
You can now reference this class with the word "Integer" alone.
- Try the query from the last task in its new, shortened form:
VALUES "Integer".toString(35);
Execute Some More Methods Using Class Aliases
- You can execute some methods off of the class JBMSTours.serializabletypes.City. Luckily, the class already has an alias for it, City. Execute the following SQL-J statement:
VALUES City.findCity(GETCURRENTCONNECTION(), 1);
In this case, the parameter to the VALUES expression actually does get data out of a table, so you would not expect it to be in a VALUES expression. However, the SQL-J statement that accesses the data is being called by the database-side JDBC method and is "hidden" from the outer statement.
Quiz: What statement would you have to use if the class did not have a class alias?
|
Answer:
VALUES ((CLASS JBMSTours.serializabletypes.City).findCity( GETCURRENTCONNECTION(), 1));
|
- Execute an instance method off that last VALUES expression:
VALUES City.findCity(GETCURRENTCONNECTION(), 1).getName();
NOTE: To learn more about invoking static methods, which are the SQL-J constructs most like standard database stored procedures, see Static Method Aliases.
Use a Constructor
Cloudscape supports constructor methods. That means you can construct new instances of classes within an SQL-J statement, where applicable. If an alias is defined for the class, you can use the alias. Otherwise, specify the full package and class name.
- Execute the following in ij:
VALUES (new java.lang.Integer('1'));
- Here's another one:
VALUES new Location(52, 21, 'N', 4, 55, 'E');
(ij truncates the display.)
- Here's another:
VALUES (new Person('Your', 'Name'));
- Which leads to:
INSERT INTO people (person) VALUES (new Person( 'Your', 'Name'));
- Don't make this change in the database permanent, however; issue the ij rollback command:
rollback;
|