![]()
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Meet the City ClassIn this section, you will be working with the JBMSTours.serializabletypes.City class. Open the javadoc or the Java source file for this class now. Scroll through the methods to start getting familiar with what these methods do. Javadoc for JBMSTours is in the /demo/programs/tours/javadoc subdirectory in the cloudscape base directory. TASK: Examine and Execute CityTestCityTest.java, which you should have copied into your working directory from the scripts directory, is a simple Java application that does not access the database. Instead, it creates two instances of the City class and executes some of its methods for one of those (it uses the other instances as a parameter to two method calls). One of the methods it uses is getDistanceFrom. The getDistanceFrom method shows off the power of object programming. One of the fields in the City class is a Location object (you can look at the javadoc for Location to learn the details). The Location object defines the location in terms of latitude and longitude. The getDistanceFrom method uses a well known trigonometric formula to calculate the approximate distance between two locations. In this task, you will run CityTest, which executes application logic in the application. In the next task, you will execute some of those same methods in SQL-J statements instead of from an applicationexecuting application logic in Cloudscape. As you will see, there is not much difference.
|
Notes About Starting ijThe name of the program ij is COM.cloudscape.tools.ij. When you start ij, you will specify an ij property called ij.protocol. The ij.protocol consists of the protocol and subprotocol portions of the database connection URL you will be using. For example, the ij.protocol you have used throughout this tutorial is jdbc:cloudscape:. Users working in client/server mode use jdbc:cloudscape:weblogic:. When you specify ij.protocol, ij automatically boots the appropriate JDBC driver. So if you specify jdbc:cloudscape:, ij automatically boots the embedded JDBC driver for you. In addition, after you specify this property, ij allows you to provide a short form of a database connection URL to connect to a database. That is, instead of having to specify jdbc:cloudscape:HelloWorldDB to connect to HelloWorldDB (as you did in a Java application in Chapter 2, Cloudscape Basics and the Sample Database), you need only specify HelloWorldDB. You will use the short form of the database connection URL as an argument to the ij command Connect. You specify the ij.protocol property on the command line when starting up ij, like this: NOTE: The good news is that Cloudscape provides a batch file for you to make this easier. If you are working on a Windows or UNIX platform and you have the /bin subdirectory of your cloudscape base directory in your PATH, you can accomplish the same thing by merely typing ij. For example: The rest of this book assumes you dont have this shortcut and shows you the long way, but use this shortcut if you have it. |
NOTE: ij commands are case-insensitive. ij command statements end with a semicolon.
java -Dcloudscape.system.home=your_tutorial_system
-Dij.protocol=jdbc:cloudscape: COM.cloudscape.tools.ij
You should have an ij> prompt ready for you to enter SQL-J statements or ij commands.
The string you pass to the Connect command is the short form of embedded database connection URL.
The rest of the examples in this chapter do not include the ij> prompt, so that it is easier to cut and paste queries into the command window.
Now that you are connected to the toursDB database, you can access the City table.
One of the methods that CityTest executed was the City classs getName() method. CityTest executed that method on two City objects residing in memory. You can have Cloudscape execute that method on one, some, or all of the City objects stored in the table.
SELECT city.getName() FROM CITIES;
city is the column name. That column happens to store instances of -JBMSTours.serializabletypes.City (the column could have been called something different).
SELECT city.getName() FROM CITIES
WHERE city_id > 50;
SELECT city_id FROM CITIES
WHERE city.getName() = 'Santiago';
Executing a method in a WHERE clause allows you to refine your search conditions.
To access the field of a stored object in an SQL-J statement, you add the field name to the column name. The two-character combination -> separates the field name from the column name.
For you to access a field, the field must be public.
The language field in the City class is public.
SELECT city->language FROM Cities;
Question: Why cant you access the airport field? |
Answer: Because it is a private field. |
CityTest executed two methods that took City objects as parameters. For example, the getDistanceFrom(City) method took a City object as a parameter. How can you execute those methods within an SQL-J statement?
Well, its possible but a little complicated.
One way to do it is to craft an SQL-J statement that returns a City object, and then to use that statement as the parameter to the method. It must return only one City object, not more. Since the city_id is a unique column, we specify a single city_id in the WHERE clause.
SELECT City FROM Cities WHERE city_id = 35;
That query should return the following:CITY
---------------
Paris, France
Lets find how far each City in the database is from Paris. We can do that by passing in the above SQL-J statement as a parameter to the getDistanceFrom(City) method call. If the main SQL-J statement has no WHERE clause, Cloudscape evaluates the main expression for every row in the database.
SELECT city.getName(), city.getDistanceFrom(
(SELECT city
FROM Cities
WHERE city_id = 35))
AS MILES_FROM_PARIS
FROM Cities;
SELECT city.getName(), city.getDistanceFrom(
(SELECT city FROM Cities WHERE city_id = 35))
AS MILES_FROM_PARIS
FROM Cities
ORDER BY MILES_FROM_PARIS;
A DBMSs ability to filter and sort records is one of the reasons to use a database instead of just storing data yourself.
CityTest executed the getDistanceFrom method only for Santiago.
You may recall that the output of CityTest was:C:\tutorial_home>java CityTest
constructing city object for Santiago
constructing city object for Paris
The airport for Santiago is SCL
The distance between Santiago and Paris is 7244 miles
The current average temperature in Santiago is 66.5
The time difference between Santiago and Paris right now
is -6.0 hours
SELECT city.getDistanceFrom(
(SELECT city FROM Cities WHERE city_id = 35))
FROM Cities
WHERE city_id = 40;
The ij exit command automatically shuts down the system properly in an embedded environment.
This section and the last section showed how you can execute the same methods that you execute in a Java program in the database. When executed within an SQL-J statement, methods are called database-side methods.