Package io.github.connellite.jdbc
Class NamedPreparedStatement
java.lang.Object
io.github.connellite.jdbc.NamedPreparedStatement
- All Implemented Interfaces:
AutoCloseable
PreparedStatement wrapper with named parameters (e.g.
:id).
Internally converts SQL to positional placeholders (?) for drivers
that only support JDBC positional parameters.-
Constructor Summary
ConstructorsConstructorDescriptionNamedPreparedStatement(Connection connection, String sql) Creates a named-parameter statement and compiles SQL to JDBC positional placeholders.NamedPreparedStatement(Connection connection, String sql, int autoGeneratedKeys) Creates a named-parameter statement and requests auto-generated keys behavior.NamedPreparedStatement(Connection connection, String sql, int[] columnIndexes) Creates a named-parameter statement and requests generated keys by column indexes.NamedPreparedStatement(Connection connection, String sql, int[] columnIndexes, SqlParser sqlParser) Creates a named-parameter statement with a custom parser and requests generated keys by column indexes.NamedPreparedStatement(Connection connection, String sql, int resultSetType, int resultSetConcurrency) Creates a named-parameter statement with a target result-set type and concurrency.NamedPreparedStatement(Connection connection, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) Creates a named-parameter statement with result-set type, concurrency and holdability.NamedPreparedStatement(Connection connection, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, SqlParser sqlParser) Creates a named-parameter statement with a custom parser, result-set type, concurrency and holdability.NamedPreparedStatement(Connection connection, String sql, int resultSetType, int resultSetConcurrency, SqlParser sqlParser) Creates a named-parameter statement with a custom parser, target result-set type and concurrency.NamedPreparedStatement(Connection connection, String sql, int autoGeneratedKeys, SqlParser sqlParser) Creates a named-parameter statement with a custom parser and requests auto-generated keys behavior.NamedPreparedStatement(Connection connection, String sql, SqlParser sqlParser) Creates a named-parameter statement using a custom SQL parser.NamedPreparedStatement(Connection connection, String sql, String[] columnNames) Creates a named-parameter statement and requests generated keys by column names.NamedPreparedStatement(Connection connection, String sql, String[] columnNames, SqlParser sqlParser) Creates a named-parameter statement with a custom parser and requests generated keys by column names. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBatch()Adds current parameter set to batch after binding validation.voidClears both wrapper binding state and underlying JDBC parameter values.voidclose()booleanexecute()Executes statement after ensuring that all named parameters are bound.int[]Executes JDBC batch on underlying statement.Executes query after ensuring that all named parameters are bound.intExecutes update after ensuring that all named parameters are bound.Debug helper: names of parameters bound since lastclearParameters().Returns parameter names in positional order (including repeated occurrences).Returns parsed SQL transformed to JDBC placeholders.static NamedPreparedStatementof(Connection connection, String sql) Static factory forNamedPreparedStatement.static NamedPreparedStatementof(Connection connection, String sql, int autoGeneratedKeys) Static factory forNamedPreparedStatementwith auto-generated keys behavior.static NamedPreparedStatementof(Connection connection, String sql, int[] columnIndexes) Static factory forNamedPreparedStatementrequesting generated keys by column indexes.static NamedPreparedStatementof(Connection connection, String sql, int[] columnIndexes, SqlParser sqlParser) Static factory forNamedPreparedStatementrequesting generated keys by column indexes with custom SQL parser.static NamedPreparedStatementof(Connection connection, String sql, int resultSetType, int resultSetConcurrency) Static factory forNamedPreparedStatementwith result-set type and concurrency.static NamedPreparedStatementof(Connection connection, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) Static factory forNamedPreparedStatementwith result-set type, concurrency and holdability.static NamedPreparedStatementof(Connection connection, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, SqlParser sqlParser) Static factory forNamedPreparedStatementwith result-set type, concurrency, holdability and custom SQL parser.static NamedPreparedStatementof(Connection connection, String sql, int resultSetType, int resultSetConcurrency, SqlParser sqlParser) Static factory forNamedPreparedStatementwith result-set type, concurrency and custom SQL parser.static NamedPreparedStatementof(Connection connection, String sql, int autoGeneratedKeys, SqlParser sqlParser) Static factory forNamedPreparedStatementwith auto-generated keys behavior and custom SQL parser.static NamedPreparedStatementof(Connection connection, String sql, SqlParser sqlParser) Static factory forNamedPreparedStatementwith a custom SQL parser.static NamedPreparedStatementof(Connection connection, String sql, String[] columnNames) Static factory forNamedPreparedStatementrequesting generated keys by column names.static NamedPreparedStatementof(Connection connection, String sql, String[] columnNames, SqlParser sqlParser) Static factory forNamedPreparedStatementrequesting generated keys by column names with custom SQL parser.Binds all entries from map usingsetObject(String, Object).Binds SQL array value.setAsciiStream(String name, InputStream value) Binds ASCII stream.setAsciiStream(String name, InputStream value, int length) Binds ASCII stream with int length.setAsciiStream(String name, InputStream value, long length) Binds ASCII stream with long length.Binds@Bindmembers ofbean.setBigDecimal(String name, BigDecimal value) BindsBigDecimalvalue to all occurrences of named parameter.setBinaryStream(String name, InputStream value) Binds binary stream.setBinaryStream(String name, InputStream value, int length) Binds binary stream with int length.setBinaryStream(String name, InputStream value, long length) Binds binary stream with long length.setBlob(String name, InputStream inputStream) Binds blob binary stream value.setBlob(String name, InputStream inputStream, long length) Binds blob binary stream value with explicit length.BindsBlobvalue.setBoolean(String name, boolean value) Binds boolean value to all occurrences of named parameter.Binds byte value to all occurrences of named parameter.Binds byte array value to all occurrences of named parameter.setCharacterStream(String name, Reader reader) Binds character stream.setCharacterStream(String name, Reader reader, int length) Binds character stream with int length.setCharacterStream(String name, Reader reader, long length) Binds character stream with long length.Binds character reader as CLOB.Binds character reader as CLOB with explicit length.BindsClobvalue.BindsDatevalue to all occurrences of named parameter.BindsDatevalue with calendar to all occurrences of named parameter.Binds double value to all occurrences of named parameter.Binds float value to all occurrences of named parameter.Binds int value to all occurrences of named parameter.Binds long value to all occurrences of named parameter.setNCharacterStream(String name, Reader value) Binds NCHAR character stream.setNCharacterStream(String name, Reader value, long length) Binds NCHAR character stream with explicit length.Binds reader as NCLOB.Binds reader as NCLOB with explicit length.BindsNClobvalue.setNString(String name, String value) Binds national character string value.Binds SQL NULL of generic typeTypes.NULL.Binds SQL NULL with explicit JDBC type.Binds SQL NULL with explicit JDBC type and database-specific type name.Binds object value to all occurrences of named parameter.Binds object with explicit JDBC SQL type.Binds object with explicit JDBC SQL type and scale/length.Binds object withSQLType.Binds object withSQLTypeand scale/length.Binds SQL reference value.Binds JDBC row id value.Binds short value to all occurrences of named parameter.Binds SQLXML value.Binds string value to all occurrences of named parameter.BindsTimevalue to all occurrences of named parameter.BindsTimevalue with calendar to all occurrences of named parameter.setTimestamp(String name, Timestamp value) BindsTimestampvalue to all occurrences of named parameter.setTimestamp(String name, Timestamp value, Calendar cal) BindsTimestampvalue with calendar to all occurrences of named parameter.Binds URL value.unwrap()Returns underlying JDBC prepared statement.
-
Constructor Details
-
NamedPreparedStatement
Creates a named-parameter statement and compiles SQL to JDBC positional placeholders.- Parameters:
connection- JDBC connection used to create underlying prepared statementsql- SQL with named parameters (for example:id)- Throws:
SQLException
-
NamedPreparedStatement
public NamedPreparedStatement(Connection connection, String sql, SqlParser sqlParser) throws SQLException Creates a named-parameter statement using a custom SQL parser.- Parameters:
connection- JDBC connection used to create underlying prepared statementsql- SQL with parser-specific named parameterssqlParser- SQL parser used to detect named parameters- Throws:
SQLException
-
NamedPreparedStatement
public NamedPreparedStatement(Connection connection, String sql, int autoGeneratedKeys) throws SQLException Creates a named-parameter statement and requests auto-generated keys behavior.- Throws:
SQLException
-
NamedPreparedStatement
public NamedPreparedStatement(Connection connection, String sql, int autoGeneratedKeys, SqlParser sqlParser) throws SQLException Creates a named-parameter statement with a custom parser and requests auto-generated keys behavior.- Throws:
SQLException
-
NamedPreparedStatement
public NamedPreparedStatement(Connection connection, String sql, int resultSetType, int resultSetConcurrency) throws SQLException Creates a named-parameter statement with a target result-set type and concurrency.- Throws:
SQLException
-
NamedPreparedStatement
public NamedPreparedStatement(Connection connection, String sql, int resultSetType, int resultSetConcurrency, SqlParser sqlParser) throws SQLException Creates a named-parameter statement with a custom parser, target result-set type and concurrency.- Throws:
SQLException
-
NamedPreparedStatement
public NamedPreparedStatement(Connection connection, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException Creates a named-parameter statement with result-set type, concurrency and holdability.- Throws:
SQLException
-
NamedPreparedStatement
public NamedPreparedStatement(Connection connection, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, SqlParser sqlParser) throws SQLException Creates a named-parameter statement with a custom parser, result-set type, concurrency and holdability.- Throws:
SQLException
-
NamedPreparedStatement
public NamedPreparedStatement(Connection connection, String sql, int[] columnIndexes) throws SQLException Creates a named-parameter statement and requests generated keys by column indexes.- Throws:
SQLException
-
NamedPreparedStatement
public NamedPreparedStatement(Connection connection, String sql, int[] columnIndexes, SqlParser sqlParser) throws SQLException Creates a named-parameter statement with a custom parser and requests generated keys by column indexes.- Throws:
SQLException
-
NamedPreparedStatement
public NamedPreparedStatement(Connection connection, String sql, String[] columnNames) throws SQLException Creates a named-parameter statement and requests generated keys by column names.- Throws:
SQLException
-
NamedPreparedStatement
public NamedPreparedStatement(Connection connection, String sql, String[] columnNames, SqlParser sqlParser) throws SQLException Creates a named-parameter statement with a custom parser and requests generated keys by column names.- Throws:
SQLException
-
-
Method Details
-
of
Static factory forNamedPreparedStatement.- Throws:
SQLException
-
of
public static NamedPreparedStatement of(Connection connection, String sql, SqlParser sqlParser) throws SQLException Static factory forNamedPreparedStatementwith a custom SQL parser.- Throws:
SQLException
-
of
public static NamedPreparedStatement of(Connection connection, String sql, int autoGeneratedKeys) throws SQLException Static factory forNamedPreparedStatementwith auto-generated keys behavior.- Throws:
SQLException
-
of
public static NamedPreparedStatement of(Connection connection, String sql, int autoGeneratedKeys, SqlParser sqlParser) throws SQLException Static factory forNamedPreparedStatementwith auto-generated keys behavior and custom SQL parser.- Throws:
SQLException
-
of
public static NamedPreparedStatement of(Connection connection, String sql, int resultSetType, int resultSetConcurrency) throws SQLException Static factory forNamedPreparedStatementwith result-set type and concurrency.- Throws:
SQLException
-
of
public static NamedPreparedStatement of(Connection connection, String sql, int resultSetType, int resultSetConcurrency, SqlParser sqlParser) throws SQLException Static factory forNamedPreparedStatementwith result-set type, concurrency and custom SQL parser.- Throws:
SQLException
-
of
public static NamedPreparedStatement of(Connection connection, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException Static factory forNamedPreparedStatementwith result-set type, concurrency and holdability.- Throws:
SQLException
-
of
public static NamedPreparedStatement of(Connection connection, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability, SqlParser sqlParser) throws SQLException Static factory forNamedPreparedStatementwith result-set type, concurrency, holdability and custom SQL parser.- Throws:
SQLException
-
of
public static NamedPreparedStatement of(Connection connection, String sql, int[] columnIndexes) throws SQLException Static factory forNamedPreparedStatementrequesting generated keys by column indexes.- Throws:
SQLException
-
of
public static NamedPreparedStatement of(Connection connection, String sql, int[] columnIndexes, SqlParser sqlParser) throws SQLException Static factory forNamedPreparedStatementrequesting generated keys by column indexes with custom SQL parser.- Throws:
SQLException
-
of
public static NamedPreparedStatement of(Connection connection, String sql, String[] columnNames) throws SQLException Static factory forNamedPreparedStatementrequesting generated keys by column names.- Throws:
SQLException
-
of
public static NamedPreparedStatement of(Connection connection, String sql, String[] columnNames, SqlParser sqlParser) throws SQLException Static factory forNamedPreparedStatementrequesting generated keys by column names with custom SQL parser.- Throws:
SQLException
-
setObject
Binds object value to all occurrences of named parameter.- Throws:
SQLException
-
setInt
Binds int value to all occurrences of named parameter.- Throws:
SQLException
-
setLong
Binds long value to all occurrences of named parameter.- Throws:
SQLException
-
setString
Binds string value to all occurrences of named parameter.- Throws:
SQLException
-
setBoolean
Binds boolean value to all occurrences of named parameter.- Throws:
SQLException
-
setByte
Binds byte value to all occurrences of named parameter.- Throws:
SQLException
-
setShort
Binds short value to all occurrences of named parameter.- Throws:
SQLException
-
setFloat
Binds float value to all occurrences of named parameter.- Throws:
SQLException
-
setDouble
Binds double value to all occurrences of named parameter.- Throws:
SQLException
-
setBigDecimal
BindsBigDecimalvalue to all occurrences of named parameter.- Throws:
SQLException
-
setBytes
Binds byte array value to all occurrences of named parameter.- Throws:
SQLException
-
setDate
BindsDatevalue to all occurrences of named parameter.- Throws:
SQLException
-
setDate
BindsDatevalue with calendar to all occurrences of named parameter.- Throws:
SQLException
-
setTime
BindsTimevalue to all occurrences of named parameter.- Throws:
SQLException
-
setTime
BindsTimevalue with calendar to all occurrences of named parameter.- Throws:
SQLException
-
setTimestamp
BindsTimestampvalue to all occurrences of named parameter.- Throws:
SQLException
-
setTimestamp
public NamedPreparedStatement setTimestamp(String name, Timestamp value, Calendar cal) throws SQLException BindsTimestampvalue with calendar to all occurrences of named parameter.- Throws:
SQLException
-
setNull
Binds SQL NULL of generic typeTypes.NULL.- Throws:
SQLException
-
setNull
Binds SQL NULL with explicit JDBC type.- Throws:
SQLException
-
setNull
public NamedPreparedStatement setNull(String name, int sqlType, String typeName) throws SQLException Binds SQL NULL with explicit JDBC type and database-specific type name.- Throws:
SQLException
-
setObject
public NamedPreparedStatement setObject(String name, Object value, int targetSqlType) throws SQLException Binds object with explicit JDBC SQL type.- Throws:
SQLException
-
setObject
public NamedPreparedStatement setObject(String name, Object value, int targetSqlType, int scaleOrLength) throws SQLException Binds object with explicit JDBC SQL type and scale/length.- Throws:
SQLException
-
setObject
public NamedPreparedStatement setObject(String name, Object value, SQLType targetSqlType) throws SQLException Binds object withSQLType.- Throws:
SQLException
-
setObject
public NamedPreparedStatement setObject(String name, Object value, SQLType targetSqlType, int scaleOrLength) throws SQLException Binds object withSQLTypeand scale/length.- Throws:
SQLException
-
setArray
Binds SQL array value.- Throws:
SQLException
-
setRef
Binds SQL reference value.- Throws:
SQLException
-
setURL
Binds URL value.- Throws:
SQLException
-
setRowId
Binds JDBC row id value.- Throws:
SQLException
-
setSQLXML
Binds SQLXML value.- Throws:
SQLException
-
setBlob
BindsBlobvalue.- Throws:
SQLException
-
setBlob
Binds blob binary stream value.- Throws:
SQLException
-
setBlob
public NamedPreparedStatement setBlob(String name, InputStream inputStream, long length) throws SQLException Binds blob binary stream value with explicit length.- Throws:
SQLException
-
setClob
BindsClobvalue.- Throws:
SQLException
-
setClob
Binds character reader as CLOB.- Throws:
SQLException
-
setClob
Binds character reader as CLOB with explicit length.- Throws:
SQLException
-
setNClob
BindsNClobvalue.- Throws:
SQLException
-
setNClob
Binds reader as NCLOB.- Throws:
SQLException
-
setNClob
Binds reader as NCLOB with explicit length.- Throws:
SQLException
-
setAsciiStream
Binds ASCII stream.- Throws:
SQLException
-
setAsciiStream
public NamedPreparedStatement setAsciiStream(String name, InputStream value, int length) throws SQLException Binds ASCII stream with int length.- Throws:
SQLException
-
setAsciiStream
public NamedPreparedStatement setAsciiStream(String name, InputStream value, long length) throws SQLException Binds ASCII stream with long length.- Throws:
SQLException
-
setBinaryStream
Binds binary stream.- Throws:
SQLException
-
setBinaryStream
public NamedPreparedStatement setBinaryStream(String name, InputStream value, int length) throws SQLException Binds binary stream with int length.- Throws:
SQLException
-
setBinaryStream
public NamedPreparedStatement setBinaryStream(String name, InputStream value, long length) throws SQLException Binds binary stream with long length.- Throws:
SQLException
-
setCharacterStream
Binds character stream.- Throws:
SQLException
-
setCharacterStream
public NamedPreparedStatement setCharacterStream(String name, Reader reader, int length) throws SQLException Binds character stream with int length.- Throws:
SQLException
-
setCharacterStream
public NamedPreparedStatement setCharacterStream(String name, Reader reader, long length) throws SQLException Binds character stream with long length.- Throws:
SQLException
-
setNCharacterStream
Binds NCHAR character stream.- Throws:
SQLException
-
setNCharacterStream
public NamedPreparedStatement setNCharacterStream(String name, Reader value, long length) throws SQLException Binds NCHAR character stream with explicit length.- Throws:
SQLException
-
setNString
Binds national character string value.- Throws:
SQLException
-
setAll
Binds all entries from map usingsetObject(String, Object).- Throws:
SQLException
-
setBean
Binds@Bindmembers ofbean.Each member is read by reflection. When
Bind.converter()is notTypeConverter.DefaultConverter,TypeConverter.convert(Object)runs first. Anullresult is bound withsetNull(String); any other result usessetObject(String, Object). Members whose names are not parameters of this statement are ignored. Collection values are not expanded.- Throws:
SQLException- when a converter cannot be created orconvertfailsIllegalArgumentException- whenbeanhas no@Bindmembers or none of those names are parameters of this statementIllegalStateException- when a member cannot be readNullPointerException- whenbeanisnull
-
clearParameters
Clears both wrapper binding state and underlying JDBC parameter values.- Throws:
SQLException
-
executeQuery
Executes query after ensuring that all named parameters are bound.- Throws:
SQLException
-
executeUpdate
Executes update after ensuring that all named parameters are bound.- Throws:
SQLException
-
execute
Executes statement after ensuring that all named parameters are bound.- Throws:
SQLException
-
addBatch
Adds current parameter set to batch after binding validation.- Throws:
SQLException
-
executeBatch
Executes JDBC batch on underlying statement.- Throws:
SQLException
-
unwrap
Returns underlying JDBC prepared statement. -
getBoundValues
Debug helper: names of parameters bound since lastclearParameters(). -
getParsedSql
Returns parsed SQL transformed to JDBC placeholders. -
getParameterOrder
Returns parameter names in positional order (including repeated occurrences). -
close
- Specified by:
closein interfaceAutoCloseable- Throws:
SQLException
-