Class NamedParameterStatement
- All Implemented Interfaces:
AutoCloseable
com.iziix.db.prop.InputProps
with the com.iziix.db.prop.InputVariable's
containing mappings to VSField
's.This class wraps around a PreparedStatement
and enables IIZI SQL statement preparation. It also makes it easier for the com.iziix.db.api.IQueryStatementInput
custom processor to set their values as required.
- Author:
- Christopher Mindus
Field Summary
Modifier and TypeFieldDescriptionstatic final Object
The special NULL value object for a set parameter.Constructor Summary
ConstructorDescriptionNamedParameterStatement
(Connection connection, String query) Creates a NamedParameterStatement.Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the statement.int[]
getIndicies
(String name) Returns the indicies for a parameter.Gets the map of names of host variables and their indicies.Gets the original query.String[]
Gets the list of parameter names.Returns the parsed query containing question marks.Returns the prepares statement that can be used to set parameter using indicies.static String
parse
(String query, Map<String, IntArrayList> map) Parses the query statement.void
reset()
Resets the statement to release memory, i.e.void
setBigDecimal
(String name, BigDecimal x) Sets the designated parameter to the givenjava.math.BigDecimal
value.void
setBoolean
(String name, boolean x) Sets the designated parameter to the given Javaboolean
value.void
Sets the designated parameter to the given Javabyte
value.void
Sets the designated parameter to the given Java array of bytes.void
Sets the designated parameter to the givenjava.sql.Date
value using the default time zone of the virtual machine that is running the application.void
Sets the designated parameter to the givenjava.sql.Date
value, using the givenCalendar
object.void
Sets the designated parameter to the given Javadouble
value.void
Sets the designated parameter to the given Javafloat
value.void
Sets the designated parameter to the given Javaint
value.void
Sets the designated parameter to the given Javalong
value.void
Sets the designated parameter to SQLNULL
.void
Sets the designated parameter to SQLNULL
.void
Sets the value of the designated parameter using the given object.void
Sets the value of the designated parameter with the given object.void
Sets the value of the designated parameter with the given object.void
Sets the value of the designated parameter with the given object.void
Sets the value of the designated parameter with the given object.void
Sets the designated parameter to the givenjava.sql.RowId
object.void
Sets the designated parameter to the given Javashort
value.void
Sets the designated parameter to the given JavaString
value.void
Sets the designated parameter to the givenjava.sql.Time
value.void
Sets the designated parameter to the givenjava.sql.Time
value, using the givenCalendar
object.void
setTimestamp
(String name, Timestamp x) Sets the designated parameter to the givenjava.sql.Timestamp
value.void
setTimestamp
(String name, Timestamp x, Calendar cal) Sets the designated parameter to the givenjava.sql.Timestamp
value, using the givenCalendar
object.void
Sets the designated parameter to the givenjava.net.URL
value.
Field Details
NULL_VALUE
The special NULL value object for a set parameter.
Constructor Details
NamedParameterStatement
Creates a NamedParameterStatement. Wraps a call to connection.prepareStatement
.- Parameters:
connection
- The database connection.query
- The parameterized query.- Throws:
SQLException
- If the statement could not be created.
Method Details
parse
Parses the query statement. Upon return, the map contains the host name parameters encountered in the query and a list of integer indicies that are one-based for JDBC usage.- Parameters:
query
- The query statement.map
- The map is filled with names and their JDBC variable indicies upon return.- Returns:
- Parsed query statement.
getPreparedStatement
Returns the prepares statement that can be used to set parameter using indicies. For certain advanced statement operation not provided in this class, you will have to use this method to retrieve the statement and to call the method required of the statement directly. The indices of the parameter can be retrieved usinggetIndicies(String name)
.- Returns:
- The prepared statement.
getParsedQuery
Returns the parsed query containing question marks.This String is used to prepare the statement for JDBC.
- Returns:
- The parsed query where the ":variableNames" are replaced with "?".
getOriginalQuery
Gets the original query.- Returns:
- The original query containing the host variable parameters.
getParameterNames
Gets the list of parameter names.- Returns:
- An array with the parameter names, never null.
getIndicies
Returns the indicies for a parameter.- Parameters:
name
- Parameter name.- Returns:
- The parameter indicies, where an index is one-based for JDBC usage.
- Throws:
NamedParameterException
- If the parameter does not exist.
getIndiciesMap
Gets the map of names of host variables and their indicies.- Returns:
- The map.
reset
public void reset()Resets the statement to release memory, i.e. parameters and set values.This method is not intended to be called by client code, it is used by the framework.
setNull
Sets the designated parameter to SQLNULL
.Note: You must specify the parameter's SQL type.
- Parameters:
name
- The name of the parameter.sqlType
- The SQL type code defined injava.sql.Types
.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.SQLFeatureNotSupportedException
- ifsqlType
is aARRAY
,BLOB
,CLOB
,DATALINK
,JAVA_OBJECT
,NCHAR
,NCLOB
,NVARCHAR
,LONGNVARCHAR
,REF
,ROWID
,SQLXML
orSTRUCT
data type and the JDBC driver does not support this data type.NamedParameterException
- if the parameter does not exist.
setBoolean
Sets the designated parameter to the given Javaboolean
value. The driver converts this to an SQLBIT
orBOOLEAN
value when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.NamedParameterException
- if the parameter does not exist.
setByte
Sets the designated parameter to the given Javabyte
value. The driver converts this to an SQLTINYINT
value when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setShort
Sets the designated parameter to the given Javashort
value. The driver converts this to an SQLSMALLINT
value when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setInt
Sets the designated parameter to the given Javaint
value. The driver converts this to an SQLINTEGER
value when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setLong
Sets the designated parameter to the given Javalong
value. The driver converts this to an SQLBIGINT
value when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setFloat
Sets the designated parameter to the given Javafloat
value. The driver converts this to an SQLREAL
value when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setDouble
Sets the designated parameter to the given Javadouble
value. The driver converts this to an SQLDOUBLE
value when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setBigDecimal
Sets the designated parameter to the givenjava.math.BigDecimal
value. The driver converts this to an SQLNUMERIC
value when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setString
Sets the designated parameter to the given JavaString
value. The driver converts this to an SQLVARCHAR
orLONGVARCHAR
value (depending on the argument's size relative to the driver's limits onVARCHAR
values) when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setBytes
Sets the designated parameter to the given Java array of bytes. The driver converts this to an SQLVARBINARY
orLONGVARBINARY
(depending on the argument's size relative to the driver's limits onVARBINARY
values) when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setDate
Sets the designated parameter to the givenjava.sql.Date
value using the default time zone of the virtual machine that is running the application. The driver converts this to an SQLDATE
value when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setTime
Sets the designated parameter to the givenjava.sql.Time
value. The driver converts this to an SQLTIME
value when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setTimestamp
Sets the designated parameter to the givenjava.sql.Timestamp
value. The driver converts this to an SQLTIMESTAMP
value when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setObject
Sets the value of the designated parameter with the given object. This method is similar tosetObject(String name, Object x, int targetSqlType, int scaleOrLength)
, except that it assumes a scale of zero.- Parameters:
name
- The name of the parameter.x
- The object containing the input parameter value.targetSqlType
- The SQL type (as defined in java.sql.Types) to be sent to the database.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closed PreparedStatement.SQLFeatureNotSupportedException
- if the JDBC driver does not support the specified targetSqlType.NamedParameterException
- if the parameter does not exist.- See Also:
setObject
Sets the value of the designated parameter using the given object.The JDBC specification specifies a standard mapping from Java
Object
types to SQL types. The given argument will be converted to the corresponding SQL type before being sent to the database.Note that this method may be used to pass datatabase-specific abstract data types, by using a driver-specific Java type. If the object is of a class implementing the interface
SQLData
, the JDBC driver should call the methodSQLData.writeSQL
to write it to the SQL data stream. If, on the other hand, the object is of a class implementingRef
,Blob
,Clob
,NClob
,Struct
,java.net.URL
,RowId
,SQLXML
orArray
, the driver should pass it to the database as a value of the corresponding SQL type.Note: Not all databases allow for a non-typed Null to be sent to the backend. For maximum portability, the
setNull
or thesetObject(int parameterIndex, Object x, int sqlType)
method should be used instead ofsetObject(int parameterIndex, Object x)
.Note: This method throws an exception if there is an ambiguity, for example, if the object is of a class implementing more than one of the interfaces named above.
- Parameters:
name
- The name of the parameter.x
- The object containing the input parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs; this method is called on a closedPreparedStatement
or the type of the given object is ambiguous.NamedParameterException
- if the parameter does not exist.
setDate
Sets the designated parameter to the givenjava.sql.Date
value, using the givenCalendar
object. The driver uses theCalendar
object to construct an SQLDATE
value, which the driver then sends to the database. With aCalendar
object, the driver can calculate the date taking into account a custom timezone. If noCalendar
object is specified, the driver uses the default timezone, which is that of the virtual machine running the application.- Parameters:
name
- The name of the parameter.x
- The parameter value.cal
- TheCalendar
object the driver will use to construct the date.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setTime
Sets the designated parameter to the givenjava.sql.Time
value, using the givenCalendar
object. The driver uses theCalendar
object to construct an SQLTIME
value, which the driver then sends to the database. With aCalendar
object, the driver can calculate the time taking into account a custom timezone. If noCalendar
object is specified, the driver uses the default timezone, which is that of the virtual machine running the application.- Parameters:
name
- The name of the parameter.x
- The parameter value.cal
- TheCalendar
object the driver will use to construct the time.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setTimestamp
Sets the designated parameter to the givenjava.sql.Timestamp
value, using the givenCalendar
object. The driver uses theCalendar
object to construct an SQLTIMESTAMP
value, which the driver then sends to the database. With aCalendar
object, the driver can calculate the timestamp taking into account a custom timezone. If noCalendar
object is specified, the driver uses the default timezone, which is that of the virtual machine running the application.- Parameters:
name
- The name of the parameter.x
- The parameter value.cal
- TheCalendar
object the driver will use to construct the timestamp.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.NamedParameterException
- if the parameter does not exist.
setNull
Sets the designated parameter to SQLNULL
. This version of the methodsetNull
should be used for user-defined types and REF type parameters. Examples of user-defined types include: STRUCT, DISTINCT, JAVA_OBJECT, and named array types.Note: To be portable, applications must give the SQL type code and the fully-qualified SQL type name when specifying a NULL user-defined or REF parameter. In the case of a user-defined type the name is the type name of the parameter itself. For a REF parameter, the name is the type name of the referenced type. If a JDBC driver does not need the type code or type name information, it may ignore it.
Although it is intended for user-defined and Ref parameters, this method may be used to set a null parameter of any JDBC type. If the parameter does not have a user-defined or REF type, the given typeName is ignored.
- Parameters:
name
- The name of the parameter.sqlType
- A value fromjava.sql.Types
.typeName
- The fully-qualified name of an SQL user-defined type; ignored if the parameter is not a user-defined type or REF.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.SQLFeatureNotSupportedException
- ifsqlType
is aARRAY
,BLOB
,CLOB
,DATALINK
,JAVA_OBJECT
,NCHAR
,NCLOB
,NVARCHAR
,LONGNVARCHAR
,REF
,ROWID
,SQLXML
orSTRUCT
data type and the JDBC driver does not support this data type or if the JDBC driver does not support this method.NamedParameterException
- if the parameter does not exist.
setURL
Sets the designated parameter to the givenjava.net.URL
value. The driver converts this to an SQLDATALINK
value when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- Thejava.net.URL
object to be set.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.SQLFeatureNotSupportedException
- if the JDBC driver does not support this method.NamedParameterException
- if the parameter does not exist.
setObject
public void setObject(String name, Object x, int targetSqlType, int scaleOrLength) throws SQLException Sets the value of the designated parameter with the given object. If the second argument is anInputStream
then the stream must contain the number of bytes specified by scaleOrLength. If the second argument is aReader
then the reader must contain the number of characters specified by scaleOrLength. If these conditions are not true the driver will generate aSQLException
when the prepared statement is executed.The given Java object will be converted to the given targetSqlType before being sent to the database. If the object has a custom mapping (is of a class implementing the interface
SQLData
), the JDBC driver should call the methodSQLData.writeSQL
to write it to the SQL data stream. If, on the other hand, the object is of a class implementingRef
,Blob
,Clob
,NClob
,Struct
,java.net.URL
, orArray
, the driver should pass it to the database as a value of the corresponding SQL type.Note that this method may be used to pass database-specific abstract data types.
- Parameters:
name
- The name of the parameter.x
- The object containing the input parameter value.targetSqlType
- The SQL type (as defined in java.sql.Types) to be sent to the database. The scale argument may further qualify this type.scaleOrLength
- Forjava.sql.Types.DECIMAL
orjava.sql.Types.NUMERIC types
, this is the number of digits after the decimal point. For Java Object typesInputStream
andReader
, this is the length of the data in the stream or reader. For all other types, this value will be ignored.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs; this method is called on a closedPreparedStatement
or if the Java Object specified by x is an InputStream or Reader object and the value of the scale parameter is less than zeroSQLFeatureNotSupportedException
- if the JDBC driver does not support the specified targetSqlTypeNamedParameterException
- if the parameter does not exist.- See Also:
setRowId
Sets the designated parameter to the givenjava.sql.RowId
object. The driver converts this to a SQLROWID
value when it sends it to the database.- Parameters:
name
- The name of the parameter.x
- The parameter value.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
.SQLFeatureNotSupportedException
- if the JDBC driver does not support this method.NamedParameterException
- if the parameter does not exist.
setObject
public void setObject(String name, Object x, SQLType targetSqlType, int scaleOrLength) throws SQLException Sets the value of the designated parameter with the given object.If the second argument is an
InputStream
then the stream must contain the number of bytes specified by scaleOrLength. If the second argument is aReader
then the reader must contain the number of characters specified by scaleOrLength. If these conditions are not true the driver will generate aSQLException
when the prepared statement is executed.The given Java object will be converted to the given targetSqlType before being sent to the database.
If the object has a custom mapping (is of a class implementing the interface
SQLData
), the JDBC driver should call the methodSQLData.writeSQL
to write it to the SQL data stream. If, on the other hand, the object is of a class implementingRef
,Blob
,Clob
,NClob
,Struct
,java.net.URL
, orArray
, the driver should pass it to the database as a value of the corresponding SQL type.Note that this method may be used to pass database-specific abstract data types.
The default implementation will throw
SQLFeatureNotSupportedException
.- Parameters:
name
- The name of the parameter.x
- The object containing the input parameter value.targetSqlType
- The SQL type to be sent to the database. The scale argument may further qualify this type.scaleOrLength
- forjava.sql.JDBCType.DECIMAL
orjava.sql.JDBCType.NUMERIC types
, this is the number of digits after the decimal point. For Java Object typesInputStream
andReader
, this is the length of the data in the stream or reader. For all other types, this value will be ignored.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
or if the Java Object specified by x is an InputStream or Reader object and the value of the scale parameter is less than zeroSQLFeatureNotSupportedException
- if the JDBC driver does not support the specified targetSqlTypeNamedParameterException
- if the parameter does not exist.- See Also:
setObject
Sets the value of the designated parameter with the given object.This method is similar to
setObject(String name, Object x, SQLType targetSqlType, int scaleOrLength)
, except that it assumes a scale of zero.The default implementation will throw
SQLFeatureNotSupportedException
.- Parameters:
name
- The name of the parameter.x
- The object containing the input parameter value.targetSqlType
- The SQL type to be sent to the database.- Throws:
SQLException
- if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closedPreparedStatement
SQLFeatureNotSupportedException
- if the JDBC driver does not support the specified targetSqlTypeNamedParameterException
- if the parameter does not exist.- See Also:
close
Closes the statement.- Specified by:
close
in interfaceAutoCloseable
- Throws:
SQLException
- If there is an SQL exception during close.