2005_604_wagner_slides

89
USING ORACLE PL/SQL AND JDBC TO MANAGE FILE UPLOADS WITHIN THE DATABASE - A HANDS-ON EXAMPLE Mary Wagner Delaware Department of Transportation Session #604

Upload: mary-wagner

Post on 12-Apr-2017

118 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2005_604_Wagner_slides

USING ORACLE PL/SQL AND JDBC TO MANAGE FILE UPLOADS WITHIN THE DATABASE - A HANDS-ON EXAMPLE

Mary WagnerDelaware Department of

TransportationSession #604

Page 2: 2005_604_Wagner_slides

MY GOAL

• Provide a useful solution• Give you all the code so you can

try it yourself

Page 3: 2005_604_Wagner_slides

My Background• Currently employed as a Database

Administrator / Developer• Certified in Oracle 8i and 9i

10 years of IT Experience including…• Oracle and SQL Server Database Administration• Database design and development• Java and PL/SQL development• Internet / Intranet web site development • LAN Administration

Page 4: 2005_604_Wagner_slides

Session Objectives

• Storing BLOBs– Using the Oracle Data Type

• Uploading Binary Files– Using PL/SQL and Java Streaming

• Viewing Binary Files– Using PL/SQL and Java Streaming

Page 5: 2005_604_Wagner_slides

What is a BLOB?

• An Oracle Data Type• A Type of Large Object (LOB)• A Binary File stored inside an

Oracle database

Page 6: 2005_604_Wagner_slides

What is a BLOB (con’t) ?

• Spreadsheets• Word Processing Documents• Adobe Acrobat Documents• Image Files• Any other type of binary file stored

in the database ….

Page 7: 2005_604_Wagner_slides

Why Use Them?

• File System Dependence Removed• Files are Included in Database

Backups• Files are More Secure• Higher Recoverability

Page 8: 2005_604_Wagner_slides

An Example - myDMS

• Small Document Management System (50mb or less)

• Uses JDBC and PL/SQL• Documents are Categorized by

Topic• Documents can be Uploaded,

Viewed and Deleted

Page 9: 2005_604_Wagner_slides

An Example – myDMS (con’t)

• Database Server - Oracle 9i• Java Server - Weblogic 8.1• Application Architecture - Java MVC• Java IDE – Intellij IDEA

Page 10: 2005_604_Wagner_slides

myDMS – Logging In• Accounts – Manager and User

Page 11: 2005_604_Wagner_slides

myDMS – View Document List

• After logging in a list of uploaded documents is displayed….

Page 12: 2005_604_Wagner_slides

myDMS – View a Document

• After clicking the “view” link the BLOB is displayed…

Page 13: 2005_604_Wagner_slides

myDMS – Upload a Document

• After clicking the “upload new document” link, the upload form is displayed…

Page 14: 2005_604_Wagner_slides

myDMS – Delete a Document

• After clicking the “delete” link, the document is deleted…

Page 15: 2005_604_Wagner_slides

myDMS – View Topic List• After clicking the “topic list” link, the

list of topics is displayed…

Page 16: 2005_604_Wagner_slides

myDMS – Add a Topic• After clicking the “add new topic”

link, a new topic can be added…

Page 17: 2005_604_Wagner_slides

myDMS – Delete a Topic• After clicking the “delete” link, the

topic is deleted…

Page 18: 2005_604_Wagner_slides

myDMS – Edit a Topic• After clicking the “edit” link, the topic

can be modified…

Page 19: 2005_604_Wagner_slides

myDMS – Development Phases

• Phase 1 – Create Tablespaces, Users, System Objects and Tables

• Phase 2 – Create PL/SQL API• Phase 3 – Create Java Objects• Phase 4 – Putting it Together

Page 20: 2005_604_Wagner_slides

myDMS – Phase 1.

• Create Tablespaces • Create Users• Create System Objects• Create Application Tables

Page 21: 2005_604_Wagner_slides

myDMS – Phase 1. - Tablespaces

• MYDMS_DATA

CREATE TABLESPACE MYDMS_DATA DATAFILE'C:\ORACLE\ORADATA\MW92\MYDMS_DATA01.DBF' SIZE 10304K EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;

Page 22: 2005_604_Wagner_slides

myDMS – Phase 1. – Tablespaces (con’t)

• MYDMS_BLOB

CREATE TABLESPACE MYDMS_BLOB DATAFILE'C:\ORACLE\ORADATA\MW92\MYDMS_BLOB01.DBF' SIZE 51264K EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;

Page 23: 2005_604_Wagner_slides

