Class NamedParameterStatement
- java.lang.Object
-
- com.identityworksllc.iiq.common.query.AbstractNamedParameterStatement<PreparedStatement>
-
- com.identityworksllc.iiq.common.query.NamedParameterStatement
-
- All Implemented Interfaces:
AutoCloseable
public final class NamedParameterStatement extends AbstractNamedParameterStatement<PreparedStatement>
Named parameter prepared statement wrapper, derived from various examples around the Internet.Rather than setting arguments by position, you can set them by name, the way Hibernate or certain JDBC drivers permit.
Tokens have the form
:name
and must be alphanumeric.The same tokens can be re-used more than once in your query.
NamedParameterStatement stmt = new NamedParameterStatement( connection, "select id from spt_identity where (extended3 = :username or name = :username) and extended4 = :department" ); stmt.setString("username", someUsername); stmt.setString("department", someDepartment); // Use the statement like any other
If you attempt to set a token that does not exist, such as
stmt.setString("xyz", str)
in the above example, the default behavior is to throw anIllegalArgumentException
. You can useAbstractNamedParameterStatement.setAllowMissingAttributes(boolean)
to disable this behavior.
-
-
Field Summary
-
Fields inherited from class com.identityworksllc.iiq.common.query.AbstractNamedParameterStatement
allowMissingAttributes, connection, indexMap, statement
-
-
Constructor Summary
Constructors Constructor Description NamedParameterStatement(Connection connection, String query)
Creates a NamedParameterStatement.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addBatch()
Adds the current row to the batchboolean
execute()
Executes the statement.ResultSet
executeQuery()
Executes the statement and returns a result setint
executeUpdate()
Executes the statement and returns the update countvoid
setArray(String name, List<String> value)
Sets a parameter to an array.void
setBlob(String name, byte[] blob)
Sets the given byte array as a Blob input to the statementvoid
setBlob(String name, Blob blob)
Sets the given byte array as a Blob input to the statementvoid
setClob(String name, Reader clob)
Sets a parameter to a clobvoid
setClob(String name, String clob)
Sets a parameter to a clobvoid
setClob(String name, Clob clob)
Sets a parameter to a clobvoid
setDate(String name, Date value)
Sets a parameter.void
setDate(String name, Instant value)
Sets a parameter to a Java time instant.void
setDate(String name, LocalDateTime value)
Sets a parameter to a Java time instant.void
setDate(String name, Date value)
Sets a parameter to a Java date.void
setDouble(String name, double value)
Sets a parameter.void
setInt(String name, int value)
Sets a parameter.void
setLong(String name, long value)
Sets a parameter.void
setNull(String name)
Sets a parameter to null, assuming the type to be VARCHAR.void
setNull(String name, int type)
Sets a typed parameter to nullvoid
setObject(String name, Object value)
Sets a parameter to the given object.void
setString(String name, String value)
Sets a parameter.void
setTimestamp(String name, Timestamp value)
Sets a parameter.void
setXml(String name, Object xmlObject)
Serializes the input object into XML and then adds it to the query as a CLOB.-
Methods inherited from class com.identityworksllc.iiq.common.query.AbstractNamedParameterStatement
close, executeBatch, getIndexes, getParameterNames, getStatement, isClosed, parse, setAllowMissingAttributes, setFetchDirection, setFetchSize, setMaxRows, setParameters, setQueryTimeout
-
-
-
-
Constructor Detail
-
NamedParameterStatement
public NamedParameterStatement(Connection connection, String query) throws SQLException
Creates a NamedParameterStatement.Wraps a call to
Connection.prepareStatement(java.lang.String)
.- Parameters:
connection
- the database connectionquery
- the parameterized query- Throws:
SQLException
- if the statement could not be created
-
-
Method Detail
-
addBatch
public void addBatch() throws SQLException
Adds the current row to the batch- Specified by:
addBatch
in classAbstractNamedParameterStatement<PreparedStatement>
- Throws:
SQLException
- The batch- See Also:
PreparedStatement.addBatch()
,PreparedStatement.addBatch()
-
execute
public boolean execute() throws SQLException
Executes the statement.- Returns:
- true if the first result is a
ResultSet
- Throws:
SQLException
- if an error occurred- See Also:
PreparedStatement.execute()
-
executeQuery
public ResultSet executeQuery() throws SQLException
Executes the statement and returns a result set- Returns:
- The result set
- Throws:
SQLException
- on errors
-
executeUpdate
public int executeUpdate() throws SQLException
Executes the statement and returns the update count- Returns:
- The update count
- Throws:
SQLException
- on errors
-
setArray
public void setArray(String name, List<String> value) throws SQLException
Sets a parameter to an array.Note that this is not supported in all database engines (particularly, not in MySQL)
- Parameters:
name
- The name of the fieldvalue
- The list to change to an array- Throws:
SQLException
- if a failure generating the array occurs
-
setBlob
public void setBlob(String name, Blob blob) throws SQLException
Sets the given byte array as a Blob input to the statement- Parameters:
name
- The parameter nameblob
- The blob- Throws:
SQLException
- on failures
-
setBlob
public void setBlob(String name, byte[] blob) throws SQLException
Sets the given byte array as a Blob input to the statement- Parameters:
name
- The parameter nameblob
- The blob- Throws:
SQLException
- on failures setting the blob or reading the byte array
-
setClob
public void setClob(String name, Reader clob) throws SQLException
Sets a parameter to a clob- Parameters:
name
- The named parameterclob
- The clob to set- Throws:
SQLException
- on failures
-
setClob
public void setClob(String name, String clob) throws SQLException
Sets a parameter to a clob- Parameters:
name
- The named parameterclob
- The clob to set- Throws:
SQLException
- on failures
-
setClob
public void setClob(String name, Clob clob) throws SQLException
Sets a parameter to a clob- Parameters:
name
- The named parameterclob
- The clob to set- Throws:
SQLException
- on failures
-
setDate
public void setDate(String name, LocalDateTime value) throws SQLException
Sets a parameter to a Java time instant.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
PreparedStatement.setDate(int, Date)
-
setDate
public void setDate(String name, Instant value) throws SQLException
Sets a parameter to a Java time instant.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
PreparedStatement.setDate(int, Date)
-
setDate
public void setDate(String name, Date value) throws SQLException
Sets a parameter to a Java date.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
PreparedStatement.setDate(int, Date)
-
setDate
public void setDate(String name, Date value) throws SQLException
Sets a parameter.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
PreparedStatement.setDate(int, Date)
-
setDouble
public void setDouble(String name, double value) throws SQLException
Sets a parameter.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
PreparedStatement.setDouble(int, double)
-
setInt
public void setInt(String name, int value) throws SQLException
Sets a parameter.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
PreparedStatement.setInt(int, int)
-
setLong
public void setLong(String name, long value) throws SQLException
Sets a parameter.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
PreparedStatement.setLong(int, long)
-
setNull
public void setNull(String name) throws SQLException
Sets a parameter to null, assuming the type to be VARCHAR.- Parameters:
name
- parameter name- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
PreparedStatement.setNull(int, int)
-
setNull
public void setNull(String name, int type) throws SQLException
Sets a typed parameter to null- Parameters:
name
- parameter nametype
- The SQL type of the argument to set- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
PreparedStatement.setNull(int, int)
-
setObject
public void setObject(String name, Object value) throws SQLException
Sets a parameter to the given object.If the object is a SailPointObject, the parameter will be set as to its ‘id’ as a string instead.
- Specified by:
setObject
in classAbstractNamedParameterStatement<PreparedStatement>
- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
PreparedStatement.setObject(int, java.lang.Object)
-
setString
public void setString(String name, String value) throws SQLException
Sets a parameter.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
PreparedStatement.setString(int, java.lang.String)
-
setTimestamp
public void setTimestamp(String name, Timestamp value) throws SQLException
Sets a parameter.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
PreparedStatement.setTimestamp(int, java.sql.Timestamp)
-
setXml
public void setXml(String name, Object xmlObject) throws SQLException
Serializes the input object into XML and then adds it to the query as a CLOB.- Parameters:
name
- The field namexmlObject
- The XML object- Throws:
SQLException
- if any failures occur
-
-