33
copyright 2004 all rights reserved www.r hinosystemsinc.com 1 JAVA & PL/SQL For Oracle RDBMS (8i & 9i) By Joel A. Thompson ([email protected]) www.rhinosystemsinc.com 05/2004

Java and Plsql-1hr

Embed Size (px)

DESCRIPTION

Java and PLSQL

Citation preview

  • JAVA & PL/SQLFor Oracle RDBMS (8i & 9i)By Joel A. Thompson ([email protected])www.rhinosystemsinc.com05/2004

    all rights reserved www.rhinosystemsinc.com

  • INTRODUCTIONJoel A. Thompson15 years of industry experienceConsultant: Architecture, Software Engineering and Project ManagerJava and Oracle expert

    all rights reserved www.rhinosystemsinc.com

  • OVERVIEWBasics of PLSQLOptimizing RDBMS Calls from JAVAReturn Values & CursorsLogic processing on Server, ARRAY insertsAnd more ExamplesWRAP UP / Q&A

    all rights reserved www.rhinosystemsinc.com

  • Getting Started with JavaMust install a JDK & IDEJDK (Java Developers Kit) from Sun http://java.sun.com/j2se/1.4/index.htmlIDE (Integrated Developers Environment)Oracles JDeveloper (technet.oracle.com)IntelliJ: (www.intellij.net)Textpad: http://www.textpad.com

    all rights reserved www.rhinosystemsinc.com

  • Getting Started with OracleServer Install Oracle (Steps)Download/Install from technet.oracle.comCreate the database, use Oracle's Database Configuration Assistant.Make sure network listeners are started typically your instance will be available on port 1525 use Oracle's Net Configuration Assistant, to configure a "Listener".Client RequirementCopy the $ORACLE_HOME/jdbc/lib/classes12.jar to your client side's CLASSPATH.Connect string example (thin driver) requires no client side libraries: jdbc:oracle:thin:joel/welcome@localhost:1521:UCDBA

    all rights reserved www.rhinosystemsinc.com

  • Client Server Java/PLSQL Architecture

    all rights reserved www.rhinosystemsinc.com

  • What is PL/SQL?A programming languageIf/then/elseLoopsFunction callsTransactionalProceduralFeature richPerformance GainOracle only

    all rights reserved www.rhinosystemsinc.com

  • Some Good Reasons to use PL/SQLBasically you'd like to do some logic on the server side in one call to the databaseTransaction support within the PL/SQLTemporary table queries/inserts to filter data further before returning result setTake advantage of server side resources while processing you PL/SQL send message out through Oracle's Advanced Queuing. Distributed Computing update another databaseYou want to abstract your database layer into Oracle, such that you can change the table's or columns around without affecting the client program.

    all rights reserved www.rhinosystemsinc.com

  • Ways to call PL/SQLADHOCSend the PL/SQL block of code from the client java program to the server for processing.FUNCTIONClient Side calls a PL/SQL function in Oracle. PL/SQL is already compiled and loaded in Oracle.Function will return a valuePROCEDUREClient calls a PL/SQL procedureSimilar to function, but does not return values.

    all rights reserved www.rhinosystemsinc.com

  • ADHOC: Java Client Example 1SQL embedded in call from Java Client// EXAMPLE OF SIMPLE SELECT STATEMENTStatement stmt=connection.createStatement();

    // QUERY THE ENTIRE TABLEString sSQL="select ACCOUNT_ID,SSNO,FNAME,LNAME,PHONE from PERSON order by account_id";

    // GET THE RESULT SET AND PROCESS IT.ResultSet rs=stmt.executeQuery(sSQL);while(rs.next()){ nPERSON_ID=rs.getInt(1); sSSNO=rs.getString(2);/// check to see if matches with new account you'd like to add.}

    // IF ACCOUNT DOESN'T ALREADY EXIST, THEN ADD IT HERE:stmt.executeUpdate("insert into PERSON(ACCOUNT_ID,SSNO,FNAME,LNAME) values(account_id_seq.nextval," + "'" + sSSNO + "'," + "'" + sFNAME + "'," +"'" + sLNAME + "'");

    all rights reserved www.rhinosystemsinc.com

  • Issues Regarding Example 1Selecting from entire table is generally a bad idea. (unless small "lookup table").One call for the Query, and 2nd call to create the account.

    all rights reserved www.rhinosystemsinc.com

  • ADHOC: Java Client Example 2Prepared Statement/Bind from Java Clientpublic void initializeOnce(){ // THE SQL INSERT TO BE MADE.String sQueryNotify="insert into PERSON (PERSON_ID,SSNO,FNAME,LNAME)" + " values (person_id_seq.nextval,?,?,?)";

    // CREATE A PREPARED STATEMENT, BASED ON THE ABOVE SQLm_psInsertPerson=conn.prepareStatement(sQueryNotify);}

    public void insertNewPerson(String SSNO,String FNAME,String LNAME){ // BIND THE PARAMETER - REUSE THE ALREADY CREATED PREPARED STATEMENT m_psInsertPerson.setString(1, SSNO); m_psInsertPerson.setString(2, FNAME); m_psInsertPerson.setString(3, LNAME);

    // EXECUTE THE INSERT m_psInsertPerson.executeUpdate(); return;}

    all rights reserved www.rhinosystemsinc.com

  • Issues Regarding Example 2Good Created the preparedStatement once at initialization time & then binding values in function.Loader routine purpose? still worse performance than a SQL batch processing.Does not check to see if account exists already.GOOD uses sequence number generation from oracle

    all rights reserved www.rhinosystemsinc.com

  • ADHOC: Java Client Example 3Query based on Prepared Statement{ // IN CONSTRUCTOR or INITIALIZATION BLOCK

    // THE SQL QUERY TO BE MADE.String sQueryNotify="select email_author,notification,subject,s.name as name " +" from project p, status s " +" where p.id=? " +" and s.id=p.status_id";

    // CREATE A PREPARED STATEMENT, BASED ON THE ABOVE SQLm_psLookupProjectID=conn.prepareStatement(sQueryNotify);}

    public EmailInfo queryEmailInfo(int id){ // BIND THE PARAMETER m_psLookupProjectID.setInt(1,id); // REUSE THE ALREADY CREATED PREPARED STATEMENT

    // GET THE RESULT SET AND PROCESS IT. ResultSet rs=m_psLookupProjectID.executeQuery(); return info;}

    all rights reserved www.rhinosystemsinc.com

  • ADHOC: PL/SQL Example 4PreparedStatement ps=null; sSQL=new String("declare" + " l_id number:=0;" + " ret number:=0;" + " lssno varchar2(32):=?;" + " error_msg varchar2(1026):=null;" + " BEGIN" + " select count(*) into ret from person where SSNO= lssno ;" + " if(ret=0) then" + " insert into person(id,ssno) values(seq_id.nextval, lssno) returning id into ret;" + " end if;" + " ?:=ret;" + " EXCEPTION WHEN OTHERS THEN" + " ?:=SQLERRM;" + " END;");

    ps =connection.prepareStatement(sbSQL.toString());int indx=1;

    ps.setString(indx++,SSNO);

    int rettype=Types.INTEGER;ps.registerOutParameter(indx++,rettype);

    rettype=Types.VARCHAR;ps.registerOutParameter(indx++,rettype);

    ps.executeUpdate();l_sError=ps.getString(2);if(l_sError!=null){System.err.println("PL/SQL Error: " + l_sError); return;}else{ ret=ps.getInt(1); System.out.println("New USERID: " + ret);}

    all rights reserved www.rhinosystemsinc.com

  • Analysis of Example 4Refer to labels in red from example.1) Notice that "DECLARE" begins our PL/SQL code. You don't need a DECLARE section, however, you can setup variables here by binding them to Java variables on your client side. Also notice the BEGIN statement, which is matched with the "END;" statement in #4.DECLAREBEGINEND;

    all rights reserved www.rhinosystemsinc.com

  • Analysis of Example 4Refer to labels in red from example.2) Test to see if the person exists before inserting into the table. (notice the RETURNING clause on the insert).3) If there are any exceptions that are thrown in the surrounding block, then execute this line of code assign the ERROR MESSAGE (SQLERRM) to the return message4) The "END" tag is used to close the BEGIN - Also notice the usage of ";" after the END tag also notice that we finished the string.

    all rights reserved www.rhinosystemsinc.com

  • Analysis of Example 4Refer to labels in red from example.5) Use an existing SQL connection to create the callable statement a prepared statement, used to bind input variables and output variables. Notice the index variable declared and assigned value of 1. 6) Bind the SSNO string to the first occurance of a "?" in the PLSQL. (what will happen if SSNO string's length is greater than 32?). Next few lines we register the output variables. (NOTICE how they match up with the ?). One is of type INTEGER the other VARCHAR.

    all rights reserved www.rhinosystemsinc.com

  • Analysis of Example 4Refer to labels in red from example.7) We executed the SQL statement, and now check for errors. If we find an error Message (ie not null), then we will report it, and return immediately. ELSE we'll continue and report the new userid (created from a sequence number in Oracle).

    all rights reserved www.rhinosystemsinc.com

  • Issues from Example 4Constructing SQL on the client side.Need to recompile in order to change sql Not very portable.Not modularTrade offs?Plain SQL insert and catch duplicate key exception need to make sure your Primary KEY or unique indices are setup properly.

    all rights reserved www.rhinosystemsinc.com

  • FUNCTION/PROCEDURE CALL Example 5Call a function, that processes some data and returns a result set.KEY THINGS TO NOTE:Minimize SQL code on client side.Ready for portability Within Oracle yesUsing other database perhapsSQL 92 syntax?

    all rights reserved www.rhinosystemsinc.com

  • Example 5 Syntax considerationJDBC SYNTAX For portability {?= call [,, ...]} {call [,, ...]} ORACLE SYNTAX (not portable) BEGIN ?:=[,, ...]; END;

    all rights reserved www.rhinosystemsinc.com

  • Example 5 PL/SQL server codeLoad the following into oracle once at install timeDeclare function in a package (not necessary but good to do). CREATE OR REPLACE PACKAGE api_person_pkg AS TYPE T_REFCURSOR IS REF CURSOR; -- Define a cursor reference. FUNCTION getPeople (VARCHAR2) RETURN T_REFCURSOR; END api_person_pkg; /

    all rights reserved www.rhinosystemsinc.com

  • Example 5 PL/SQL server codeCreate the body of the package/functionCREATE OR REPLACE PACKAGE BODY api_person_pkg AS FUNCTION function getPeople(p_filter varchar2:=null) RETURN T_REFCURSORIS v_ret_cursor API_PERSON_PKG.T_REFCURSOR;BEGIN if (p_filter is null) then OPEN v_ret_cursor FOR 'select * from person'; else OPEN v_ret_cursor FOR 'select * from person where username like ''%' || p_filter || '%'''; end if;

    RETURN v_ret_cursor;

    END; END api_person_pkg;/ Consider using global temporary table. Create the table at install time, then use the table to insert records into from different tables the records are only visible to current session. On commit records go away.

    all rights reserved www.rhinosystemsinc.com

  • Example 5 PLSQL Test code to test the PL/SQLSQL> set serveroutput on;SQL> declare v_ret_cursor API_PERSON_PKG.T_REFCURSOR; v_id number; --PERSON ID v_name varchar2(120); -- the usernamebegin v_ret_cursor:=API_PERSON_PKG.getPeople('jthomps'); LOOPFETCH v_ret_cursor into v_id ,v_name;EXIT WHEN v_ret_cursor%NOTFOUND; dbms_output.put_line('values retrieved =' || v_id || ',' || v_name); END LOOP;END;/

    all rights reserved www.rhinosystemsinc.com

  • Example 5 java client code

    Call a function to return a result set. try{String sql=new String("{ ? = call api_person_pkg.getPeople('joelt') }"); // could have used "BEGIN ?:= api_person_pkg.getPeople('joelt'); END;"

    final int cursorRefType=oracle.jdbc.driver.OracleTypes.CURSOR;

    cs=m_Conn.prepareCall(sql);cs.registerOutParameter(1,cursorRefType);cs.execute();rs=(ResultSet)cs.getObject(1);

    int ColNameIndex=1;ResultSetMetaData rsmd=rs.getMetaData();

    String ColName1=rsmd.getColumnName(ColNameIndex++);String ColName2=rsmd.getColumnName(ColNameIndex);System.out.println(ColName1 + "," + ColName2);while(rs.next()){l_sPersonID=rs.getString(1);l_sUsername=rs.getString(2);System.out.println(l_sPersonID + "," + l_sUsername);}}catch(SQLException sqle){ // ...System.out.println("Error: " + sqle.getMessage());}finally{rs.close();cs.close();try{ m_Conn.close(); } catch(Exception e){}}

    all rights reserved www.rhinosystemsinc.com

  • Example 6 objectiveJava to insert/update record with stored procedure, showing how to create primary key & foreign key records using Oracle's sequences.

    all rights reserved www.rhinosystemsinc.com

  • Example 6 create the STR_VARRAY typeYou need to create a type to hold the array. This type will be referenced in the PL/SQL procedure as a parameter type, and in the Java Code. create or replace TYPE str_varray AS VARRAY(10) OF VARCHAR2(5)/

    all rights reserved www.rhinosystemsinc.com

  • Example 6 stored procedureCreate the stored procedurePROCEDURE storePersonCar(p_username varchar2,p_Cars str_varray) AS cnt number:=0; id person.id%TYPE; indx integer; BEGIN select count(*) into cnt from person where username=p_username; BEGIN if ( cnt = 0 ) then insert into person(id,username) values(seq_person_id.nextval,p_username) returning id into id; else select id into id from person where username=p_username;delete from cars where person_id=id;end if; indx:=p_Cars.FIRST; while indx
  • Example 6 Java Codeimport java.sql.*;import oracle.sql.*; PreparedStatement ps =null; ArrayDescriptor desc = ArrayDescriptor.createDescriptor("JOEL.STR_VARRAY", m_Conn);

    String sCar=""; String cars[]=new String[10];

    for(int i=0;i

  • Example 6 Java NotesMake sure to include classes12.jar and nls_charset12.jar both found in $ORACLE_HOME/jdbc/lib directory.This example works with Oracle 9.2.0.1.0 on Windows XP.Java version 1.4.2_03

    all rights reserved www.rhinosystemsinc.com

  • Portability & Compatibility issuesSQL StandardsJDBC standardsOther issues (dates, functionsetc.)Portability from schema-to-schema abstracting calls to database with PL/SQL layer

    all rights reserved www.rhinosystemsinc.com

  • WRAP-UPQUESTIONS and AnswersMORE INFORMATION:Contact: [email protected] call: 530-888-6248 x205

    all rights reserved www.rhinosystemsinc.com