DuckDBPreparedStatement
A wrapper around a JDBC PreparedStatement that provides type-safe parameter binding and error handling with DuckDB-specific error types.
This class encapsulates a JDBC PreparedStatement and provides methods for setting parameters and executing queries/updates in a functional style. All parameter setting methods return Either types for error handling, making it safe to compose operations.
Value parameters
- sql
-
the SQL query string, used for error reporting
- underlying
-
the underlying JDBC PreparedStatement
Attributes
- See also
-
DuckDBConnection.prepareStatement for creating prepared statements
DuckDBBatch for batch operations
DuckDBResultSet for query results
- Since
-
0.1.0
- Example
-
// Setting parameters and executing a query val result = for { stmt <- connection.prepareStatement("SELECT * FROM users WHERE age > ? AND name LIKE ?") _ <- stmt.setInt(1, 18) _ <- stmt.setString(2, "John%") rs <- stmt.executeQuery() } yield rs // Batch operations val batch = for { stmt <- connection.prepareStatement("INSERT INTO users (name, age) VALUES (?, ?)") _ <- stmt.setString(1, "Alice") _ <- stmt.setInt(2, 25) _ <- stmt.addBatch() _ <- stmt.setString(1, "Bob") _ <- stmt.setInt(2, 30) _ <- stmt.addBatch() } yield stmt
- Graph
-
- Supertypes
-
trait Serializabletrait Producttrait Equalstrait AutoCloseableclass Objecttrait Matchableclass AnyShow all
Members list
Value members
Concrete methods
Adds the current parameter values to the batch for later execution.
Adds the current parameter values to the batch for later execution.
After setting parameters, this method adds them to the batch. Multiple sets of parameters can be added to build up a batch for efficient execution.
Attributes
- Returns
-
Right(this) on success, Left(DuckDBError) on failure
- See also
-
DuckDBBatch for batch execution
- Since
-
0.1.0
- Example
-
val batch = for { stmt <- connection.prepareStatement("INSERT INTO users (name, age) VALUES (?, ?)") _ <- stmt.setString(1, "Alice") _ <- stmt.setInt(2, 25) _ <- stmt.addBatch() _ <- stmt.setString(1, "Bob") _ <- stmt.setInt(2, 30) _ <- stmt.addBatch() } yield stmt
Clears all parameter values that have been set on this prepared statement.
Clears all parameter values that have been set on this prepared statement.
After calling this method, all parameters will be unset and need to be set again before executing the statement.
Attributes
- Returns
-
Right(this) on success, Left(DuckDBError) on failure
- Since
-
0.1.0
- Example
-
val result = for { stmt <- connection.prepareStatement("SELECT * FROM users WHERE age > ?") _ <- stmt.setInt(1, 18) _ <- stmt.clearParameters() // Clear previous value _ <- stmt.setInt(1, 21) // Set new value rs <- stmt.executeQuery() } yield rs
Closes this prepared statement and releases its database resources.
Closes this prepared statement and releases its database resources.
It's important to close prepared statements when done with them to free database resources. This method is called automatically when using resource management patterns like Using
.
Attributes
- Since
-
0.1.0
- Example
-
val stmt = connection.prepareStatement("SELECT * FROM users") try { // Use the statement... } finally { stmt.close() }
Executes the prepared statement as a query and returns a result set.
Executes the prepared statement as a query and returns a result set.
This method is used for SELECT statements and other queries that return data. The returned result set must be properly closed after use.
Attributes
- Returns
-
Right(DuckDBResultSet) on success, Left(DuckDBError) on failure
- See also
-
DuckDBResultSet for processing query results
- Since
-
0.1.0
- Example
-
val result = for { stmt <- connection.prepareStatement("SELECT name, age FROM users WHERE id = ?") _ <- stmt.setInt(1, 123) rs <- stmt.executeQuery() } yield { if (rs.next()) { (rs.getString("name"), rs.getInt("age")) } else { ("Not found", 0) } }
Executes the prepared statement as an update and returns the number of affected rows.
Executes the prepared statement as an update and returns the number of affected rows.
This method is used for INSERT, UPDATE, DELETE statements and other SQL statements that don't return a result set.
Attributes
- Returns
-
Right(rowCount) on success, Left(DuckDBError) on failure
- Since
-
0.1.0
- Example
-
val result = for { stmt <- connection.prepareStatement("UPDATE users SET age = ? WHERE name = ?") _ <- stmt.setInt(1, 26) _ <- stmt.setString(2, "Alice") rows <- stmt.executeUpdate() } yield rows result match { case Right(count) => println(s"Updated $count rows") case Left(error) => println(s"Update failed: $error") }
Sets a boolean parameter in the prepared statement.
Sets a boolean parameter in the prepared statement.
Value parameters
- parameterIndex
-
the parameter index (1-based)
- value
-
the boolean value to set
Attributes
- Returns
-
Right(value) on success, Left(DuckDBError) on failure
- Since
-
0.1.0
- Example
-
stmt.setBoolean(1, true)
Sets a double parameter in the prepared statement.
Sets a double parameter in the prepared statement.
Value parameters
- parameterIndex
-
the parameter index (1-based)
- value
-
the double value to set
Attributes
- Returns
-
Right(value) on success, Left(DuckDBError) on failure
- Since
-
0.1.0
- Example
-
stmt.setDouble(1, 3.14159)
Sets an integer parameter in the prepared statement.
Sets an integer parameter in the prepared statement.
Value parameters
- parameterIndex
-
the parameter index (1-based)
- value
-
the integer value to set
Attributes
- Returns
-
Right(value) on success, Left(DuckDBError) on failure
- Since
-
0.1.0
- Example
-
val result = for { stmt <- connection.prepareStatement("SELECT * FROM users WHERE age > ?") _ <- stmt.setInt(1, 18) rs <- stmt.executeQuery() } yield rs
Sets a long parameter in the prepared statement.
Sets a long parameter in the prepared statement.
Value parameters
- parameterIndex
-
the parameter index (1-based)
- value
-
the long value to set
Attributes
- Returns
-
Right(value) on success, Left(DuckDBError) on failure
- Since
-
0.1.0
- Example
-
stmt.setLong(1, 1234567890L)
Sets a parameter to NULL in the prepared statement.
Sets a parameter to NULL in the prepared statement.
Value parameters
- parameterIndex
-
the parameter index (1-based)
- sqlType
-
the SQL type code (from java.sql.Types)
Attributes
- Returns
-
Right(sqlType) on success, Left(DuckDBError) on failure
- Since
-
0.1.0
- Example
-
import java.sql.Types stmt.setNull(1, Types.VARCHAR)
Sets a string parameter in the prepared statement.
Sets a string parameter in the prepared statement.
Value parameters
- parameterIndex
-
the parameter index (1-based)
- value
-
the string value to set
Attributes
- Returns
-
Right(value) on success, Left(DuckDBError) on failure
- Since
-
0.1.0
- Example
-
stmt.setString(1, "John Doe")