Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Advantages of a <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html" rel="noreferrer"><code>PreparedStatement</code></a>:</p> <ul> <li><p>Precompilation and DB-side caching of the SQL statement leads to overall faster execution and the ability to reuse the same SQL statement in <a href="https://stackoverflow.com/questions/2467125/reusing-a-preparedstatement-multiple-times">batches</a>.</p></li> <li><p>Automatic prevention of <a href="http://en.wikipedia.org/wiki/SQL_injection" rel="noreferrer">SQL injection</a> <a href="http://unixwiz.net/techtips/sql-injection.html" rel="noreferrer">attacks</a> by builtin escaping of quotes and other special characters. Note that this requires that you use any of the <code>PreparedStatement</code> <code>setXxx()</code> methods to set the values </p> <pre><code>preparedStatement = connection.prepareStatement("INSERT INTO Person (name, email, birthdate, photo) VALUES (?, ?, ?, ?)"); preparedStatement.setString(1, person.getName()); preparedStatement.setString(2, person.getEmail()); preparedStatement.setTimestamp(3, new Timestamp(person.getBirthdate().getTime())); preparedStatement.setBinaryStream(4, person.getPhoto()); preparedStatement.executeUpdate(); </code></pre> <p>and thus <strong>don't</strong> inline the values in the SQL string by string-concatenating.</p> <pre><code>preparedStatement = connection.prepareStatement("INSERT INTO Person (name, email) VALUES ('" + person.getName() + "', '" + person.getEmail() + "'"); preparedStatement.executeUpdate(); </code></pre></li> <li><p>Eases setting of non-standard Java objects in a SQL string, e.g. <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setDate%28int,%20java.sql.Date%29" rel="noreferrer"><code>Date</code></a>, <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setTime%28int,%20java.sql.Time%29" rel="noreferrer"><code>Time</code></a>, <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setTimestamp%28int,%20java.sql.Timestamp%29" rel="noreferrer"><code>Timestamp</code></a>, <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setBigDecimal%28int,%20java.math.BigDecimal%29" rel="noreferrer"><code>BigDecimal</code></a>, <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setBinaryStream%28int,%20java.io.InputStream%29" rel="noreferrer"><code>InputStream</code></a> (<a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setBlob%28int,%20java.sql.Blob%29" rel="noreferrer"><code>Blob</code></a>) and <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setCharacterStream%28int,%20java.io.Reader%29" rel="noreferrer"><code>Reader</code></a> (<a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setClob%28int,%20java.sql.Clob%29" rel="noreferrer"><code>Clob</code></a>). On most of those types you can't "just" do a <code>toString()</code> as you would do in a simple <code>Statement</code>. You could even refactor it all to using <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setObject%28int,%20java.lang.Object%29" rel="noreferrer"><code>PreparedStatement#setObject()</code></a> inside a loop as demonstrated in the utility method below:</p> <pre><code>public static void setValues(PreparedStatement preparedStatement, Object... values) throws SQLException { for (int i = 0; i &lt; values.length; i++) { preparedStatement.setObject(i + 1, values[i]); } } </code></pre> <p>Which can be used as below:</p> <pre><code>preparedStatement = connection.prepareStatement("INSERT INTO Person (name, email, birthdate, photo) VALUES (?, ?, ?, ?)"); setValues(preparedStatement, person.getName(), person.getEmail(), new Timestamp(person.getBirthdate().getTime()), person.getPhoto()); preparedStatement.executeUpdate(); </code></pre></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload