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 Summary
ConstructorDescriptionNamedParameterStatement
(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 Summary
Modifier and TypeMethodDescriptionvoid
addBatch()
Adds the current set of parameters as a batch entry.void
close()
Closes the statement.boolean
execute()
Executes the statement.int[]
Executes all of the batched statements.Executes the statement, which must be a query.int
Executes 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.void
Sets a parameter.void
Sets a parameter.void
Sets a parameter.void
setParameters
(Map<String, Object> parameters) void
Sets a parameter.void
setTimestamp
(String name, Timestamp value) Sets a parameter.
-
Constructor Details
-
NamedParameterStatement
public NamedParameterStatement(Connection connection, String query, Map<String, Object> parameters) throws SQLExceptionCreates a NamedParameterStatement. Wraps a call toc.
.prepareStatement
- Parameters:
connection
- the database connectionquery
- the parameterized queryparameters
- the map of parameter values- Throws:
SQLException
- if the statement could not be created
-
NamedParameterStatement
public NamedParameterStatement(Connection connection, String query, Map<String, Object> parameters, Integer maxRows) throws SQLExceptionCreates a NamedParameterStatement. Wraps a call toc.
.prepareStatement
- Parameters:
connection
- the database connectionquery
- the parameterized queryparameters
- the map of parameter valuesmaxRows
- maximum number of records to return- Throws:
SQLException
- if the statement could not be created
-
-
Method Details
-
setParameters
- Throws:
SQLException
-
setObject
Sets a parameter.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
-
setString
Sets a parameter.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
-
setInt
Sets a parameter.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
-
setLong
Sets a parameter.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
-
setTimestamp
Sets a parameter.- Parameters:
name
- parameter namevalue
- parameter value- Throws:
SQLException
- if an error occurredIllegalArgumentException
- if the parameter does not exist- See Also:
-
getStatement
Returns the underlying statement.- Returns:
- the statement
-
execute
Executes the statement.- Returns:
- true if the first result is a
ResultSet
- Throws:
SQLException
- if an error occurred- See Also:
-
executeQuery
Executes the statement, which must be a query.- Returns:
- the query results
- Throws:
SQLException
- if an error occurred- See Also:
-
executeUpdate
Executes 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:
-
close
Closes the statement.- Specified by:
close
in interfaceAutoCloseable
- Throws:
SQLException
- if an error occurred- See Also:
-
addBatch
Adds the current set of parameters as a batch entry.- Throws:
SQLException
- if something went wrong
-
executeBatch
Executes all of the batched statements. SeeStatement.executeBatch()
for details.- Returns:
- update counts for each statement
- Throws:
SQLException
- if something went wrong
-