This tutorial shows you how to develop, tune, and debug the SQL and PL/SQL portions of an application with JDeveloper.
The tutorial will discuss the following:
Overview | ||
Prerequisites | ||
Browsing the Database | ||
Executing and Tuning SQL Statements | ||
Creating and Deploying a Java Stored Procedure | ||
Summary |
60 minutes
Move your mouse over this icon to show all screenshots.
You can also move your mouse over each individual icon to see only the screenshot
associated with it.
JDeveloper provides functionality to view and modify database objects, including stored procedures. In this lesson you create and modify both PL/SQL and Java stored procedures. You deploy Java stored procedures and call them from PL/SQL. You also use JDeveloper to execute and tune SQL statements and debug stored procedures.
You have existing PL/SQL and Java stored procedures
that you want you manage with JDeveloper. You also want to use JDeveloper to
tune SQL statements, create and deploy new Java stored procedures, and create
and debug PL/SQL stored procedures.
In order for this tutorial to work successfully, you will need to have performed the following:
1. |
Install Oracle JDeveloper 10g.
|
2. |
Install the Oracle Sample Schemas and create a connection to the HR or HR8 schema. See Installing the Sample Schemas and Establishing a Database Connection.
|
3 . |
For the PL/SQL debugging portion of this tutorial, the HR or HR8 user needs special debug privileges if the database is Oracle 9i Release 2 or later. To grant the necessary privileges, the DBA user can
issue the following SQL statement:
|
There are several ways that you can use JDeveloper to browse database objects. This topic discusses the following subtopics:
Browsing Database Objects Owned by Other Users | ||
Filtering Database Objects for Browsing | ||
Browsing Database Objects Owned by the Current User
This topic assumes that you have already defined a database connection to the HR or HR8 user, as described in Prerequisites. The example uses MyHRConn for the name of that connection, but if you have used a different name, just select the connection that you defined.
To browse database objects owned by the current user, perform the following steps:
1. |
Select View | Connections Navigator.
|
2. |
Expand the Database node to show all database connections.
Expand MyHRConn.
By default, only the database objects owned by the current user are displayed.
|
Browsing Database Objects Owned by Other Users
To browse database objects owned by other users, perform the following steps:
Filtering Database Objects for Browsing
You can filter out objects you don't need to browse. This
is especially useful for schemas which may contain thousands of objects. To
filter out classes of database objects, perform the following steps:
Note: There's another way to find what you are looking for in the Navigator. With the Navigator in focus, you can simply begin typing the name of the node you are looking for. JDeveloper will automatically navigate you to the first node it finds starting with the text you've typed in.
When working with many objects in the navigator, it
may be difficult to find the node you are looking for. JDeveloper provides filters
at every level of a database connection to limit the number of objects you need
to look through. You've already seen one example of a filter when you selected
which schemas you wanted to view.
To browse a table in the HR schema and its data, perform the following steps:
To create, edit, compile, and test a PL/SQL procedure, perform the following steps:
1. |
Create the PL/SQL procedure: Right-click on the Procedures node in the Connections Navigator and choose New PL/SQL Procedure. Enter emp_list as the Object Name. Click OK. JDeveloper creates a skeleton procedure.
|
|
2. |
Edit the PL/SQL procedure. Copy and paste the following code into the PL/SQL editor: PROCEDURE EMP_LIST(pMaxRows
NUMBER)
|
|
3. |
There are several ways to find syntax errors. To detect the syntax error
in your sample code, perform one of the following: |
|
Select View > Structure to display the Structure Pane, then expand the Errors node. You can then navigate to the detected error by double-clicking the error.
|
||
Place your cursor next to one of the parentheses in the WHILE statement. JDeveloper highlights the matching symbol for the parenthesis at the cursor. If you place your cursor next to the first parenthesis in the statement, you will notice that it is highlighted in red which indicates that it does not have a matching symbol.
|
||
Compile the PL/SQL subprogram by clicking the Save button in the toolbar. Compilation errors are shown in the log window. You can navigate to the line reported in the error by double-clicking on the error. Note that when an invalid PL/SQL subprogram is detected by JDeveloper, the status is indicated with a red X over the icon for the subprogram in the Connections Navigator:
|
||
4. |
Fix the syntax error: Add the missing ) at the end of the WHILE statement just after pMaxRows and before the LOOP keyword. Click the Save button in the toolbar. You should see a message in the status bar indicating a Successful compilation.
|
|
5. |
Run the PL/SQL procedure: Right-click the procedure and choose Run from the context menu.
|
|
6. |
This invokes the Run PL/SQL dialog. The Run PL/SQL dialog enables you to select the target procedure or function to run (useful for packages) and displays a list of parameters for the selected target. In the PL/SQL block text area is some generated code that JDeveloper uses to call the selected program. You can use this area to populate parameters to be passed to the program unit and to handle complex return types. In the PL/SQL Block replace PMAXROWS := NULL; with PMAXROWS := 5;
|
|
7. |
Click OK. You should see the results of the 5 rows returned in the Log window.
|
You can use JDeveloper to improve performance of SQL statements. This topic discusses the following subtopics:
Retrieving the Explain Plan for a SQL Statement | ||
Tuning a SQL Statement |
To execute a SQL statement, perform the following steps:
Retrieving the Explain Plan for a SQL Statement
To retrieve the explain plain for a SQL statement, perform the following steps:
1. |
Delete the SQL statement from the SQL Worksheet.
|
2. |
If the EMP_LIST source is no longer visible, double-click the EMP_LIST node to open the procedure in the code editor. Highlight the SELECT statement from the cursor declaration and copy it to the clipboard (Ctrl+C). SELECT l.state_province, l.country_id,
d.department_name, e.last_name, |
3. |
Paste (Ctrl+V) the SELECT statement into the SQL Worksheet. Click
the Execute Statement
button. You should see the results of the statement.
|
4. |
Click the Execute Explain Plan button. Note: If you do not have a PLAN_TABLE in the current schema, you will be prompted to create it. In this case, simply click OK to create the PLAN_TABLE. JDeveloper displays the results of the SQL explain plan. By default, the system most likely performs a full table scan on all the tables as shown in the Explain Plan Results section in the following illustration: Note: If your results are significantly different from those shown, it may be because the schema has not been analyzed. To analyze the schema, enter and execute the following in the SQL Worksheet, then try again: BEGIN
|
There are several techniques to improve performance of a SQL statement. One of these is to specify that the server should quickly retrieve and display the first set of rows. To add a FIRST_ROWS hint to a SQL statement, perform the following steps:
1. |
Edit the SQL statement to include the optimizer hint   FIRST_ROWS. In this case, you should see a visible difference in the explain plan by requesting the server to retrieve the first set of rows as quickly as possible. After inserting the FIRST_ROWS optimizer hint, the query should appear as follows: SELECT /*+ FIRST_ROWS */ l.state_province,
l.country_id,
|
2. |
Click the Execute Explain Plan button. You should now see a different explain plan that uses one or more indexes for data retrieval.
|
JDeveloper simplifies deployment of Java stored procedures and allows debugging of Java stored procedures. You will create a new Java class as the basis for the Java stored procedure, then deploy it to the database.
This topic presents the following subtopics:
Creating a New Java Class for the Stored Procedure | ||
Creating a New Deployment Profile | ||
Creating a New PL/SQL Wrapper Definition | ||
Deploying the Java Stored Procedure | ||
Testing the Java Stored Procedure |
Creating a New Application Workspace
To create a new application workspace in JDeveloper, perform the following steps:
1. |
In the Applications Navigator, right click on the Applications node and choose New Application Workspace from the context menu.
|
2. |
Enter DBApplication as the Application Name. Select No Template [All Technologies] as the Application Template. You can enter a package name of your choice or leave the default package name. Click OK.
|
Creating a New Java Class for the Stored Procedure
To create a new Java class, perform the following steps:
1. |
Select the Project node in the Applications Navigator. Choose File > New from the main menu.
|
2. |
In the New Gallery, select General from the Categories list on the left side, and Java Class from Items on the right. Click OK.
|
3. |
Enter JavaStoredProc as the name of the new Java class. Click OK.
|
4. |
Write the code for the Java stored procedure. You can write any Java code with a public static method for your Java stored procedure. In this case, you can copy and paste the following: public class JavaStoredProc Choose File > Save All from the main menu to save your work.
|
Creating a New Deployment Profile
To create a deployment profile for the Java stored procedure, perform the following steps:
1. |
Select the Project node in the Applications Navigator. Right-click and choose Make from the context menu.
|
2. |
Choose File > New from the main menu.
Select the General > Deployment Profiles category.
|
3. |
Enter MyJavaSPProfile.deploy
as the Deployment Profile Name and click OK.
|
4. |
Click OK to accept the default values in the Deployment Profile Properties window.
|
Creating a New PL/SQL Wrapper Definition
In order to execute the Java stored procedure from SQL or PL/SQL, a PL/SQL wrapper is required. To create a PL/SQL wrapper for the Java stored procedure, perform the following steps:
Deploying the Java Stored Procedure
To deploy the Java stored procedure, perform the following steps:
1. |
Select MyJavaSPProfile.deploy in the Applications Navigator. Right click and choose Deploy to > MyHRConn from the context menu.
|
2. |
Verify that the deployment completed successfully. Check for a success message in the Log window: |
Testing the Java Stored Procedure
To test the Java stored procedure deployment, perform the following steps:
1. |
Expand the Functions node in the Connections Navigator. Right click on the GETHELLOFROMJAVA node and choose Run from the context menu. (If you are unable to see the GETHELLOFROMJAVA node, then select View > Refresh from the JDeveloper menu.)
|
2. |
In the Run PL/SQL window, click OK.
|
3. |
Confirm that the output v_Return = Hello World Hello World Hello World appears in the Log window.
|
JDeveloper supports PL/SQL debugging with Oracle databases. Debugging Java stored procedures is available only with Oracle9i Release 2 and later.
This topic includes the following subtopics:
Debugging the PL/SQL Procedure | ||
Stepping through the Code | ||
Modifying the PL/SQL Procedure at Runtime |
Modifying the PL/SQL to Call the Java Stored Procedure
To modify the PL/SQL procedure to call the Java stored procedure, perform the following steps:
1. |
In the Connections Navigator, expand the MyHRConn
connection, the HR node, and the Procedures node. Double-click
EMP_LIST to open it in the code editor.
|
2. |
Look for the line of code in the EMP_LIST procedure: Just below this line, add the following: |
Debugging the PL/SQL Procedure
To debug the PL/SQL procedure, perform the following steps:
1. |
Set a breakpoint in the EMP_LIST procedure by clicking in the margin at the line with the OPEN emp_cursor; statement
|
2. |
Right-click on the EMP_LIST node in the Connections Navigator and choose Debug. Note: If you receive the error "ORA-30683: failure establishing connection to debugger", and if you are using a Virtual Private Network (VPN) to connect to the database, you must set JDeveloper to prompt for the host IP address. Choose Tools > Preferences, select the Debugger node, and select the check box labeled Prompt for Debugger Host for Database Debugging. Then when you run the debugger, enter the VPN IP address when prompted.
|
3. |
In the Debug PL/SQL dialog, click OK. The debugger should halt
at the line where you placed the breakpoint. You can now control the flow
of execution, modify values of variables and perform other debugging functions. Note: If at this point you receive the error message "This session requires DEBUG CONNECT SESSION and DEBUG ANY PROCEDURE user privileges", you need to complete Step 3 of Prerequisites.
|
To step through the code of the PL/SQL procedure, perform the following steps:
Modifying the PL/SQL Procedure at Runtime
To modify the EMP_LIST procedure at runtime, as you would if you found an error while debugging, perform the following steps:
1. |
Right-click the line that reads DBMS_OUTPUT.PUT_LINE(emp_tab(j).last_name); and choose Run to Cursor from the context menu.
|
2. |
Use the data window to drill into the PL/SQL table of records called emp_tab. In the data window you can access the entire structure of composite data types. Keep drilling down until you see the values of the fields in a given record of the table. |
3. |
Right click on the LAST_NAME field of the record and choose Modify Value from the context menu. Modify the last name to a name of your choice. Click OK. You've now modified the value in the PL/SQL table of records.
|
4. |
Click Resume to allow the PL/SQL to run to completion. Check to see that your modified value is displayed in the Log window.
|
In this lesson you used JDeveloper10g to accomplish the following:
Browsing database objects and table data |
|
Defining a filter for browsing database objects |
|
Creating and compiling a PL/SQL procedure |
|
Executing and tuning a SQL statement |
|
Creating and deploying a Java stored procedure |
|
Debugging a PL/SQL procedure and a Java stored procedure |
For more information, refer to the JDeveloper
product page on OTN.