Package org.passay.dictionary
Class JDBCDictionary
- java.lang.Object
-
- org.passay.dictionary.JDBCDictionary
-
- All Implemented Interfaces:
Dictionary
public class JDBCDictionary extends Object implements Dictionary
Provides aDictionary
backed by a database table.- Author:
- Middleware Services
-
-
Field Summary
Fields Modifier and Type Field Description protected DataSource
dataSource
JDBC data source.protected String
searchStatement
SQL search statement.protected String
sizeStatement
SQL size statement.
-
Constructor Summary
Constructors Constructor Description JDBCDictionary(DataSource source, String searchSql, String sizeSql)
Creates a new JDBC dictionary.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T> T
executeStatement(String statement, Class<T> type, Object... params)
Executes a prepared statement against the database.protected Connection
getConnection()
Returns a connection that is ready for use.boolean
search(String word)
Returns whether the supplied word exists in the dictionary.long
size()
Returns the number of words in this dictionary
-
-
-
Field Detail
-
dataSource
protected DataSource dataSource
JDBC data source.
-
searchStatement
protected String searchStatement
SQL search statement.
-
sizeStatement
protected String sizeStatement
SQL size statement.
-
-
Constructor Detail
-
JDBCDictionary
public JDBCDictionary(DataSource source, String searchSql, String sizeSql)
Creates a new JDBC dictionary.- Parameters:
source
- connection data sourcesearchSql
- prepared statement to query for words; first parameter is the wordsizeSql
- prepared statement to query for size; no parameters are provided
-
-
Method Detail
-
search
public boolean search(String word)
Description copied from interface:Dictionary
Returns whether the supplied word exists in the dictionary.- Specified by:
search
in interfaceDictionary
- Parameters:
word
- to search for- Returns:
- whether word was found
-
size
public long size()
Description copied from interface:Dictionary
Returns the number of words in this dictionary- Specified by:
size
in interfaceDictionary
- Returns:
- total number of words to search
-
executeStatement
protected <T> T executeStatement(String statement, Class<T> type, Object... params) throws SQLException
Executes a prepared statement against the database. Only the first result is returned.- Type Parameters:
T
- return type- Parameters:
statement
- to executetype
- of object to returnparams
- to set on the prepared statement- Returns:
- first result of the prepared statement
- Throws:
SQLException
- if the statement execution fails
-
getConnection
protected Connection getConnection() throws SQLException
Returns a connection that is ready for use.- Returns:
- database connection.
- Throws:
SQLException
- if a connection is not available
-
-