PLSQL
PLSQL
What is PL/SQL
Procedural Language – SQL
An extension to SQL with design features of
:=
is not the same as the equality operator
=
All statements end with a ;
PL/SQL Sample Program
DECLARE
v_inv_value number(10,2);
v_price number(8,2) := 10.25;
v_quantity number(8,0) := 400;
BEGIN
v_inv_value := v_price * v_quantity;
dbms_output.put('The value is: ');
dbms_output.put_line(v_inv_value);
END;
PL/SQL Comments
DECLARE
v_salary number(9,2) := 40000;
BEGIN
/* this is a multi-line comment that
will be ignored by the pl/sql
interpreter */
v_salary := v_salary * 2;
END; -- end of program
PL/SQL Sample Program
(For Loop)
DECLARE
x NUMBER := 100;
BEGIN
FOR i IN 1..10 LOOP
IF MOD(i,2) = 0 THEN -- i is even
INSERT INTO temp VALUES (i, x, 'i is even');
ELSE
INSERT INTO temp VALUES (i, x, 'i is odd');
END IF;
x := x + 100;
END LOOP;
COMMIT;
END;
Example
Consider Students table with columns
(sid, fname, lname, major, gpa)
Example
DECLARE
v_max_gpa number(3,2);
v_numstudents number(4);
v_lname students.lname%type; Display the highest GPA
v_major students.major%type;
BEGIN
select max(gpa) into v_max_gpa
from students;
DBMS_OUTPUT.PUT_LINE ('The highest GPA is '||v_max_gpa);
select count(sid) into v_numstudents Retrieve the no. of students with highest GPA
from students
where gpa = v_max_gpa;
IF v_numstudents > 1 then
If no. of students
DBMS_OUTPUT.PUT_LINE ('There are '||v_numstudents||' withare moreGPA');
that than 1, then
ELSE display the count or else display the
select lname, major into v_lname, v_major lname and major of the hisghest GPA
from students scorer.
where gpa=v_max_gpa;
DBMS_OUTPUT.PUT_LINE ('The student name is '||v_lname);
DBMS_OUTPUT.PUT_LINE ('The student major is '||v_major);
END IF;
END;
/
%ROWTYPE
Set serveroutput on
DECLARE
v_student students%rowtype;
BEGIN
select * into v_student
from students
where sid='123456';
DBMS_OUTPUT.PUT_LINE (v_student.lname);
DBMS_OUTPUT.PUT_LINE (v_student.major);
DBMS_OUTPUT.PUT_LINE (v_student.gpa);
END;
/
Stored Procedures
PL/SQL code stored in the database and executed when called by the
user.
Called by procedure name from another PL/SQL block or using
EXECUTE from SQL+. For example EXEC SQR(50)
Example:
BEGIN
SQR(19);
END;
Function
PL/SQL user defined function stored in the database and
executed when a function call is made in code: example x :=
SQUARED(50)
Example:
ResultSet
Implicit cursors are created for every query
made in Oracle
Explicit cursors can be declared by a
v_student students_cursor%rowtype;
/* instead we could do v_student students%rowtype */
BEGIN
DBMS_OUTPUT.PUT_LINE ('******************');
OPEN students_cursor;
FETCH students_cursor into v_student;
WHILE students_cursor%found LOOP
DBMS_OUTPUT.PUT_LINE (v_student.last);
DBMS_OUTPUT.PUT_LINE (v_student.major);
DBMS_OUTPUT.PUT_LINE ('******************');
FETCH students_cursor into v_student;
END LOOP;
CLOSE students_cursor;
END;
Sample Cursor Program
(same program without composite variable)
DECLARE
CURSOR students_cursor IS
SELECT last, major from students;
v_Last students.last%type;
v_major students.major%type;
BEGIN
DBMS_OUTPUT.PUT_LINE ('******************');
OPEN students_cursor;
FETCH students_cursor into v_last, v_major;
WHILE students_cursor%found LOOP
DBMS_OUTPUT.PUT_LINE (v_last);
DBMS_OUTPUT.PUT_LINE (v_major);
DBMS_OUTPUT.PUT_LINE ('******************');
FETCH students_cursor into v_last, v_major;
END LOOP;
CLOSE students_cursor;
END;
/
Stored Procedure Example
Write a procedure using cursor to display all
tablenames from user tables of Oracle database
Stored Procedure Example
Write a procedure using cursor to display all
tablenames from user tables of Oracle database
Create or replace procedure mytabs
AS
CURSOR table_cursor IS
Select table_name from user_tables;
v_tablename varchar2(30);
BEGIN
open table_cursor;
fetch table_cursor into v_tablename;
while table_cursor%found loop
dbms_output.put_line(v_tablename);
fetch table_cursor into v_tablename;
end loop;
close table_cursor;
END;
When is PL/SQL handy
When something is too complicated for SQL
When conditional branching and looping are
needed
JDBC
JDBC is a standard interface for connecting to
relational databases from Java.
The JDBC classes and interfaces are in the
java.sql package.
JDBC 1.22 is part of JDK 1.1; JDBC 2.0 is part
of Java 2
Overview of Querying a
Database With JDBC
Connect
Query
Process
results
Close
Stage 1: Connect
Process
results
Close
A JDBC Driver
Is an interpreter that translates JDBC method
calls to vendor-specific database commands
Database
JDBC calls commands
Driver
Database
Applet
JDBC
Client Server
Oracle JDBC Drivers: OCI Client
Drivers
Written in C and Java
Must be installed on the client
Application
JDBC
OCI driver
Oracle
ocixxx.dll
Client Server
Other JDBC Drivers
JDBC-ODBC Bridge
◦ Translates JDBC into open database connectivity
(ODBC) calls
◦ Allows communication with existing ODBC
drivers when no JDBC driver is available
JDBC URLs
JDBC uses a URL to identify the database
connection.
jdbc:<subprotocol>:<subname>
Database
Protocol Subprotocol
identifier
jdbc:oracle:<driver>:@<database>
JDBC URLs with Oracle
Drivers
Thin driver
jdbc:oracle:thin:@<host>:<port>:<SID>
OCI driver
jdbc:oracle:oci8:@<TNSNAMES entry>
How to Make the
Connection
1. Register the driver.
DriverManager.registerDriver (new
oracle.jdbc.driver.OracleDriver());
2.
2. Connect
Connect to
to the
the database.
database.
Connection conn = DriverManager.getConnection
(URL, userid, password);
("jdbc:oracle:thin:@localhost:1521:XE","scott
2","tiger");
Stage 2: Query
Connect
Close
The Statement Object
A Statement object sends your SQL
statement to the database.
You need an active connection to create a
JDBC statement.
Statement has three methods to execute a
SQL statement:
◦ executeQuery() for QUERY statements
◦ executeUpdate() for INSERT, UPDATE, DELETE,
or DDL statements
◦ execute() for either type of statement
How to Query the
Database
1. Create an empty statement object.
Query
Step through the results
Close
The ResultSet Object
JDBC returns the results of a query in a
ResultSet object.
A ResultSet maintains a cursor pointing to
by row.
getString(), getInt(), and so on assign each
while (rset.next()) {
String title = rset.getString("TITLE");
String year = rset.getString("YEAR");
… // Process or display the data
}
Stage 4: Close
Connect
Query
Close the result set
Process
results Close the statement