Package com.logicaldoc.core
Class NamedParameterStatement
java.lang.Object
com.logicaldoc.core.NamedParameterStatement
- All Implemented Interfaces:
- AutoCloseable
This class wraps around a 
PreparedStatement and allows the programmer
 to set parameters by name instead of by index. This eliminates any confusion
 as to which parameter index represents what. This also means that rearranging
 the SQL statement or adding a parameter doesn't involve renumbering your
 indices. Code such as this:
 
 Connection con=getConnection(); String query="select * from my_table where
 name=? or address=?"; PreparedStatement p=con.prepareStatement(query);
 p.setString(1, "bob"); p.setString(2, "123 terrace ct"); ResultSet
 rs=p.executeQuery();
 
 can be replaced with:
 
 Connection con=getConnection(); String query="select * from my_table where
 name=:name or address=:address"; NamedParameterStatement p=new
 NamedParameterStatement(con, query); p.setString("name", "bob");
 p.setString("address", "123 terrace ct"); ResultSet rs=p.executeQuery();- 
Constructor SummaryConstructorsConstructorDescriptionNamedParameterStatement(Connection connection, String query, Map<String, Object> parameters) Creates a NamedParameterStatement.NamedParameterStatement(Connection connection, String query, Map<String, Object> parameters, Integer maxRows) Creates a NamedParameterStatement.
- 
Method SummaryModifier and TypeMethodDescriptionvoidaddBatch()Adds the current set of parameters as a batch entry.voidclose()Closes the statement.booleanexecute()Executes the statement.int[]Executes all of the batched statements.Executes the statement, which must be a query.intExecutes the statement, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement that returns nothing, such as a DDL statement.Returns the underlying statement.voidSets a parameter.voidSets a parameter.voidSets a parameter.voidsetParameters(Map<String, Object> parameters) voidSets a parameter.voidsetTimestamp(String name, Timestamp value) Sets a parameter.
- 
Constructor Details- 
NamedParameterStatementpublic NamedParameterStatement(Connection connection, String query, Map<String, Object> parameters) throws SQLExceptionCreates a NamedParameterStatement. Wraps a call toc..prepareStatement- Parameters:
- connection- the database connection
- query- the parameterized query
- parameters- the map of parameter values
- Throws:
- SQLException- if the statement could not be created
 
- 
NamedParameterStatementpublic NamedParameterStatement(Connection connection, String query, Map<String, Object> parameters, Integer maxRows) throws SQLExceptionCreates a NamedParameterStatement. Wraps a call toc..prepareStatement- Parameters:
- connection- the database connection
- query- the parameterized query
- parameters- the map of parameter values
- maxRows- maximum number of records to return
- Throws:
- SQLException- if the statement could not be created
 
 
- 
- 
Method Details- 
setParameters- Throws:
- SQLException
 
- 
setObjectSets a parameter.- Parameters:
- name- parameter name
- value- parameter value
- Throws:
- SQLException- if an error occurred
- IllegalArgumentException- if the parameter does not exist
- See Also:
 
- 
setStringSets a parameter.- Parameters:
- name- parameter name
- value- parameter value
- Throws:
- SQLException- if an error occurred
- IllegalArgumentException- if the parameter does not exist
- See Also:
 
- 
setIntSets a parameter.- Parameters:
- name- parameter name
- value- parameter value
- Throws:
- SQLException- if an error occurred
- IllegalArgumentException- if the parameter does not exist
- See Also:
 
- 
setLongSets a parameter.- Parameters:
- name- parameter name
- value- parameter value
- Throws:
- SQLException- if an error occurred
- IllegalArgumentException- if the parameter does not exist
- See Also:
 
- 
setTimestampSets a parameter.- Parameters:
- name- parameter name
- value- parameter value
- Throws:
- SQLException- if an error occurred
- IllegalArgumentException- if the parameter does not exist
- See Also:
 
- 
getStatementReturns the underlying statement.- Returns:
- the statement
 
- 
executeExecutes the statement.- Returns:
- true if the first result is a ResultSet
- Throws:
- SQLException- if an error occurred
- See Also:
 
- 
executeQueryExecutes the statement, which must be a query.- Returns:
- the query results
- Throws:
- SQLException- if an error occurred
- See Also:
 
- 
executeUpdateExecutes the statement, which must be an SQL INSERT, UPDATE or DELETE statement; or an SQL statement that returns nothing, such as a DDL statement.- Returns:
- number of rows affected
- Throws:
- SQLException- if an error occurred
- See Also:
 
- 
closeCloses the statement.- Specified by:
- closein interface- AutoCloseable
- Throws:
- SQLException- if an error occurred
- See Also:
 
- 
addBatchAdds the current set of parameters as a batch entry.- Throws:
- SQLException- if something went wrong
 
- 
executeBatchExecutes all of the batched statements. SeeStatement.executeBatch()for details.- Returns:
- update counts for each statement
- Throws:
- SQLException- if something went wrong
 
 
-