Free Programming Books
Free download ebooks on computer and programming | |||
Free Oracle ebook "Expert Oracle JDBC Programming" Sample ChapterExpert Oracle JDBC Programming Download chapterFree download Chapter 5: Statement and PreparedStatement JDBC is the most commonly used API in Java to access and manipulate data in a database. Oracle is one of the most popular and scalable databases in the world. This book is a must-have for any developer building an application that employs JDBC on Oracle database. Unlike other JDBC books, this book has been written to complement not rehash the contents of Oracle JDBC documentation and the JDBC specification. The book teaches you not just how to write JDBC code, but how to write effective JDBC code in a step-by-step fashion. This book does not assume any prior knowledge of JDBC though it does assume basic knowledge of SQL and PL/SQL. It covers JDBC with a focus on writing high-performing, scalable and secure applications for Oracle 10g and 9i. Using this book you will learn, among other things:
Statement and PreparedStatementIn this chapter, you'll briefly look at how Oracle processes SQL statements and then start your journey into the world of statements in JDBC. JDBC statements provide a mechanism for creating and executing SQL statements to query and modify the data stored in a database. As a quick introduction, JDBC offers the following flavors of statement interfaces:
You can browse the javadoc API for these and other JDBC classes and interfaces at http://java.sun.com. In this chapter, we'll focus on the Statement and PreparedStatement interfaces and their Oracle extensions. We'll cover CallableStatement and its Oracle extensions in the next chapter. By the end of this chapter, I hope to convince you that, in production code, you should always use PreparedStatement (or CallableStatement) objects instead of Statement objects. In fact, in the next chapter, I make a strong case for almost exclusively using CallableStatement in production code. Before starting the discussion of the Statement objects, let's take a quick look at how Oracle processes SQL statements submitted by a client (through SQL*Plus, a JDBC application, etc.). This information will be useful in helping us arrive at certain performance-related conclusions later in this chapter. Overview of How Oracle Processes SQL Statements (DML)For this discussion, we only consider Data Manipulation Language (DML) statements. In particular, we exclude Data Definition Language (DDL) statements, as these are typically (and should be) done at install time and are not part of the application code. DML statements are the statements that you will encounter most often, as you use them to query or manipulate data in existing schema objects. They include select, insert, update, delete, and merge statements. Oracle goes through the following stages to process a DML statement:
Step 2, generating the execution plan, can be very CPU-intensive. To skip this step in most cases, Oracle saves the results of the execution plan in a shared memory structure called the shared pool (see the section "Shared Pool and Bind Variables" of Chapter 2). When you submit a statement to Oracle, as part of the first step of parsing, it checks against the shared pool to see if the same statement was submitted by your session or some other, earlier session. If Oracle does not find the statement in the shared pool, it has to go through all three steps. This phenomenon is called a hard parse. On the other hand, if Oracle gets a hit in its shared pool cache, then it can skip the second step of generating the execution plan and directly go to the execution step. This phenomenon is called a soft parse. | |||