Package com.logicaldoc.core

# Class NamedParameterStatement

java.lang.Object
com.logicaldoc.core.NamedParameterStatement

All Implemented Interfaces:
  `AutoCloseable`

public class NamedParameterStatement extends Object implements 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

Constructors
Constructor
Description
`NamedParameterStatement(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 Type
Method
Description
`void`
`addBatch()`
Adds the current set of parameters as a batch entry.
`void`
`close()`
Closes the statement.
`boolean`
`execute()`
Executes the statement.
`int[]`
`executeBatch()`
Executes all of the batched statements.
`ResultSet`
`executeQuery()`
Executes the statement, which must be a query.
`int`
`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.
`PreparedStatement`
`getStatement()`
Returns the underlying statement.
`void`
`setInt(String name, int value)`
Sets a parameter.
`void`
`setLong(String name, long value)`
Sets a parameter.
`void`
`setObject(String name, Object value)`
Sets a parameter.
`void`
`setParameters(Map<String,Object> parameters)`
`void`
`setString(String name, String value)`
Sets a parameter.
`void`
`setTimestamp(String name, Timestamp value)`
Sets a parameter.

### Methods inherited from class java.lang.Object

`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### NamedParameterStatement

public NamedParameterStatement(Connection connection, String query, Map<String,Object> parameters) throws SQLException
Creates a NamedParameterStatement. Wraps a call to `c.`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

### NamedParameterStatement

public NamedParameterStatement(Connection connection, String query, Map<String,Object> parameters, Integer maxRows) throws SQLException
Creates a NamedParameterStatement. Wraps a call to `c.`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

public void setParameters(Map<String,Object> parameters) throws SQLException

Throws:
  `SQLException`

### setObject

public void setObject(String name, Object value) throws SQLException
Sets a parameter.

Parameters:
  `name` - parameter name
  `value` - parameter value
Throws:
  `SQLException` - if an error occurred
  `IllegalArgumentException` - 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 name
  `value` - parameter value
Throws:
  `SQLException` - if an error occurred
  `IllegalArgumentException` - if the parameter does not exist
See Also:
  - `PreparedStatement.setString(int, java.lang.String)`

### setInt

public void setInt(String name, int value) throws SQLException
Sets a parameter.

Parameters:
  `name` - parameter name
  `value` - parameter value
Throws:
  `SQLException` - if an error occurred
  `IllegalArgumentException` - 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 name
  `value` - parameter value
Throws:
  `SQLException` - if an error occurred
  `IllegalArgumentException` - if the parameter does not exist
See Also:
  - `PreparedStatement.setInt(int, int)`

### setTimestamp

public void setTimestamp(String name, Timestamp value) throws SQLException
Sets a parameter.

Parameters:
  `name` - parameter name
  `value` - parameter value
Throws:
  `SQLException` - if an error occurred
  `IllegalArgumentException` - if the parameter does not exist
See Also:
  - `PreparedStatement.setTimestamp(int, java.sql.Timestamp)`

### getStatement

public PreparedStatement getStatement()
Returns the underlying statement.

Returns:
  the statement

### 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, which must be a query.

Returns:
  the query results
Throws:
  `SQLException` - if an error occurred
See Also:
  - `PreparedStatement.executeQuery()`

### executeUpdate

public int executeUpdate() throws SQLException
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:
  - `PreparedStatement.executeUpdate()`

### close

public void close() throws SQLException
Closes the statement.

Specified by:
  `close` in interface `AutoCloseable`
Throws:
  `SQLException` - if an error occurred
See Also:
  - `Statement.close()`

### addBatch

public void addBatch() throws SQLException
Adds the current set of parameters as a batch entry.

Throws:
  `SQLException` - if something went wrong

### executeBatch

public int[] executeBatch() throws SQLException
Executes all of the batched statements. See `Statement.executeBatch()` for details.

Returns:
  update counts for each statement
Throws:
  `SQLException` - if something went wrong