public class JdbcConnector extends Object implements AutoCloseable
Connection and PreparedStatement,
to be used in Repository classes. Reducing boilerplate code.
The Connection is a pooled Connection from DataSourceUtils class
SQLException will be converted to AccessDataBaseException
at this level: opening and closing connections, setting parameters.
It's not thread safe, and is expected to be created and then closed within a single method.
It implements AutoCloseable interface and could be used with try-with-resources statement
| Constructor and Description |
|---|
JdbcConnector(DataSource dataSource,
NativeSQLQueries query)
Creates
Connection and PreparedStatement objects from
given dataSource, using provided query. |
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Close underlying statement and release connection
|
long |
getLong(int resultIndex,
int columnIndex)
Gets long from resultIndex
ResultSet at
columnIndex position |
<T> T |
getObject(int resultIndex,
org.springframework.jdbc.core.RowMapper<T> mapper)
Retrieves an object from
ResultSet found in a list of results
at given index, using provided mapper |
<T> Set<T> |
getObjects(int resultIndex,
org.springframework.jdbc.core.RowMapper<T> mapper)
Retrieves a set of objects from
results list
at given index, using provided mapper |
List<ResultSet> |
getResults()
Execute statement and return a list with
ResultSet objects |
String |
getString(int resultIndex,
int columnIndex)
Gets String from resultIndex
ResultSet at
columnIndex position |
int |
getUpdateCount()
Returns the first update count after execution.
|
void |
setLong(int pos,
long num)
Set
Long number at specified position |
void |
setRequiredString(int pos,
String field,
String fieldName)
Set String which is required for current statement at given position.
|
void |
setString(int pos,
String field)
Set String that can be null or empty to current statement at given position.
|
public JdbcConnector(DataSource dataSource, NativeSQLQueries query)
Connection and PreparedStatement objects from
given dataSource, using provided query.
Don't forget to call close() method in the end.
dataSource - the DataSource to be usedquery - the NativeSQLQueries queryAccessDataBaseException - if open connection and prepare statement
operations were not successfulpublic void close()
close in interface AutoCloseableAccessDataBaseException - if closing operations were not successfulpublic int getUpdateCount()
This method won't get or cache any results
(i.e. Result Sets are not expected). Use getResults()
method instead if you need them.
AccessDataBaseException - translates SQLExceptionsStatement.getUpdateCount()public List<ResultSet> getResults()
ResultSet objectsRAM)
or empty list if there were Update Counts onlyAccessDataBaseException - translates SQLExceptionspublic long getLong(int resultIndex,
int columnIndex)
ResultSet at
columnIndex positionresultIndex - The results list index (java - 0 based)columnIndex - The column number (sql - 1 based)AccessDataBaseException - translates SQLExceptions,
and 'out of bound' errors will also fall into this categorypublic String getString(int resultIndex, int columnIndex)
ResultSet at
columnIndex positionresultIndex - The results list index (java - 0 based)columnIndex - The column number (sql - 1 based)AccessDataBaseException - translates SQLExceptions,
and 'out of bound' errors will also fall into this categorypublic <T> T getObject(int resultIndex,
org.springframework.jdbc.core.RowMapper<T> mapper)
ResultSet found in a list of results
at given index, using provided mapperT - Entity type is expected (like Role or User)resultIndex - list index (0 if expecting single result)mapper - the mapper to be used for objects creationAccessDataBaseException - translates SQLExceptions,
and 'out of bound' errors will also fall into this categorypublic <T> Set<T> getObjects(int resultIndex, org.springframework.jdbc.core.RowMapper<T> mapper)
results list
at given index, using provided mapperT - Entity type is expected (like Role or User)resultIndex - list index (0 if expecting single result)mapper - the mapper to be used for objects creationAccessDataBaseException - translates SQLExceptions,
and 'out of bound' errors will also fall into this categorypublic void setLong(int pos,
long num)
Long number at specified positionpos - The position is SQL 1-based indexnum - a number to setAccessDataBaseException - translates
PreparedStatement.setLong(int, long) exceptionpublic void setString(int pos,
String field)
pos - The position is SQL 1-based indexfield - the actual String to be setAccessDataBaseException - translates
PreparedStatement.setString(int, String) exceptionpublic void setRequiredString(int pos,
String field,
String fieldName)
pos - The position is SQL 1-based indexfield - the actual String to be setfieldName - field name (for logging and error messages)IllegalArgumentException - if field is null or empty stringAccessDataBaseException - translates
PreparedStatement.setString(int, String) exception