myDMS – Phase 1. – Users• MYDMS – create schema ownerCREATE USER MYDMS IDENTIFIED BY MYDMSDEFAULT TABLESPACE MYDMS_DATA TEMPORARY TABLESPACE TEMP QUOTA UNLIMITED ON MYDMS_BLOB QUOTA UNLIMITED ON MYDMS_DATA;GRANT CONNECT TO MYDMS;GRANT CREATE PROCEDURE TO MYDMS;GRANT CREATE TRIGGER TO MYDMS;

Page 24: 2005_604_Wagner_slides

myDMS – Phase 1. – Users (con’t)• MYDMS_USER – create

application userCREATE USER MYDMS_USER IDENTIFIED BY MYDMS_USER DEFAULT TABLESPACE USERS

TEMPORARY TABLESPACE TEMP;GRANT CONNECT TO MYDMS_USER;

Page 25: 2005_604_Wagner_slides

myDMS – Phase 1. – Users (con’t)• MYDMS_USER – create logon triggerCREATE OR REPLACE TRIGGER

MYDMS_USER.TRG_LOGON_SET_SCHEMA AFTER LOGON ON MYDMS_USER.SCHEMABEGINEXECUTE IMMEDIATE 'ALTER SESSION SET

CURRENT_SCHEMA=MYDMS';END TRG_LOGON_SET_SCHEMA; /

Page 26: 2005_604_Wagner_slides

myDMS – Phase 1. – System ObjectsTABLES………@create_tables_action_and_user.sql• ACTIONS• USERTYPE• USERS• USERACCESS

Page 27: 2005_604_Wagner_slides

myDMS – Phase 1. – System Objects (con’t)PL/SQL PACKAGE………@create_package_mvc.sql • PKG_MVC

– User access and verification (verify_user)

– Application navigation meta information (get_action_info)

Page 28: 2005_604_Wagner_slides

myDMS – Phase 1. – Application Tables

There are 3 application tables…• DOCUMENTS (contains BLOB)• TOPICS • DOCUMENTDETAILS

Page 29: 2005_604_Wagner_slides

myDMS – Phase 1. – Application Tables (con’t)DOCUMENTS – the BLOB table@create_table_documents.sql• LOB Initialization -• Enable Storage in Row - • LOB Tablespace – • Chunk Size – • Lob Cache -

Page 30: 2005_604_Wagner_slides

myDMS – Phase 1. – Application Tables (con’t)• DOCUMENTS – the BLOB table--create tableCREATE TABLE DOCUMENTS (DOCUMENTID NUMBER(10) NOT NULL, BINARYFILE BLOB DEFAULT EMPTY_BLOB() NOT NULL,DATESTAMP DATE DEFAULT SYSDATE NOT NULL, CONSTRAINT PK_DOCUMENTS_DOCUMENTID PRIMARY

KEY(DOCUMENTID)) TABLESPACE MYDMS_BLOB; --create sequence to populate primary keyCREATE SEQUENCE SEQ_DOCUMENTS_DOCUMENTID;

Page 31: 2005_604_Wagner_slides

myDMS – Phase 1. – Application Tables (con’t)• TOPICS - @create_table_topics.sql 

Page 32: 2005_604_Wagner_slides

myDMS – Phase 1. – Application Tables (con’t)• DOCUMENTDETAILS -

@create_table_document_details.sql 

Page 33: 2005_604_Wagner_slides

myDMS – Phase 2 – PL/SQL API• PKG_DMS - @create_package_dms.sql

– Includes stored procedures that handle all database transaction processing

– Includes global variables and functions used by stored procedures

– Called by related Java DAO methods

Page 34: 2005_604_Wagner_slides

myDMS – Phase 2 – PL/SQL API (con’t)• PKG_DMS – stored procedures

– VIEW_DOCUMENT_LIST– UPLOAD_DOCUMENT– VIEW_DOCUMENT– DELETE_DOCUMENT

Page 35: 2005_604_Wagner_slides

myDMS – Phase 3 – Java Objects• Model – View – Controller (MVC)

– MODEL (value object or VO)– VIEW (java server page of jsp)– CONTROLLER – ACTIONS– DATA ACCESS OBJECTS (dao)

Page 36: 2005_604_Wagner_slides

myDMS – Phase 3 – Value Objects• DocumentVO

– Place holder for data retrieved from database

– Setter methods called by DAOs– Getter methods called by JSPs

Page 37: 2005_604_Wagner_slides

myDMS – Phase 3 – Value Objects (con’t)• DocumentVO 

Page 38: 2005_604_Wagner_slides

myDMS – Phase 3 – Views

• viewDocumentList.jsp• viewDocumentServlet• formUploadDocument.jsp

Page 39: 2005_604_Wagner_slides

myDMS – Phase 3 – Servlets

• ControllerServlet • ProcessRequestServlet

Page 40: 2005_604_Wagner_slides

myDMS – Phase 3 – Action Classes• ViewDocumentListAction • ViewDocumentAction• FormUploadDocumentAction• SaveUploadDocumentAction• SaveDeleteDocumentAction

