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 the Connection_prepareStatement() method.
Consider this statement:
INSERT INTO employee(name, picture) VALUES(?, ?)There are two in parameters in this statement, the parameter for setting the name has index 1 and the one for the picture 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 picture we submit a binary value using the method PreparedStatement_setBlob().
Note that string and blob parameter values are set by reference and must not "disappear" before either PreparedStatement_execute() or PreparedStatement_executeQuery() is called.
To summarize, here is the code in context.
PreparedStatement_T p = Connection_prepareStatement(con, "INSERT INTO employee(name, picture) VALUES(?, ?)"); PreparedStatement_setString(p, 1, "Kamiya Kaoru"); PreparedStatement_setBlob(p, 2, jpeg, jpeg_size); PreparedStatement_execute(p);
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:
PreparedStatement_T p = Connection_prepareStatement(con, "INSERT INTO employee(name, picture) VALUES(?, ?)"); for (int i = 0; employees[i].name; i++) { PreparedStatement_setString(p, 1, employees[i].name); PreparedStatement_setBlob(p, 2, employees[i].picture, employees[i].picture_size); PreparedStatement_execute(p); }
Here is another example where we use a Prepared Statement to execute a query which returns a Result Set:
PreparedStatement_T p = Connection_prepareStatement(con, "SELECT id FROM employee WHERE name LIKE ?"); PreparedStatement_setString(p, 1, "%Kaoru%"); ResultSet_T r = PreparedStatement_executeQuery(p); while (ResultSet_next(r)) printf("employee.id = %d\n", ResultSet_getInt(r, 1));
A ResultSet returned from PreparedStatement_executeQuery() "lives" until the Prepared Statement is executed again or until the Connection is returned to the Connection Pool.
Defines | |
#define | T PreparedStatement_T |
Typedefs | |
typedef struct T * | T |
Functions | |
void | PreparedStatement_setString (T P, int parameterIndex, const char *x) |
Sets the in parameter at index parameterIndex to the given 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 int 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_execute (T P) |
Executes the prepared SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement. | |
ResultSet_T | PreparedStatement_executeQuery (T P) |
Executes the prepared SQL statement, which returns a single ResultSet object. |
#define T PreparedStatement_T |
void PreparedStatement_setString | ( | T | P, | |
int | parameterIndex, | |||
const char * | x | |||
) |
Sets the in parameter at index parameterIndex
to the given string value.
P | A PreparedStatement object | |
parameterIndex | The first parameter is 1, the second is 2,.. | |
x | The string value to set. Must be a NUL terminated string. NULL is allowed to indicate a SQL NULL value. |
SQLException | if a database access error occurs or if parameter index is out of range |
void PreparedStatement_setInt | ( | T | P, | |
int | parameterIndex, | |||
int | x | |||
) |
Sets the in parameter at index parameterIndex
to the given int value.
P | A PreparedStatement object | |
parameterIndex | The first parameter is 1, the second is 2,.. | |
x | The int value to set |
SQLException | if a database access error occurs or if parameter index is out of range |
void PreparedStatement_setLLong | ( | T | P, | |
int | parameterIndex, | |||
long long int | x | |||
) |
Sets the in parameter at index parameterIndex
to the given long long value.
P | A PreparedStatement object | |
parameterIndex | The first parameter is 1, the second is 2,.. | |
x | The long long value to set |
SQLException | if a database access error occurs or if parameter index is out of range |
void PreparedStatement_setDouble | ( | T | P, | |
int | parameterIndex, | |||
double | x | |||
) |
Sets the in parameter at index parameterIndex
to the given double value.
P | A PreparedStatement object | |
parameterIndex | The first parameter is 1, the second is 2,.. | |
x | The double value to set |
SQLException | if a database access error occurs or if parameter index is out of range |
void PreparedStatement_setBlob | ( | T | P, | |
int | parameterIndex, | |||
const void * | x, | |||
int | size | |||
) |
Sets the in parameter at index parameterIndex
to the given blob value.
P | A PreparedStatement object | |
parameterIndex | The first parameter is 1, the second is 2,.. | |
x | The blob value to set | |
size | The number of bytes in the blob |
SQLException | if a database access error occurs or if parameter index is out of range |
void PreparedStatement_execute | ( | T | P | ) |
Executes the prepared SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
P | A PreparedStatement object |
SQLException | if a database error occurs |
ResultSet_T PreparedStatement_executeQuery | ( | T | P | ) |
Executes the prepared SQL statement, which returns a single ResultSet object.
A ResultSet "lives" only until the next call to a PreparedStatement method or until the Connection is returned to the Connection Pool. This means that Result Sets cannot be saved between queries.
P | A PreparedStatement object |
SQLException | if a database error occurs |
Copyright © 2004-2011 Tildeslash Ltd. All rights reserved.