|
SYS-CON.TV Webcasts
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Top Links You Must Click On
Product Reviews PL/SQL
PL/SQL
By: Vivek Sharma
Aug. 1, 1999 12:00 AM
As the Web grows, a great deal of effort is being made toward writing applications in Java that interact with databases. Fortunately, JDBC provides an easy, database-vendor-independent way of writing such applications. While this approach works for a number of applications, there are limitations. For instance, if the application requires execution of a large number of SQL statements, efficiency becomes an issue since round-trips from the application to the database are costly. Also, since these statements aren't precompiled, the database system spends more time executing them than it would have if they were compiled and residing in the database. This is where PL/SQL comes in. It's a database language that enhances SQL by providing structure to SQL programs. It's rich with elements like control structures that we're used to seeing in such languages as C and Java. Thus you can write arbitrarily complex routines that manipulate the database using PL/SQL. More important, you can compile PL/SQL programs in an Oracle database so they execute faster at runtime.
Oracle Application Server
Since describing the OAS in detail isn't feasible here, I'll cover the basic items you'll need to understand the rest of the material. Broadly speaking, the OAS consists of a Web Request Broker (WRB) that handles Web requests that require execution of an application. The WRB routes these requests to the appropriate executable code. It provides load balancing and other services, such as transaction management, to the applications executing in the OAS environment.
Cartridges
The OAS comes with a number of cartridges, one of which is the JWeb cartridge. This cartridge is meant for executing Java applications. It maintains an instance of a Java Virtual Machine and executes the Java class specified in the request. The OAS also comes with a PL/SQL cartridge, which can be used for executing PL/SQL applications directly. However, the focus of our current discussion is on integrating Java applications with database applications, so we won't discuss the PL/SQL cartridge here.
Virtual Paths
A virtual path also has a physical directory associated with it - this is where the executable code you've written needs to reside. The following example should make this clear. Let's say you've declared a virtual path called "myjava" in the OAS that maps to the JWeb cartridge, and specified that the associated directory is /home/me/java_dir. If somebody sitting on a client such as a browser requests the URL http://yourmachine/myjava/Employees, the OAS will recognize myjava as a virtual path. It'll also determine that this maps to a JWeb cartridge. Accordingly, it'll direct an available instance of the JWeb cartridge to execute Employees.class. This is a class file created by you that resides in the directory /home/me/java_dir on the server where your OAS is running.
pl2java Utility
With this background about the OAS, we can move on to see how a program running in the JWeb cartridge can execute PL/SQL procedures and functions. To illustrate a typical usage, we'll develop a small application - one that allows people to register online on your Web site.
Developing a Sample Application
Internally, this application is written in Java where variables from your form are extracted and error checking is done. A PL/SQL procedure that writes the information to the database and returns a success or failure code to the Java application is then invoked. Based on this result, the Java application returns a thank you or error page to the user.
Designing the Database
Obviously, the first and last name go together. So let's create a table called "Person" with two fields, First_Name and Last_Name, both of VARCHAR2 with max size of 50 each. Address can be in a separate table of the same name. Fields can be "Line_1", "Line_2", "City", "State", "Country" and "Zip". How about e-mail and phone? They're essentially telecom addresses. Keeping them in the same table will have the added advantage that if we want to expand the registration application later to include fax, mobile phone, etc., we won't have to write an additional table to store this new information. But we need to distinguish one type from another. So we'll have a "Type" field in this table that can take on values like "E-MAIL", "FAX" and "PHONE". This table would be "Telecom_Address" and would have the following fields: "Code" and "Type", both of which are VARCHARs. A quick recap - we have three tables:
Person - First_Name, Last_Name We also need to establish a relationship between these tables so we can link the records. In the Person table we add a field called "Id". This will have a unique value for each person who uses the registration system. In the Address and Telecom_Address tables we add a field called "Person_Id" that points to a record in the Person table. To illustrate how it'll work, assume that five people have registered. For the first user our application stores "1" in the ID field of record created for this user in the Person table, "2" for the second one and so on. It also stores the same number in the Person_Id field of records created in the Address and Telecom_Address table. To find the name, address and e-mail of the second person, we can issue the following SQL statement:
SELECT Person.First_Name, Person.Last_Name,
PL/SQL for Manipulating the Database
CREATE SEQUENCE Person_Seq START WITH 1; As far as packages are concerned, we'll create four packages - one for handling insertion into each of the three tables, and a top-level package that calls routines in the other packages. We do this so only one API will be consistently exposed to the Java application that needs to call these routines. I won't go into much detail about packages and PL/SQL in general - suffice it to say that a package consists of a declaration followed by an implementation, also known as the package body. A sample package for adding stuff to the Person table would look like:
CREATE PACKAGE Person_Pkg AS Packages for manipulating the other two tables would look similar. Our top-level package is Reg_Api. The body of this package contains the procedure add(), which looks like Listing 1. The next step is to compile these packages by using something like SQL*Plus. Once these packages are compiled, we can use the pl2java utility to generate a class file.
Invoking pl2java
pl2java myschema/myschema@myConnect Reg_Api This will generate a class called Reg_Api.class that can be used in our Java application. Note that pl2java accepts some command-line parameters that enable you to do things like changing the name of the output class file.
HTML Page for the Application
<FORM ACTION='http://yourserver/myjava/Register'> This HTML file can be put in the document root directory of the machine hosting the OAS. We now need to create a Java application - specifically, a class called Register. After we've created it, we need to move it to the directory that corresponds to the virtual path "myjava".
The Java Application
Listing 2 shows what our Java program looks like.
Conclusion
(The opinions and statements expressed in this article are my own and don't necessarily represent those of Oracle Corporation.) Reader Feedback: Page 1 of 1
Enterprise Open Source Magazine Latest Stories . . .
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||