Page 41: 2005_604_Wagner_slides

myDMS – Phase 3 – Data Access Objects

• BaseDAO • DocumentDAO

Page 42: 2005_604_Wagner_slides

myDMS – Phase 4 – Putting it all Together

• Viewing a Document Listing • Viewing an Uploaded File• Displaying the Upload Form• Saving the Upload Form• Deleting an Uploaded File

Page 43: 2005_604_Wagner_slides

myDMS – Phase 4 – Viewing a Document Listing• ACTION ViewDocumentListAction• DAO viewDocumentList• PKG_DMS VIEW_DOCUMENT_LIST• VIEW viewDocumentList.jsp

Page 44: 2005_604_Wagner_slides

myDMS – Phase 4 – Viewing a Document Listing (con’t)• ACTION ViewDocumentListAction

Page 45: 2005_604_Wagner_slides

myDMS – Phase 4 – Viewing a Document Listing (con’t)• DAO viewDocumentList

Page 46: 2005_604_Wagner_slides

myDMS – Phase 4 – Viewing a Document Listing (con’t)• PKG_DMS VIEW_DOCUMENT_LIST

Page 47: 2005_604_Wagner_slides

myDMS – Phase 4 – Viewing a Document Listing (con’t)• VIEW viewDocumentList.jsp

Page 48: 2005_604_Wagner_slides

myDMS – Phase 4 – Viewing an Uploaded File• ACTION ViewDocumentAction• DAO viewDocument• PKG_DMS VIEW_DOCUMENT• VIEW ViewDocumentServlet

Page 49: 2005_604_Wagner_slides

myDMS – Phase 4 – Viewing an Uploaded File (con’t)• ACTION ViewDocumentAction

Page 50: 2005_604_Wagner_slides

myDMS – Phase 4 – Viewing an Uploaded File (con’t)• VIEW ViewDocumentServlet

Page 51: 2005_604_Wagner_slides

myDMS – Phase 4 – Viewing an Uploaded File (con’t)• DAO viewDocument

– Step 1.) Retrieve BLOB from Database

Page 52: 2005_604_Wagner_slides

myDMS – Phase 4 – Viewing an Uploaded File (con’t)• PKG_DMS VIEW_DOCUMENT

Page 53: 2005_604_Wagner_slides

myDMS – Phase 4 – Viewing an Uploaded File (con’t)• DAO viewDocument (con’t)

– Step 2.) Set output and input Streams

Page 54: 2005_604_Wagner_slides

myDMS – Phase 4 – Viewing an Uploaded File (con’t)• DAO viewDocument (con’t)

– Step 3.) Set buffer and write file to view Servlet

Page 55: 2005_604_Wagner_slides

myDMS – Phase 4 – Displaying Upload Form

• ACTION FormUploadDocumentAction• DAO viewTopicList• PKG_DMS VIEW_TOPIC_LIST• VIEW formUploadDocument.jsp

Page 56: 2005_604_Wagner_slides

myDMS – Phase 4 – Displaying Upload Form (con’t)• ACTION FormUploadDocumentAction

Page 57: 2005_604_Wagner_slides

myDMS – Phase 4 – Displaying Upload Form (con’t)• DAO viewTopicList

Page 58: 2005_604_Wagner_slides

myDMS – Phase 4 – Displaying Upload Form (con’t)• PKG_DMS VIEW_TOPIC_LIST

Page 59: 2005_604_Wagner_slides

myDMS – Phase 4 – Displaying Upload Form (con’t)• VIEW formUploadDocument.jsp

Page 60: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form

• ACTION SaveUploadDocumentAction• DAO uploadDocument• PKG_DMS UPLOAD_DOCUMENT• VIEW viewDocumentList.jsp

Page 61: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form (con’t)• ACTION SaveUploadDocumentAction

– Step 1.) Declare max allowable file size and MultiPart Content boolean

Page 62: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form (con’t)• ACTION SaveUploadDocumentAction

(con’t)– Step 2.) If form field is a file, build

DiskFileUpload item and declare iterator

Page 63: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form (con’t)• ACTION SaveUploadDocumentAction

(con’t)– Step 3.) If form field is not a file, get values

Page 64: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form (con’t)• ACTION SaveUploadDocumentAction

(con’t)– Step 4.) Get file, set input stream and variables

and validate form

Page 65: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form (con’t)• ACTION SaveUploadDocumentAction

(con’t)– Step 5.) If validation succeeds, call DAO to begin

upload process

Page 66: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form (con’t)• DAO uploadDocument

– Step 1.) Send form info to the database

Page 67: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form (con’t)• DAO uploadDocument (con’t)

– Step 2.) Get returned blob locator, set chunk, buffer, length and output stream

