You are on page 1of 17

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

Written by

Steven Feuerstein, PL/SQL Evangelist, Quest Software, Inc. Daniel Norwood, Senior Product Manager, Quest Software, Inc.

TECHNICAL BRIEF

2011 Quest Software, Inc. ALL RIGHTS RESERVED. This document contains proprietary information protected by copyright. No part of this document may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying and recording for any purpose without the written permission of Quest Software, Inc. (Quest). The information in this document is provided in connection with Quest products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Quest products. EXCEPT AS SET FORTH IN QUEST'S TERMS AND CONDITIONS AS SPECIFIED IN THE LICENSE AGREEMENT FOR THIS PRODUCT, QUEST ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL QUEST BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, BUSINESS INTERRUPTION OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF QUEST HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Quest makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Quest does not make any commitment to update the information contained in this document. If you have any questions regarding your potential use of this material, contact: Quest Software World Headquarters LEGAL Dept 5 Polaris Way Aliso Viejo, CA 92656 www.quest.com email: legal@quest.com Refer to our Web site for regional and international office information.

Trademarks
Quest, Quest Software, the Quest Software logo, AccessManager, ActiveRoles, Aelita, Akonix, AppAssure, Benchmark Factory, Big Brother, BridgeAccess, BridgeAutoEscalate, BridgeSearch, BridgeTrak, BusinessInsight, ChangeAuditor, ChangeManager, Defender, DeployDirector, Desktop Authority, DirectoryAnalyzer, DirectoryTroubleshooter, DS Analyzer, DS Expert, Foglight, GPOADmin, Help Desk Authority, Imceda, IntelliProfile, InTrust, Invirtus, iToken, I/Watch, JClass, Jint, JProbe, LeccoTech, LiteSpeed, LiveReorg, LogADmin, MessageStats, Monosphere, MultSess, NBSpool, NetBase, NetControl, Npulse, NetPro, PassGo, PerformaSure, Point,Click,Done!, PowerGUI, Quest Central, Quest vToolkit, Quest vWorkSpace, ReportADmin, RestoreADmin, ScriptLogic, Security Lifecycle Map, SelfServiceADmin, SharePlex, Sitraka, SmartAlarm, Spotlight, SQL Navigator, SQL Watch, SQLab, Stat, StealthCollect, Storage Horizon, Tag and Follow, Toad, T.O.A.D., Toad World, vAutomator, vControl, vConverter, vFoglight, vOptimizer, vRanger, Vintela, Virtual DBA, VizionCore, Vizioncore vAutomation Suite, Vizioncore vBackup, Vizioncore vEssentials, Vizioncore vMigrator, Vizioncore vReplicator, WebDefender, Webthority, Xaffire, and XRT are trademarks and registered trademarks of Quest Software, Inc in the United States of America and other countries. Other trademarks and registered trademarks used in this guide are property of their respective owners.

Updated[January, 2011]

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

Contents
Contents .........................................................................................................................................................................1 Introduction ....................................................................................................................................................................3 Choosing the Right Tool for the Job ............................................................................................................................... 4 The Role Oracle PL/SQL Plays in Application Development .........................................................................................5 Tight Integration with SQL ..........................................................................................................................................5 High Performance.......................................................................................................................................................5 Application Management ............................................................................................................................................5 Highly Secure Applications .........................................................................................................................................5 Integrated Application Development Tool Key ...............................................................................................................6 Using Toad from Within Eclipse...................................................................................................................................7 Toad Extension for Eclipse ......................................................................................................................................7 Installing Toad Extension for Eclipse ..........................................................................................................................7 Connecting Your Oracle Database ............................................................................................................................. 8 Opening a Stored Procedure .................................................................................................................................... 11 Conclusion ................................................................................................................................................................... 14 Resources ................................................................................................................................................................ 14 About the Authors ........................................................................................................................................................ 15

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

