16.10.2015 Views

Getting Startedwith pureQuery

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6 – The Client Optimizer: <strong>pureQuery</strong> for Existing Applications 129<br />

dynamically, replacing SQL statements is not allowed by default, and you must set<br />

pdq.enableDynamicSQLReplacement to TRUE to allow it. For SQL statements that<br />

execute statically, if you cannot sufficiently control access to your <strong>pureQuery</strong>Xml, you can<br />

check your <strong>pureQuery</strong>Xml file before each bind to ensure that it does not contain<br />

replacement statements.<br />

6.10 SQL Literal substitution<br />

Sometimes applications execute SQL statements that contain literal values instead of<br />

parameter markers. For example, if an application searches the EMPLOYEE table for an<br />

employee with an employee number that the user supplies, the application might use string<br />

concatenation to create an SQL statement with a literal value, as shown in Listing 6.11. In<br />

this example, if the user supplied the employee number 000010, then the application<br />

would create the SQL statement shown in Listing 6.12.<br />

String employeeNumber = // value supplied by user<br />

String sql = "SELECT * FROM EMPLOYEE WHERE EMPNO = '" + employeeNumber +<br />

"'";<br />

PreparedStatement preparedStatement = connection.prepareStatement (sql);<br />

ResultSet resultSet = preparedStatement.executeQuery();<br />

Listing 6.11 - An example of code that creates and executes an SQL statement with a<br />

literal value<br />

SELECT * FROM EMPLOYEE WHERE EMPNO = '000010'<br />

Listing 6.12 - The SQL statement that would be created by the code in Listing 6.11 if<br />

the user provided the employee number "000010"<br />

Listing 6.13 shows an example that creates an SQL statement with a parameter, rather<br />

than one with a literal value.<br />

String employeeNumber = // value supplied by user<br />

String sql = "SELECT * FROM EMPLOYEE WHERE EMPNO = ?";<br />

PreparedStatement preparedStatement = connection.prepareStatement (sql);<br />

preparedStatement.setString (employeeNumber);<br />

ResultSet resultSet = preparedStatement.executeQuery();<br />

Listing 6.13 - An example of code that creates and executes an SQL statement with a<br />

parameter<br />

6.10.1 Advantages of parameterized SQL statements<br />

SQL statements that have parameters have many advantages over those that have literal<br />

values. For example, SQL statements that are constructed by concatenation are<br />

vulnerable to SQL injection. In addition, if the user supplies the literal values, then there<br />

can be an enormous number of very similar SQL statements. The large number of distinct<br />

statements can have negative performance impacts, such as by flooding the dynamic<br />

statement cache with statements that are executed only once, and thereby significantly

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!