Page 68: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form (con’t)• DAO uploadDocument (con’t)

– Step 3.) Stream the uploaded file to the database

Page 69: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form (con’t)• DAO uploadDocument (con’t)

– Step 4.) Set data for updated document list view and commit transaction

Page 70: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form (con’t)• PKG_DMS UPLOAD_DOCUMENT

– Step 1.) Insert Initial Record into BLOB Table and return BLOB locator

Page 71: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form (con’t)• PKG_DMS UPLOAD_DOCUMENT (con’t)

– Step 2.) Insert record into DOCUMENTDETAILS

Page 72: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form (con’t)• PKG_DMS UPLOAD_DOCUMENT (con’t)

– Step 3.) Return updated data for document list, which is displayed if upload succeeds

Page 73: 2005_604_Wagner_slides

myDMS – Phase 4 – Saving Upload Form (con’t)• VIEW viewDocumentList.jsp

Page 74: 2005_604_Wagner_slides

myDMS – Phase 4 – Deleting an Uploaded File• ACTION SaveDeleteDocumentAction• DAO deleteDocument• PKG_DMS DELETE_DOCUMENT• VIEW viewDocumentList.jsp

Page 75: 2005_604_Wagner_slides

myDMS – Phase 4 – Deleting an Uploaded File (con’t)• ACTION SaveDeleteDocumentAction

Page 76: 2005_604_Wagner_slides

myDMS – Phase 4 – Deleting an Uploaded File (con’t)• DAO deleteDocument

Page 77: 2005_604_Wagner_slides

myDMS – Phase 4 – Deleting an Uploaded File (con’t)• PKG_DMS DELETE_DOCUMENT

Page 78: 2005_604_Wagner_slides

myDMS – Phase 4 – Deleting an Uploaded File (con’t)• VIEW viewDocumentList.jsp

Page 79: 2005_604_Wagner_slides

myDMS – Phase 4 – Deploying the Application• Create JDBC Connection Pool • Create JNDI Data Source• Reference JNDI Data Source in

web.xml File• Deploy Application Archive or

Exploded Folder on your Java Server

Page 80: 2005_604_Wagner_slides

What We’ve Covered…

• Storing BLOBs– Using the Oracle Data Type

• Uploading Binary Files– Using PL/SQL and Java Streaming

• Viewing Binary Files– Using PL/SQL and Java Streaming

Page 81: 2005_604_Wagner_slides

What to do Next…

• Download the Java code and database object creation scripts

• Create the necessary database objects using the database scripts

• Create the necessary java objects using the Java files

• Deploy the application in your test environment

Page 82: 2005_604_Wagner_slides

General Suggestions…

• Be Consistent with Object Naming• Close Java Objects in Finally

Clauses• Take Your Environment into

Account when Using Examples• Test Everything as Much as

Possible

Page 83: 2005_604_Wagner_slides

Scripts / Contact InfoAll of the code and scripts for this

presentation can be downloaded at —• http://www.dbatasktracker.com

Please let me know if there are any errors in the scripts or if you need help with the scripts —

[email protected]

Page 84: 2005_604_Wagner_slides

Additional Resources (Oracle Docs)• Oracle 9i Application Developer's Guide

- Large Objects (LOBs)• Oracle 9i JDBC Developer's Guide and

Reference - Working with LOBs and BFILEs (Chapter 8)

• Oracle 9i Supplied PL/SQL Packages and Types Reference - DBMS_LOB (Chapter 23)

Page 85: 2005_604_Wagner_slides

Additional Resources (Books)• Expert Oracle One on One - Tom Kyte• Effective Oracle By Design - Tom Kyte • Mastering Oracle PL/SQL - Connor

MacDonald• Sun Certified Web Component

Developer for J2EE - Sam Dalton, Dan Jepp

Page 86: 2005_604_Wagner_slides

Additional Resources (Web Sites)• http://asktom.oracle.com• http://java.sun.com• http://forums.java.sun.com• http://www.jguru.com• http://jakarta.apache.org• http://struts.apache.org• http://e-docs.bea.com/wls/docs81/jdbc/

Page 87: 2005_604_Wagner_slides

Any Questions?

• ??????????????????????????????

Page 88: 2005_604_Wagner_slides

Thank You

• Thanks for coming! • Please complete the evaluation

form– Speaker: Mary Wagner– Session#: 604– Session Name: Using Oracle 9i Pl/SQL

and JDBC to Manage File Uploads Within The Database

Page 89: 2005_604_Wagner_slides

Disclaimer– The content provided as papers and

presentations from the IOUG conferences is copyrighted by the authors of information, and has been licensed to the IOUG. It is only authorized for the personal use of IOUG members and IOUG conference attendees directly through the IOUG web site. Downloading the files, placing them on other web sites or sharing them with other individuals is prohibited.