On this page
A java.sql.PreparedStatement prepares SQL with positional ? markers. Bind values with setters before executing the command. The first parameter index is 1. Bind application values separately from SQL text. SQL identifiers such as table names are part of the SQL statement.
Execute and batch
| Method | Behavior |
|---|---|
executeQuery() |
Executes the bound statement and returns a relational ResultSet. |
executeUpdate(), executeLargeUpdate() |
Execute a statement that returns an update count. Choose the long form for large counts. |
execute() |
Executes and returns true for a result set. Use getResultSet() or getUpdateCount() as for a Statement. |
clearParameters() |
Clears the current bindings. Driver 3.1 also clears bindings after individual executions, so bind values again for every execution. |
addBatch() |
Adds a snapshot of current bindings to the prepared batch. Bind each row before adding it. |
executeBatch(), executeLargeBatch() |
Execute the prepared batch and return the server counts as int[] or long[]. The server can return a combined count for several parameter sets. Batch execution clears the queued parameter sets. |
clearBatch() |
The method inherited from Statement clears the plain SQL batch queue. It does not clear parameter sets queued with addBatch() in 3.1. Close and prepare a fresh statement if you need to discard a prepared batch. |
getMetaData() |
Returns null in 3.1. To inspect query output, call getMetaData() on the resulting ResultSet. |
getParameterMetaData() |
Returns parameter metadata from the server’s statement signature. |
Use the overloads without a SQL string. The inherited SQL-string execution and batch methods do not provide a supported way to execute new SQL with a prepared statement.
Example: bind query parameters
First run the Statement setup example.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class FindTask {
public static void main(String[] args) throws Exception {
String url = args.length == 0 ? "jdbc:polypheny://localhost:20590" : args[0];
try (Connection connection = DriverManager.getConnection(url, "pa", "");
PreparedStatement query = connection.prepareStatement(
"SELECT title FROM public.jdbc_docs_tasks WHERE id = ?")) {
System.out.println("Parameters: " + query.getParameterMetaData().getParameterCount());
query.setInt(1, 1);
try (ResultSet result = query.executeQuery()) {
if (result.next()) {
System.out.println(result.getString("title"));
}
}
}
}
}
Setter methods for value
| Values | Setters and 3.1 behavior |
|---|---|
| Null | setNull(index, sqlType) and setNull(index, sqlType, typeName) both bind an untyped Prism null. |
| Booleans and numbers | setBoolean, setByte, setShort, setInt, setLong, setFloat, setDouble and setBigDecimal. |
| Text and binary | setString, setNString and setBytes. |
| Date and time | setDate, setTime and setTimestamp with or without a Calendar. |
| Streams and readers | setAsciiStream, setUnicodeStream (deprecated), setBinaryStream, setCharacterStream and setNCharacterStream. Prefer the overloads without lengths. Several overloads ignore the supplied length, and the setCharacterStream(index, reader, intLength) overload does not bind a value in 3.1. |
| LOBs and arrays | setBlob, setClob, setNClob and setArray. Stream and reader LOB bindings can materialize the input in memory, and the length overloads do not limit how much data is read. |
| Object conversion | setObject(index, value) infers a supported Prism value from the Java type. setObject(index, value, targetSqlType) uses a supported java.sql.Types integer target. The scale-or-length overload and the JDBC default SQLType overloads are unsupported. Prefer specific setters for predictable conversions. |
| Unsupported bindings | setRef, setURL, setRowId, setSQLXML and SQL Struct binding. |
© Polypheny GmbH. All Rights Reserved.