Introduction
Java is one of the most popular general-purpose programming languages in the world. You can run Java on virtually every operating system and computer. You can even run Java code from within the Oracle database. Oracle PL/SQL is a proprietary database programming language, used primarily within the Oracle database context, (though it can now be used with some versions of IBM DB2). It has a relatively narrowly defined purpose and does not support nearly the breadth and depth of features that Java does. The PL/SQL developer community is relatively small and isolated from the rest of the programming world. As Quest Software's PL/SQL evangelist, I recommend to all PL/SQL developers that they learn the basics of Java. Even if you are not going to become a full-time Java programmer, you should at least understand how you can "escape" to Java from PL/SQL to implement a user requirement that cannot be handled within the constraints of the PL/SQL language. In this paper, I argue the converse: that every Java developer (well, at least those who write applications that run against an Oracle database) should also learn the rudiments of Oracle PL/SQL. This recommendation will come as a surprise to (and, I expect, be resisted by) many Java developers. Why learn about Oracle PL/SQL when Java can do everything we need?

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

Choosing the Right Tool for the Job


Technology, generally, is not an end, in and of itself. Technology is, rather, a means to an end, which is to meet the demands and requirements of our user community. Every element of that technology has its own strengths and weaknesses. The best way to solve the problems faced by our users is to blend together the various technologies in such a way as to maximize the strengths of all the tools available to us. It is certainly possible to do anything and everything you need to do in Java, but that doesn't mean that a 100 percent Java solution is the optimal solution. An optimal solution is one that offers a high quality user experience, both from the standpoint of functionality and performance, and also is built on a body of code that is easy to understand and maintain. The Oracle PL/SQL language was designed for, and is highly optimized to handle, interaction with the underlying Oracle database tables. By making careful choices about when to utilize PL/SQL code to implement database-centric activity, you can achieve a higher quality application with better performance. You can also increase overall application development productivity. Beyond this common sense perspective on choosing the right tool for the job, there is a growing acceptance in the software development industry that a broader expertise is of much greater benefit than a narrow concentration of expertise. In a November 2010 Gartner research note, Eric Knipp argues that, "Hiring trends will shift away from focusing on a narrow range of expertise in a particular language or platform in favor of 'polyglot programmers who possess skills across a range of development tools." Knipp also notes that, "IT leaders must prepare to establish 'gearing' between front-end and back-end projects, as front-end projects with high customer exposure will be the first to benefit from the capabilities offered by new languages running under a Java Virtual Machine (JVM) while the back ends must evolve more slowly to maintain adequate service levels." 1 A substantial majority of those back ends that run Oracle either currently use PL/SQL or could benefit from its use. Java developers who can "put on a PL/SQL hat" will be of greater value to employers.

Eric Knipp, Jim Duggan, Ross Altman, "Predicts 2011: Anticipate Application Development Challenges Arising From Cost Optimization, Agile Methodologies and Multichannel Customer Engagement," Gartner (2010): 6-7.

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

The Role Oracle PL/SQL Plays in Application Development


The importance of PL/SQL doesnt only come from its dominant position in hundreds of thousands of existing and legacy applications; the Oracle PL/SQL language was architected to offer several key advantages.

Tight Integration with SQL


The SQL language is the dominant language for manipulating relational databases, which remain the most powerful and popular database technology in use today. While you can certainly write and execute SQL statements from within Java, you need to use JDBC to do so. The syntax for manipulating SQL statements in JDBC is much less intuitive and harder to maintain than the ability in PL/SQL to write native SQL statements. In addition, PL/SQL fully supports SQL data types, so there is no need to convert between Java types and SQL types.

High Performance
One of the most important advantages of PL/SQL is that you can execute a single block that contains multiple SQL statements. By combining all statements into a single block, you can drastically reduce network traffic between the database and an application. In addition, PL/SQL offers unique language features to optimize the execution of SQL statements. FORALL and BULK COLLECT can improve SQL performance by an order of magnitude over SQL inside a loop. Pipelined table functions can be used to reduce the user perception of elapsed time.

Application Management
When you compile a stored program unit into the database, Oracle maintains information about dependencies between database objects. As those objects change, Oracle automatically invalidates and forces recompilation of dependent objects. In addition, Oracle's shared memory facilities, in which PL/SQL code participates, improve the scalability of applications that are built on PL/SQL.

Highly Secure Applications


Many Oracle-based applications utilize PL/SQL to establish a data access layer that tightly controls access to sensitive data by client applications. Database triggers, implemented in PL/SQL, apply critical business rules and perform audit operations in a way that cannot be bypassed by any application code or code injection. In addition, the Virtual Private Database feature of Oracle, implemented through PL/SQL packages, applies security policies that transparently control the rows of data that a user can see. The bottom line is that Oracle PL/SQL offers a unique set of features and benefits that are hard to match through any other programming language. Deploying an Oracle-based application that ignores the advantages of Oracle PL/SQL means that you will write more code and that code will run more slowly than is necessary.

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

