Archive for December, 2008
Use Ext, Aptana and AIR
Back in July 2008 I wrote a tutorial titled “Use Ext, Aptana and AIR to build desktop applications” that has been published on IBM’s developerWorks website. The tutorial explores the concepts of the Aptana IDE (Integrated Development Environment), the Ext JavaScript library and the Adobe AIR platform, before walking through the process of setting up a development environment for building applications using these technologies. Once the environment is set up, a simple hello world application is created. The tutorial then concludes with the development of a simple contact management utility using Ext and deployed to the desktop using Adobe AIR.
The following is the blurb for the tutorial as found on the developerWorks website:
While Rich Internet Applications and Web 2.0 have been all the rage, the latest wave of emerging technologies are focusing on not only giving Web applications a desktop-like feel but actually bringing them to the desktop. The Adobe Integrated Runtime (AIR) has led the way, allowing Web application developers to leverage their existing knowledge of HTML, CSS, JavaScript, Flash, and Flex to build powerful desktop applications. In this tutorial, you will use the open source Aptana Studio IDE, the Adobe AIR plug-in for Aptana, and the open-source JavaScript framework Ext.
You can view the tutorial in full at http://www.ibm.com/developerworks/edu/os-dw-os-ext-air.html
Install Apache and mod_owa
In order to develop PL/SQL applications on Oracle XE, we are going to need to install a web server (Apache) and a PL/SQL gateway for Apache called mod_owa. On a regular Oracle Application Server you would probably be using Oracle HTTP Server (a modified Apache) and mod_plsql, amd you should also be able to follow my future tutorials if you have this type of setup.
Once again I will be assuming throughout this guide that you have followed my Oracle XE installation tutorial. If you didn’t, you may need to change usernames, passwords and service names to fit your setup. I will be walking through the setup process on a Windows XP machine, but it should be similar on another Windows version. Let’s get started!
IMPORTANT NOTE: If you are looking for the download for mod_owa, please note that the website for it has moved since this blog post was originally written. You can now find mod_owa on the Oracle OSS website at the following URL: http://oss.oracle.com/projects/mod_owa/dist/documentation/modowa.htm. I have updated this blog post to reflect this change.
The first thing you’ll need to do is download the relevant software. The easiest way to install Apache is to download and run the installer binary from the Apache website. At the time of writing, the latest stable version of Apache available is 2.2.9, but again the process should be similar for whatever version you are installing. You can download Apache from http://httpd.apache.org/download.cgi. You will also need to grab the Apache PL/SQL Gateway Module (mod_owa) from http://oss.oracle.com/projects/mod_owa/dist/documentation/modowa.htm. Click on the link for “Zip file for Windows”, which includes the source code and binaries for the Windows version of Apache.
The first stage in setting up our PL/SQL web development environment is the installation of the Apache web server. To start the installation, run the installer package you downloaded earlier (the filename should be something like apache_2.2.9-win32-x86-no_ssl-r2.msi). The screen should look similar to the one in Figure 3a below:
Click “Next >” to continue. On the screen that follows, read the license agreement and if you are happy with the terms, select “I accept the terms in the license agreement” and click “Next >” to move on to the readme screen, from which you can simply press “Next >” again. You should now be presented with the following screen:
Unless you are installing Apache for production use, it doesn’t matter too much what you enter into Network Domain, Server Name or Administrator’s email, but be sure to select the option to Install Apache “for All Users, on Port 80, as a Service”. Installing on Port 8080 would likely cause a conflict between Apache and the Application Express software that comes with Oracle XE, which runs on port 8080. As soon as you are ready, click “Next >” to continue.
On the “Setup Type” screen, you can safely leave the default option of “Typical” selected and click “Next >” to continue with the installation. Advanced users may wish to tweak their installation using the Custom option, but for the sake of this tutorial, there is no need to do so.
The Apache installer will now ask you where to install Apache on your hard disk. By default Apache is installed into a subfolder of the Program Files folder, but I find it easier to install Apache into a folder just beneath the root of the C drive, which I usually name “httpd”. You can safely accept the default destination, but from here on I will refer to all Apache configuration files as if you chose to install to C:\httpd\. To install to this directory, click on the “Change” button as in Figure 3d, which should popup a screen like the one in Figure 3e. In the textbox for Folder Name, enter C:\httpd\ and click “OK”. This will close the popup window and return you to the screen shown in Figure 3d. You can now click on “Next >”.
You can now safely click on the “Install” button, which will start the installation process. This should not take long, especially if you are using a modern PC. During the installation you should see a screen similar to that in Figure 3f below.
When the installation has completed, simply click the “Finish” button to exit the installer. You should now have a new icon in your system tray, like the one highlighted in Figure 3g. It should have a tiny green play symbol in it. If it has a red square stop symbol instead, left-click on the icon, and from the Apache2.2 menu, click “Start” to start the Apache service.
Before we move on to installing mod_owa, we will first check that Apache is up and running and functioning correctly. To do so, open your favourite web browser (I use Firefox), enter http://localhost/ in the address bar and press enter. If Apache is working, you will see a message like in Figure 3h:
Now that we have Apache up and running, it is time to install our PL/SQL gateway, mod_owa. The first thing we need to do is to unzip the archive we downloaded earlier, usually named windows_all.zip. You don’t need to worry about where you extract the files to, as we are only really interested in one of them. When the archive has extracted, open the folder you extracted them, and go into the modowa folder. From here, double click on apache22 and right-click on mod_owa.dll and click Copy.
We will now paste this into the modules folder under the Apache installation directory. Hold down the Windows key and press R to open the Run dialog and enter C:\httpd\modules\ as shown in Figure 3i:
This will open the modules folder. Go to the Edit menu and click on Paste to put a copy of the mod_owa.dll file here. Now that we have stored the module DLL file, we now need to tell Apache to load this module into the web server and set up a new Apache Location, which will act like an Oracle Document Access Descriptor (DAD) from where we can run our PL/SQL web applications. If none of this makes any sense to you at this point, don’t worry about it, just follow the instructions closely and you’ll be fine.
To open the Apache configuration file (httpd.conf) open the Run dialog once again (Windows+R or Start->Run). This time, enter notepad c:\httpd\conf\httpd.conf and click the OK button. Notepad should launch, with the Apache config file opened and ready for editing. Scroll down to the bottom of this file (or hold down the Ctrl button and press the End button) and below all other code, insert the text from listing 3a:
LoadModule owa_module modules/mod_owa.dll
<Location /somando>
AllowOverride None
Options None
SetHandler owa_handler
OwaUserid somando/somando1
OwaNLS WE8ISO8859P1
OwaDiag COMMAND ARGS CGIENV POOL SQL MEMORY
OwaLog "/usr/local/apache/logs/mod_owa.log"
OwaPool 20
OwaStart "doc_pkg.homepage"
OwaDocProc "doc_pkg.readfile"
OwaDocPath docs
OwaUploadMax 10M
OwaCharset "iso-8859-1"
order deny,allow
allow from all
</Location>
/
Listing 3a: mod_owa Options for Apache’s httpd.conf
When you have added the text above to your httpd.conf file, save the file, and on the Apache icon in your system tray, left-click and from the Apache2.2 menu choose the “Restart” option to restart the Apache service. If all has gone well, Apache will restart just fine and you will have a play icon in your system tray icon once again. If you did not follow my guide to install Oracle XE, you will need to change the OwaUserid parameter in the text above to your own database’s connect identifier.
Once Apache has been restarted, mod_owa should now be running from http://localhost/somando. To test this, enter that address into your favourite web browser, and if mod_owa was installed you should see an error message like the one displayed in Figure 3j. If you see a different error message, you may not have installed mod_owa correctly.
The final thing we are going to do to make sure our Apache+PL/SQL setup is working correctly is create a sample application which performs a database SELECT and displays the output in a HTML table. Before we create the PL/SQL procedure to do this, let’s run the query from the SQL*Plus command line to see what data we should expect to appear in our sample app. To open SQL*Plus, go to Start -> Programs -> Oracle Database 10g Express Edition -> Run SQL Command Line. When the command line appears log on by typing CONNECT and pressing Enter. When prompted, enter your database username and password (Username: somando Password: somando1 if you followed our Oracle XE installation guide) and as soon as you are connected, enter the SQL statement from Listing 3b to query the database.
SELECT INITCAP(LOWER(object_type)) type, COUNT(*) count FROM all_objects GROUP BY object_type;
Listing 3b: Query all_objects table by object_type
You should see a result similar to that illustrated in Figure 3j:
Now we are going to create a PL/SQL procedure that will output this data to a HTML table that we can access using our web browser. From the SQL Command Line, enter the command ed test_page. Notepad will open and say that it cannot find the test_page.sql file, asking if you would like to create a new file. Click on the “Yes” button, and paste the text from Listing 3c into the Notepad window:
CREATE OR REPLACE PROCEDURE test_page IS CURSOR get_data IS SELECT INITCAP(LOWER(object_type)) type, COUNT(*) count FROM all_objects GROUP BY object_type; BEGIN htp.p('<table border="1">'); htp.p('<tr><th>Type:</th><th>Count:</th></tr>'); FOR i IN get_data LOOP htp.p('<tr><td>'||i.type||'</td><td>'||i.count||'</td></tr>'); END LOOP; htp.p('</table>'); END test_page; /
Listing 3c: Create test_page PL/SQL Procedure
When you have pasted the above code into Notepad, click on File -> Save and then quit Notepad. Back in the SQL Command Line, you should have an SQL prompt. Here, enter the command @test_page to run the script we just created. If all goes according to plan, you should see a message “Procedure created”. We can now test this procedure from our web browser by navigating to http://localhost/somando/test_page which should display something like what you see in Figure 3k below:
That’s it! You are now ready to start developing PL/SQL web applications. In our next tutorial I will look at installing Oracle SQL Developer and how to set up a development environment for PL/SQL web application programming. Very shortly I will be writing tutorials on creating some neat web applications in PL/SQL.
Click here to view a printer friendly version of this tutorial. To download this tutorial to disk, right click here and choose “Save Target As” or “Save Link As”. You will need Adobe Reader to open this file.
Oracle Database Basics
In this tutorial, I will be looking at the basics of Oracle databases, namely, the creation of database tables and inserting of data rows. I will be showing you two ways of interacting with the database: 1.) via Oracle’s Application Express Web-Based Interface; and 2.)Via Oracle’s Command-Line Application, SQL*Plus
For the purpose of this tutorial, I will assume that you have followed my previous tutorial, which guides you through the process of installing Oracle Database 10g Express Edition (Oracle XE) on a Windows XP computer. The schemas, usernames and passwords I refer to will only work if you have set your system up as described in that lesson.
First, I am going to log into Oracle Application Express, which will allow me to manage the database via a neat web-based interface. From your Start Menu, go to Programs -> Oracle Database 10g Express Edition -> Go To Database Home Page. This will take you to a screen that should look similar to figure 2a above. On this screen enter the username somando and the password somando1. These login details will only work if you followed my previous tutorial and set up the user as described. If you used a different username and password when you created the user, be sure to use those credentials instead or you will not be able to access Application Express.
Once you have successfully logged in, you should see a screen similar to that in Figure 2b above. I am now going to create a database table. To do this, click on the arrow to the right of the Object Browser image icon. From the Create submenu, choose Table.
This screen will allow you to define a table, with fields for the table’s name, and for 8 columns. To add more than 10 columns, you can click the Add Column button. I am going to create a table called category with three columns, id, name, and parent_id. Don’t worry about the purpose of these fields for now, I’ll explain it in more detail at a later stage. Fill out the form exactly as you see in Figure 2c, and click the Next > button.
Next you will need to set up the primary key for the table. I am going to set up the id field as our primary key, populated automatically from a sequence. To do this, select Populate from a new sequence from the list of options for Primary Key, and accept the default values for the Primary Key Constraint Name and Sequence Name fields. Now, select ID(NUMBER) from the drop-down list for the Primary Key field and press Next >.
I am not going to set up any foreign keys at this time, so on the Foreign Keys screen (see Figure 2e), you can simply click the Next > button and move on. The next screen allows you to set up unique keys and check constraints. Again, I will not be using either of these right now, so you can simply press the Finish button.
Finally, you will be asked to confirm your request to create the category table on the somando schema. If you press the SQL link, you will be shown the SQL statements that will be used to create the table. The code should be as follows:
CREATE table "CATEGORY" ( "ID" NUMBER(11,0) NOT NULL, "NAME" VARCHAR2(255) NOT NULL, "PARENT_ID" NUMBER(11,0) NOT NULL, constraint "CATEGORY_PK" primary key ("ID") ) / CREATE sequence "CATEGORY_SEQ" / CREATE trigger "BI_CATEGORY" before insert on "CATEGORY" for each row begin select "CATEGORY_SEQ".nextval into :NEW.ID from dual; end; /
Listing 2a: SQL Code to create “category” table
If all looks in order, go ahead and click the Create button, which will issue the above SQL code to the database, which will in turn create the table for you. If the create operation has completed successfully, you should see a screen similar to that displayed in Figure 2g below:
Congratulations, you have created your first Oracle database table! That wasn’t so difficult now, was it? I will now take a break from using Application Express and use the SQL*Plus client that comes with Oracle Database 10g Express Edition to interact with the new table I have just created. To start SQL*Plus, go to Start -> Programs -> Oracle Database 10g Express Edition -> Run SQL Command Line. This will open up a DOS-style window with an SQL prompt ready and waiting for you, which should look like Figure 2h below:
Now that we have started SQL*Plus, we can connect to our database schema. To do this, type CONNECT and hit enter. You will now be asked for a username followed by a password, which, if you followed my previous tutorial, will be somando and somando1, respectively. If you have supplied the correct login information, you will see a message informing you that you are connected. To make sure you are connected to the correct database, type DESC category and hit enter. If you have followed this tutorial correctly, you should see output similar to the following:
Our table isn’t much good to us if we’re not going to store any data in it, so now we will use SQL to insert a few rows into the table. Teaching you the SQL language is out of the scope of this tutorial, so if you are finding it difficult to grasp some of the statements we use, it might be a good idea to read the Oracle SQL tutorial available at http://www.db.cs.ucdavis.edu/teaching/sqltutorial/ which should bring you up to speed.
To follow the code examples in this tutorial in SQL*Plus, enter each line of code, line by line, pressing enter at the end of each line. The SQL interpreter will not process your code until you issue a semicolon or forward slash, telling it that you have finished typing your statement. Don’t be afraid to make mistakes, the best way to learn is often to figure out how to correct your errors. So now, let’s get started, type up the following code listing in your SQL Command Line window:
INSERT INTO category(name, parent_id) VALUES('Oracle', 0); INSERT INTO category(name, parent_id) VALUES('SQL Server', 0);
Listing 2b: SQL to Insert Data into category table
You will notice from this code that you are issuing two statements, each of which will create a new row in the category table. After you have entered each line, you should see the message 1 row created which indicates that your statement was executed successfully. This data will now persist for the remainder of your SQL session, but because of Oracle’s architecture, you will need to “commit” the changes in order for them to be stored permanently. This process is very simple, all you need to do is type COMMIT and press enter. A message Commit complete will tell you that the command worked.
You may have noticed that the INSERT statements we used to populate the table only featured two of the three fields in our category table. This is because we have a sequence and trigger (as set up earlier using Application Express) which automatically handle the creation of a unique number for the id field every time we perform an insert on the table.
Before we select the data back from the database, I am going to issue two commands which will help to format our data a little nicer. Do not concern yourself with these commands for now, as they are not of any particular importance. You may be wondering why I don’t issue a semi-colon or forward slash after these commands. The reason for this is that they are SQL*Plus commands rather than SQL statements. Again, don’t worry too much about the difference for now.
SET LINESIZE 300 SET HEAD OFF
Listing 2c: SQL*Plus Formatting Commands
Now that I’ve changed these settings, we are ready to select back our data from the database. I am going to issue two statements, one which returns the number of rows in the table, and a second which returns the ID and Name columns of all rows in the table.
SELECT COUNT(*) FROM category; SELECT id, name FROM category;
Listing 2d: SQL to Select Data from the category table
The first command should result in the value 2 being output to the screen. The second statement will then display two rows, the first showing 1 Oracle and the second outputting 2 SQL Server, as can be seen in the following figure:
Before we finish, I’m going to delete one of the rows we have created using a DELETE statement. This code also introduces the concept of the WHERE clause, which allows you to restrict the rows affected by an SQL statement by defining conditions which the data must meet in order for the specified action to be performed on that row.
DELETE FROM category WHERE id = 2; COMMIT;
Listing 2e: SQL to delete row from the category tablee
In this lesson, I have covered the basics of creating a table using Application Express, and working with that table using the SQL*Plus command line tool. Both of these applications are very powerful and can be used to create full-blown databases and software. In future tutorials I will be looking at creating web applications using the Oracle PL/SQL language and HTML/CSS/JavaScript, and I’ll also be showing you how to use other useful tools such as Oracle’s SQL Developer.
Click here to view a printer friendly version of this tutorial. To download this tutorial to disk, right click here and choose “Save Target As” or “Save Link As”. You will need Adobe Reader to open this file.
Install Oracle XE
Oracle Database 10g Express Edition (I will refer to this as Oracle XE from here on) is a basic version of the Oracle Database 10g that is freely available for development, deployment and distribution. It is an excellent entry-level database for learning Oracle and PL/SQL and will run on most modern PCs and laptops. I will be using Oracle XE in the majority of the Oracle tutorials and sample applications on this website, so it is highly recommended that you install it so you can follow my examples closely and precisely.
In order to install Oracle XE on your computer, you will need to download it from Oracle’s website. It is available for Microsoft Windows and x86 Linux distributions. For the purpose of this guide I will be showing you how to install Oracle XE on Microsoft Windows XP, but the procedure should be similar for other Windows operating systems.
Oracle has an extensive Installation Guide for its Oracle XE product. Windows users can view this guide at: http://download.oracle.com/docs/cd/B25329_01/doc/install.102/b25143/toc.htm.
Linux users, check out Oracle’s Installation Guide at: http://www.oracle.com/technology/software/products/database/xe/files/install.102/b25144/toc.htm.
This guide will have many similarities to Oracle’s Installation Guide above, but if you follow any naming conventiones I use during installation it will make it much easier for you to follow any future Oracle guides I make available.
The first step in installing Oracle XE is to download the installation package from Oracle’s website. The direct URL to the Windows binary download page is http://www.oracle.com/technology/software/products/database/xe/htdocs/102xewinsoft.html. On this page, you must first accept the License Agreement by clicking on the radio button towards the top of the page. Once you have clicked on this, you can click on the relevant download links to download the installer. It is worth mentioning at this stage that you must be logged in to the Oracle Technology Network (OTN) to download Oracle XE. Registration to OTN is free and if you are not logged in you will be prompted to login or register before you will be allowed to proceed with the download.
The version I am using in my guides is the Oracle Database 10g Release 2 (10.2.0.1) Express Edition for Microsoft Windows (Western European). The download is highlighted in blue on Figure 1a above. It weighs in at a heavy 157mb, so it’s best to download this over a high-speed Internet connection if possible.
Once you have downloaded the installation package, run the OracleXE.exe file to start the installer.
On this welcome screen (see Figure 1b), press Next (Alt+N) to continue to the next screen.
On the License Agreement screen (Figure 1c), select “I accept the terms in the license agreement” (Alt+A) and then press the Next button (Alt+N) to move on.
On the “Choose Destination Location” screen (Figure 1d) leave the default values as they are in Figure 1d and press Next (Alt+N) to continue.
On this screen (“Specify Database Passwords” – Figure 1e), you can enter a password that will be used for the SYS and SYSTEM database accounts. Throughout the course of this guide I will always use the password somando1 and I suggest you do the same to make it easier to follow my guides. Obviously these passwords should only be used on development machines, with much stronger and secure passwords being used on production servers. Once you have entered and confirmed the password, hit Next (Alt+N) to continue.
You will now be presented with a summary of the installation options (make a note of the port numbers, they should be as in Figure 1f). If all looks in order, click Install (Alt+I).
The installation should now begin, and you may see a screen similar to Figure 1g. The installer will automatically copy the relevant files to your computer and will configure the database. This process may take a considerable amount of time depending on your computer’s specification. On my machine (an Intel Core 2 Duo T7300 2.00GHz with 2GB of RAM) the process took less than five minutes.
Once the installation has completed you will see a screen like in Figure 1h above. Leave the checkbox for “Launch the Database homepage” checked and click the Finish button. You have now successfully installed Oracle XE on your computer.
If you left the checkbox in Figure 1h checked, your default web browser will open and you will see a page similar to that shown in Figure 1i. Enter SYS in the field for Username and somando1 in the field for Password and click the Login button.
If you were able to login successfully, you should see a page similar to that in Figure 1j, which is basically a web-based control panel called Application Express. This utility is in fact a powerful web application which provides you complete control over your database. At this point I highly recommend that you click on the “Getting Started” link at the top right hand section of this screen, which will bring you to a local version of Oracle’s Database Express Edition Getting Started Guide. This guide can also be accessed online at http://download.oracle.com/docs/cd/B25329_01/doc/admin.102/b25610/toc.htm.
The Getting Started Guide will walk you through the process of unlocking a sample user, logging in under the sample user’s account, and creating and running a simple application with Application Express. It will take you 20 minutes to get through, and will give you an idea of how simple it is to create powerful applications using Application Express.
Finally, I will create a user/schema that I will be using in my guides. All the tables that I create in future guides will be created in this schema. To create a user, open the Database Home Page (Start -> Programs -> Oracle Database 10g Express Edition -> Go To Database Home Page) and login with the username SYSTEM and password somando1. Click on the Administration image, then click on Database Users, and then the “Create >” button.
On the Create Database User screen (as in Figure 1k above), enter the following details:
- Username: somando
- Password: somando1
- Confirm Password: somando1
- Expire Password: Leave this option unchecked
- Account Status: Unlocked
- Roles: Connect and Resource should be checked, DBA should be unchecked.
- Click on the Check All link at the bottom right of the User Privileges box to give this user all Direct Grant System Privileges.
Once you have filled out the form accordingly, click on the Create button at the top right hand corner of the Create Database User box. Once you click this button, the user/schema somando should be created. In order to verify that the user has been set up correctly, click on the Logout link at the right hand top corner of the page, and log back in, except this time log in with the username somando and the password somando1. If you can successfully log in, the user/schema has been created successfully.
That concludes this tutorial. In the next guide, I will be looking at creating tables and inserting data using Application Express. I will also be introducing the SQL Command Line (SQL*Plus) and some basic SQL statements.
Click here to view a printer friendly version of this tutorial. To download this tutorial to disk, right click here and choose “Save Target As” or “Save Link As”. You will need Adobe Reader to open this file.






























