A PreparedStatement represents a single SQL statement pre-compiled into byte code for later execution.
The SQL statement may contain in parameters of the form ?
. Such parameters represent unspecified literal values (or "wildcards") to be filled in later by the various setter methods defined in this interface. Each in parameter has an associated index number which is its sequence in the statement. The first in '?' parameter has index 1, the next has index 2 and so on. A PreparedStatement is created by calling Connection_prepareStatement().
Consider this statement:
INSERT INTO employee(name, photo) VALUES(?, ?)
There are two in parameters in this statement, the parameter for setting the name has index 1 and the one for the photo has index 2. To set the values for the in parameters we use a setter method. Assuming name has a string value we use PreparedStatement_setString(). To set the value of the photo we submit a binary value using the method PreparedStatement_setBlob().
Example
To summarize, here is the code in context.
PreparedStatement_T Connection_prepareStatement(T C, const char *sql,...)
Prepares a SQL statement for execution.
void PreparedStatement_setString(T P, int parameterIndex, const char *x)
Sets the in parameter at index parameterIndex to the given string value.
void PreparedStatement_execute(T P)
Executes the prepared SQL statement.
void PreparedStatement_setBlob(T P, int parameterIndex, const void *x, int size)
Sets the in parameter at index parameterIndex to the given blob value.
Reuse
A PreparedStatement can be reused. That is, the method PreparedStatement_execute() can be called one or more times to execute the same statement. Clients can also set new in parameter values and re-execute the statement as shown in this example:
for (int i = 0; employees[i]; i++)
{
}
Result Sets
Here is another example where we use a Prepared Statement to execute a query which returns a Result Set:
ResultSet_T PreparedStatement_executeQuery(T P)
Executes the prepared SQL query.
bool ResultSet_next(T R)
Moves the cursor to the next row.
int ResultSet_getInt(T R, int columnIndex)
Gets the designated column's value as an int.
A ResultSet returned from PreparedStatement_executeQuery() is valid until the Prepared Statement is executed again or until the Connection is returned to the Connection Pool.
Date and Time
PreparedStatement_setTimestamp() can be used to set a Unix timestamp value as a time_t
type. To set Date, Time or DateTime values, simply use PreparedStatement_setString() to set a time string in a format understood by your database. For instance to set a SQL Date value,,
SQL Injection Prevention
Prepared Statement is particularly useful when dealing with user-submitted data, as properly used Prepared Statements provide strong protection against SQL injection attacks. By separating SQL logic from data, PreparedStatements ensure that user input is treated as data only, not as part of the SQL command.
A PreparedStatement is reentrant, but not thread-safe and should only be used by one thread (at a time).
- Note
- Remember that parameter indices in PreparedStatement are 1-based, not 0-based.
-
To minimizes memory allocation and avoid unnecessary data copying, string and blob values are set by reference and MUST remain valid until either PreparedStatement_execute() or PreparedStatement_executeQuery() is called.
- See also
- Connection.h ResultSet.h SQLException.h
|
|
void | PreparedStatement_setString (T P, int parameterIndex, const char *x) |
| Sets the in parameter at index parameterIndex to the given string value.
|
|
void | PreparedStatement_setSString (T P, int parameterIndex, const char *x, int size) |
| Sets the in parameter at index parameterIndex to the given sized string value.
|
|
void | PreparedStatement_setInt (T P, int parameterIndex, int x) |
| Sets the in parameter at index parameterIndex to the given int value.
|
|
void | PreparedStatement_setLLong (T P, int parameterIndex, long long x) |
| Sets the in parameter at index parameterIndex to the given long long value.
|
|
void | PreparedStatement_setDouble (T P, int parameterIndex, double x) |
| Sets the in parameter at index parameterIndex to the given double value.
|
|
void | PreparedStatement_setBlob (T P, int parameterIndex, const void *x, int size) |
| Sets the in parameter at index parameterIndex to the given blob value.
|
|
void | PreparedStatement_setTimestamp (T P, int parameterIndex, time_t x) |
| Sets the in parameter at index parameterIndex to the given Unix timestamp value.
|
|
void | PreparedStatement_setNull (T P, int parameterIndex) |
| Sets the in parameter at index parameterIndex to SQL NULL.
|
|
|
void | PreparedStatement_execute (T P) |
| Executes the prepared SQL statement.
|
|
ResultSet_T | PreparedStatement_executeQuery (T P) |
| Executes the prepared SQL query.
|
|
long long | PreparedStatement_rowsChanged (T P) |
| Gets the number of rows affected by the most recent SQL statement.
|
|
|
int | PreparedStatement_getParameterCount (T P) |
| Gets the number of parameters in the prepared statement.
|
|