Integrated Application Development Tool Key


So let's suppose I have convinced you. You are ready, willing, and able to use both Java and PL/SQL as you build your application. Learning the PL/SQL language will come easily to a Java developer; the procedural and relational models are simpler than those of an object-oriented language. The bigger challenge, when it comes to adding new technologies to your repertoire, has to do with the tools you have available to you to work with that technology. For a programmer, the looming question is this: What editor can I use to work with a new language? There is really just one right answer: You should be able to use the editor you have already been using with your existing languages to write code in the new language. If you have to move between tools, you will become frustrated by the differences in UIs, and your productivity will undoubtedly suffer. The ideal solution is that you can program in multiple languages from the same IDE. Is this possible when extending your skill set to include Oracle PL/SQL? Consider the Java environment. There are certainly many different Java IDEs available, but one stands out as the dominant tool: Eclipse. Then consider the PL/SQL environment. There are also a number of different editors for PL/SQL, but one tool is the overwhelming favorite: Toad from Quest Software. Wouldn't it be nice if from within Eclipse you could view, edit, and compile PL/SQL source code with all the power and productivity you'd find in Toad? The plug-in nature of Eclipse has, in fact, made this a reality.

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

Using Toad from Within Eclipse


Toad Extension for Eclipse
Please extend a warm welcome to the newest member of the Toad family: Toad Extension for Eclipse! In September 2010, Quest announced the availability of Toad Extension for Eclipse in beta form at the Oracle Open World conference in San Francisco. Since then, both the tool and its community of users have grown significantly! It will be released as a freeware product early in 2011 and will be available at www.ToadExtensions.com. Now lets take a look at the workflow of a Java developer, working with PL/SQL in Eclipse.

Installing Toad Extension for Eclipse


First, make sure you have Eclipse version 3.5 (Galileo) or 3.6 (Helios) installed. Second, use the update mechanism in Eclipse to install Toad Extension for Eclipse (Help | Install New Software). The link to use is http://toaddownload.quest.com/toadextensions/eclipse/freeware/. Once you finish the installation, be sure to open the Toad Extension for Eclipse perspective (Window | Open Perspective | Other).

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

Figure 1: Install New Software screen. (For those of you in corporate environments where this isnt possible, you can download a ZIP file from www.ToadExtensions.com.) At this point, Toad Extension for Eclipse will work like any other Eclipse perspective. As youre working with your Java code, simply switch perspectives to begin working on your PL/SQL code within Eclipse.

Connecting Your Oracle Database


Connecting to your Oracle database will be the first order of business. Toad Extension for Eclipse uses JDBC drivers, so youll need to make sure you get the right one. (Check the www.ToadExtensions.com community for details.) If Toad Extension for Eclipse cant find the driver, it will open a dialog asking you to specify a location (Figure 2). In some cases, Eclipse may restart at this point, so be sure to save your work if this is the first time youre using Toad Extension for Eclipse.

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

Figure 2: Specify the location of your driver. Now lets create your first connection. Click on the New Connection button in the Connections pane (Figure 3).

Figure 3: New Connection button. This will open a new dialog where you can enter the server information and your credentials. If you have an Oracle client configured on your machine, I would recommend using that method to connect (Figure 4). This will give you quick access to an Oracle database server. Otherwise, you can try a direct connection over TCP/IP (Figure 5). Either way, save your connection information and right click on it to connect.

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

Figure 4: Connection screen (Oracle client).

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

10

Figure 5: Connection screen (Direct TCP/IP connections). Were in! Now lets get to work.

Opening a Stored Procedure


Ill begin by opening a stored procedure. Using the Schema Browser (Figure 6), navigate your schema to find your procedure. In my example, you can see by the icon that procedure TEST2 has compile errors. Lets open that one to see whats wrong.

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

11

Figure 6: Opening a stored procedure.

As you can see, theres a red X in the left margin as well as a red block in the scroll bar on the right, both indicating the location of our error. However, Toad Extension for Eclipse doesnt stop there! Note that our code is underlined at the location of our error, showing us exactly where the error is. Furthermore, when you try clicking on the red X on the left, youll be presented with more detailed information about the error (Figure 7). Pretty helpful info Toad Extension for Eclipse to the rescue! (OK, OK. This is obviously a very simple example, but I think it works for our purposes.)

