28.10.2014 Views

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SHOW MORE
SHOW LESS

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

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

mssql_bind() − adds a parameter to a stored procedure<br />

Reviewing Code for <strong>SQL</strong> <strong>Injection</strong> • Chapter 3 107<br />

mssql_bind($stmt, '@param', $variable, <strong>SQL</strong>VARCHAR, false, false, 100);<br />

$result = mssql_execute($stmt);<br />

// odbc_prepare() − prepares a statement for execution<br />

$stmt = odbc_prepare($db, $sql);<br />

$result = odbc_execute($stmt);<br />

// odbc_exec() − prepare <strong>and</strong> execute a <strong>SQL</strong> statement<br />

$result = odbc_exec($db, $sql);<br />

Things are a little different in Java. Java makes available the java.sql package <strong>and</strong> the Java<br />

Database Connectivity ( JDBC) API for database connectivity; for details on supported<br />

vendors, see http://java.sun.com/products/jdbc/driverdesc.html. We will concentrate on just<br />

a few common database vendors for the purpose of clarity. The following list details the<br />

relevant functions for My<strong>SQL</strong>, Microsoft <strong>SQL</strong> Server, <strong>and</strong> Oracle databases:<br />

■■<br />

createStatement( ) Creates a statement object for sending <strong>SQL</strong> statements to the<br />

database<br />

■ ■ prepareStatement( ) Creates a precompiled <strong>SQL</strong> statement <strong>and</strong> stores it in an object<br />

■■<br />

■■<br />

■■<br />

■■<br />

■■<br />

executeQuery( ) Executes the given <strong>SQL</strong> statement<br />

executeUpdate( ) Executes the given <strong>SQL</strong> statement<br />

execute( ) Executes the given <strong>SQL</strong> statement<br />

addBatch( ) Adds the given <strong>SQL</strong> comm<strong>and</strong> to the current list of comm<strong>and</strong>s<br />

executeBatch( ) Submits a batch of comm<strong>and</strong>s to the database for execution<br />

The following lines of code demonstrate how you can use these functions in a<br />

Java application:<br />

// createStatement() − is used to create a statement object that is used for<br />

// sending sql statements to the specified database<br />

statement = connection.createStatement();<br />

// PreparedStatement – creates a precompiled <strong>SQL</strong> statement <strong>and</strong> stores it<br />

// in an object.<br />

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

// executeQuery() − sql query to retrieve values from the specified table.<br />

result = statement.executeQuery(sql);<br />

// executeUpdate () − Executes an <strong>SQL</strong> statement, which may be an<br />

// INSERT, UPDATE, or DELETE statement or a statement that returns nothing<br />

result = statement.executeUpdate(sql);<br />

// execute() − sql query to retrieve values from the specified table.<br />

result = statement.execute(sql);

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

Saved successfully!

Ooh no, something went wrong!