1. Introduction:In a relational database, like Oracle Database Express Edition (XE), we can store and retrieve information. A database, as a database server,manages information. In a relational database, information are organized into structures called tables. Each table contains rows (records) that are composed of columns (fields). The tables are stored in the database in logical structures called schemas. Structured Query Language (SQL) is a nonprocedural programming language that enables to access a relational database. Using SQL statements, we can query tables to display data, create objects, modify objects, and perform administrative tasks.The SQL language compiler generates a procedure to navigate the database and perform the desired task. PL/SQL is an Oracle's procedural language extension to SQL . With PL/SQL, we can create procedures, functions, and packages. PL/SQL program units are constructed as anonymous blocks, stored functions, stored procedures, and packages:. 1.1. An anonymous block used in an application is not named or stored in the database. It groups the related declarations and statements. 1.2.A procedure or function are stored in the database and can be called by name from an application. Functions return a value when executed, whereas procedures are void. A procedure follows some rules. It starts with BEGIN and finish with END. 1.3. A package is a group of procedures, functions, and variable definitions stored in the database. They can be called from other packages, procedures, or functions. 1.4. A database trigger is a stored procedure associated with a database table, view, or event. The trigger can be called after the event, to record it, or take some follow-up action. The trigger can also be called before an event to fix new data. 2. Examples2.1 Example 1:PL/SQL: Oracle database and JDBCWe need to CONNECT to the Oracle database: 2.1.1. Connecting to Oracle 10g Database: SQL*Plus: Release 10.2.0.1.0 - Production on Thu Feb 28 09:37:14 2008 Copyright (c) 1982, 2005, Oracle. All rights reserved. SQL> connect Enter user-name: System Enter password: Connected. SQL> Then CREATE a table: 2.1.2. Creating a table: Physicists: SQL> CREATE TABLE Physicists( 2 phys_id NUMBER(3,0) NOT NULL, 3 first_name VARCHAR2(10) NOT NULL, 4 last_name VARCHAR2(15), 5 birth_date NUMBER(4,0), 6 death_date NUMBER(4,0), 7 discipline VARCHAR2(20)); Table created. And INSERT values in the table: 2.1.3. Fill out the table: 2.1.8. PL-SQL with JDBC: The result: Compile as: C:\J2EE\PL-SQL>javac SearchPhysicist.java Search for a physicist: C:\J2EE\PL-SQL>java SearchPhysicist l d 106 Louis DeBroglie 1892 1987 wave_mechanics C:\J2EE\PL-SQL>java SearchPhysicist j th 101 Joseph Thomson 1856 1940 atomic C:\J2EE\PL-SQL>java SearchPhysicist m Di 108 Maurice Dirac 1902 1984 atomic C:\J2EE\PL-SQL> 2.2 PL/SQL: Oracle database and PHP:
As PHP connects to MySQL, it connects also with Oracle:
With MySQL: <?php $conn = @mysql_connect("localhost", "root", "mypasswd");?>
With Oracle-PHP: $conn = oci_connect('user-name', 'password', '//localhost:1521/XE');
or: Oracle-JDBC:
OracleDataSource ods = new OracleDataSource();
SQL: ods.setURL("jdbc:oracle:thin@host:1521:XE","user-name","password");
PL/SQL: ods.setURL("jdbc:oracle:thin:user-name/password@host:1521/XE");
Connection conn = ods.getConnection();
To use this Oracle Call Interface (OCI) connection, once php is installed:
Go to php.ini file ( in this case in C:\WINDOWS\ directory), and uncomment
- Uncomment extension=php_oci8.dll (delete the ;)
- Then start the Apache.
Test your connection with this:
Here is an example (phys_search.php) allowing the connection to the XE Oracle database
and Physicists table. The form used gives two text area to search for a physicist
by entering her/his first and last names (or some first initials).
Save the file in a directoy related web server (http://localhost:8033/PLSQL/)
and double-click on the phys_search.php file
Here is the relate dprogram:
phys_search.php
The result are:
Query
The output:
Result
For more infor abou atomic physicists: galance at: atomic physicists |