Figure 7: Detailed error information. So lets fix our error and recompile to procedure. To do this, use the Edit Procedure button at the top of the window (Figure 8). This opens the procedure in a new window where we can make some edits.

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

12

Figure 8: Lets edit the procedure.

With our changes in place, we can use the green play button to store the procedure in the database and recompile it (Figure 9). Notice that our error is now resolved and our code compiles cleanly!

Figure 9: Save the code and recompile it into the database. At this point, you may want to do some deeper validation of your code by debugging or creating/running some unit tests. These are features that we hope to add in short order! But for now, you can switch perspectives again to pick up where you left off with your Java code or maybe hang here a while longer and run some queries to validate some test data either way, youll be able to maintain context within your favorite IDE, Eclipse!

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

13

Conclusion
With Toad, you can leverage existing skill sets while growing to meet tomorrows demands. You will continue to have a simple, consistent way to build, access, manage, and maintain database applications, no matter which platforms you are using. Toad will grow with you as you investigate and incorporate new technologies in the world of data ensuring freedom of choice today and into the future.

Resources
Toad World is Quest Softwares free online community for anyone working with databases. With more than one million visitors in 2010, it has proven to be your one stop resource for education, expertise, and collaboration. Visit Toad World at www.toadworld.com.

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

14

About the Authors


Steven Feuerstein is one of the world's leading experts on the Oracle PL/SQL language, having written 10 books on PL/SQL, including "Oracle PL/SQL Programming" and "Oracle PL/SQL Best Practices" (all from O'Reilly Media). Steven has been developing software since 1980 and spent five years with Oracle (1987-1992). Today, he serves as the PL/SQL evangelist for Quest Software. He has spent the past few years working on his latest creation, Quest Code Tester for Oracle, a tool that is revolutionizing the testing of PL/SQL programs. Steven can be contacted at steven.feuerstein@quest.com and through his popular Toad World portal, PL/SQL Obsession. Daniel Norwood is a senior product manager for Quest Software and is responsible for the application development product line. Daniel has more than ten years of experience in the software industry, specializing in RDBMS and related technologies. In addition to product management, Daniel has worked in support, testing, and project management. He spends much of his time interacting with customers around the globe, ensuring that Quests tools exceed expectations.

Why Every Java Developer Should Also Know and Use Oracle PL/SQL

15

TECHNICAL BRIEF

About Quest Software, Inc.


Quest Software (Nasdaq: QSFT) simplifies and reduces the cost of managing IT for more than 100,000 customers worldwide. Our innovative solutions make solving the toughest IT management problems easier, enabling customers to save time and money across physical, virtual and cloud environments. For more information about Quest solutions for application management, database management, Windows management, virtualization management, and IT management, go to www.quest.com.

Contacting Quest Software


PHONE

800.306.9329 (United States and Canada) If you are located outside North America, you can find your local office information on our Web site.

E-MAIL MAIL

sales@quest.com Quest Software, Inc. World Headquarters 5 Polaris Way Aliso Viejo, CA 92656 USA

Contacting Quest Support


Quest Support is available to customers who have a trial version of a Quest product or who have purchased a commercial version and have a valid maintenance contract. Quest Support provides around-the-clock coverage with SupportLink, our Web self-service. Visit SupportLink at https://support.quest.com. SupportLink gives users of Quest Software products the ability to: Search Quests online Knowledgebase Download the latest releases, documentation, and patches for Quest products Log support cases Manage existing support cases View the Global Support Guide for a detailed explanation of support programs, online services, contact information, and policies and procedures.

5 Polaris Way, Aliso Viejo, CA 92656 | PHONE 800.306.9329 | WEB www.quest.com | E-MAIL sales@quest.com

If you are located outside North America, you can find local office information on our Web site.
2011 Quest Software, Inc. ALL RIGHTS RESERVED. Quest, Quest Software, the Quest Software logo are registered trademarks of Quest Software, Inc. in the U.S.A. and/or other countries. All other trademarks and registered trademarks are property of their respective owners. TBD_WhyEveryJavaDeveloper_US_VG_20110126

You might also like