|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.javaranch.db.DBFacade
Abstract out all or most of SQL and JDBC to centralize database access, reduce code redundancy and facilitate unit testing. You will probably want to create only one of these for each database you will access.
Constructor Summary | |
DBFacade(java.lang.String jndiLookup)
Instantiate a DBFacade object where all database connections will be based on a JNDI lookup. |
|
DBFacade(java.lang.String databaseDriver,
java.lang.String databaseURL)
Instantiate a DBFacade object where all database connections will be based on loading a databaseDriver and accessing a database URL. |
|
DBFacade(java.lang.String driver,
java.lang.String databaseURL,
java.lang.String databaseName,
java.lang.String databasePassword)
Instantiate a DBFacade object where all database connections will be based on loading a driver and accessing a database URL. |
Method Summary | |
void |
delete(java.sql.Connection con,
DBQuery q)
Delete all rows matching the search portion of the DBQuery object. |
void |
delete(java.sql.Connection con,
java.lang.String table,
java.lang.String searchColumn,
java.lang.String searchText)
Delete all rows where the searchText matches the text found in column. |
void |
delete(DBQuery q)
|
void |
delete(java.lang.String table,
java.lang.String searchColumn,
java.lang.String searchText)
Delete all rows where the searchText matches the text found in column. |
DBResults |
executeQuery(java.sql.Connection con,
java.lang.String sql)
Sometimes you just gotta do it in SQL. |
java.sql.Connection |
getConnection()
Return a database connection. |
void |
insert(java.sql.Connection con,
java.lang.String table,
java.util.Map data)
Create a new row in the table and populate the fields with the provided data. |
void |
insert(java.lang.String table,
java.util.Map data)
Create a new row in the table and populate the fields with the provided data. |
long |
insertAndGetID(java.sql.Connection con,
java.lang.String table,
java.util.Map data)
|
long |
insertAndGetID(java.sql.Connection con,
java.lang.String table,
java.util.Map data,
java.lang.String idColumnName)
|
long |
insertAndGetID(java.lang.String table,
java.util.Map data)
Create a new row with a unique ID. |
long |
insertAndGetID(java.lang.String table,
java.util.Map data,
java.lang.String idColumnName)
|
static java.lang.String |
normalizeSearchText(java.lang.String searchText)
Converts a string to be used for SQL. |
DBResults |
search(java.sql.Connection con,
DBQuery q)
Use the DBQuery object to do a search. |
DBResults |
search(java.sql.Connection con,
java.lang.String table,
java.lang.String searchColumn,
int[] searchValues,
java.lang.String[] dataColumns)
Find all the data in a table where the text in a column exactly matches one of these strings. |
DBResults |
search(java.sql.Connection con,
java.lang.String table,
java.lang.String searchColumn,
int searchValue,
java.lang.String[] dataColumns)
Find all the data in a table where the text in a column exactly matches a string. |
DBResults |
search(java.sql.Connection con,
java.lang.String table,
java.lang.String whereClause,
java.lang.String[] dataColumns)
Find all the data in a table that matches up to this SQL where clause. |
DBResults |
search(java.sql.Connection con,
java.lang.String table,
java.lang.String searchColumn,
java.lang.String[] searchText,
java.lang.String[] dataColumns)
Find all the data in a table where the text in a column exactly matches one of these strings. |
DBResults |
search(java.sql.Connection con,
java.lang.String table,
java.lang.String searchColumn,
java.lang.String searchText,
java.lang.String[] dataColumns)
Find all the data in a table where the text in a column exactly matches a string. |
DBResults |
search(DBQuery q)
Use the DBQuery object to do a search. |
DBResults |
search(java.lang.String table,
java.lang.String searchColumn,
int[] searchValues,
java.lang.String[] dataColumns)
Find all the data in a table where the text in a column exactly matches one of these strings. |
DBResults |
search(java.lang.String table,
java.lang.String searchColumn,
int searchValue,
java.lang.String[] dataColumns)
Find all the data in a table where the text in a column exactly matches a string. |
DBResults |
search(java.lang.String table,
java.lang.String whereClause,
java.lang.String[] dataColumns)
Find all the data in a table that matches up to this SQL where clause. |
DBResults |
search(java.lang.String table,
java.lang.String searchColumn,
java.lang.String[] searchText,
java.lang.String[] dataColumns)
Find all the data in a table where the text in a column exactly matches one of these strings. |
DBResults |
search(java.lang.String table,
java.lang.String searchColumn,
java.lang.String searchText,
java.lang.String[] dataColumns)
Find all the data in a table where the text in a column exactly matches a string. |
void |
testConnection()
Establish a connection to the database to see if any exceptions might be generated. |
void |
update(java.sql.Connection con,
java.lang.String table,
java.lang.String whereClause,
java.util.Map data)
Update all rows matching this where clause. |
void |
update(java.sql.Connection con,
java.lang.String table,
java.lang.String searchColumn,
java.lang.String[] searchText,
java.util.Map data)
Update all rows where the text in a searchColumn exactly matches one of these strings. |
void |
update(java.sql.Connection con,
java.lang.String table,
java.lang.String searchColumn,
java.lang.String searchText,
java.util.Map data)
Update all rows where the searchText matches the text found in searchColumn. |
void |
update(java.lang.String table,
java.lang.String whereClause,
java.util.Map data)
Update all rows matching this where clause. |
void |
update(java.lang.String table,
java.lang.String searchColumn,
java.lang.String[] searchText,
java.util.Map data)
Update all rows where the text in a searchColumn exactly matches one of these strings. |
void |
update(java.lang.String table,
java.lang.String searchColumn,
java.lang.String searchText,
java.util.Map data)
Update all rows where the searchText matches the text found in column. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public DBFacade(java.lang.String jndiLookup)
jndiLookup
- the JNDI url, i.e. "java:comp/env/jdbc/DataSource"public DBFacade(java.lang.String databaseDriver, java.lang.String databaseURL)
databaseDriver
- The name of a class that will be loaded. i.e. "com.mysql.jdbc.Driver" or "com.inet.tds.TdsDriver"databaseURL
- Where the database can be found. i.e. "jdbc:databaseDriver://hostname/databasename" or "jdbc:mysql://localhost/soup"public DBFacade(java.lang.String driver, java.lang.String databaseURL, java.lang.String databaseName, java.lang.String databasePassword)
driver
- The name of a class that will be loaded. i.e. "com.mysql.jdbc.Driver" or "com.inet.tds.TdsDriver"databaseURL
- Where the database can be found. i.e. "jdbc:driver://hostname/databasename" or "jdbc:mysql://localhost/soup"databaseName
- The user name that has access to the database.databasePassword
- The password for the user that has access to the database.Method Detail |
public java.sql.Connection getConnection() throws java.lang.Exception
java.lang.Exception
public void testConnection() throws java.lang.Exception
java.lang.Exception
- The types of exceptions thrown here can change depending the type of database access.public DBResults executeQuery(java.sql.Connection con, java.lang.String sql) throws java.sql.SQLException
con
- A database connection.sql
- A complete SQL statement
java.sql.SQLException
public DBResults search(java.sql.Connection con, DBQuery q) throws java.sql.SQLException
con
- A database connection. q
- A predescribed query - see DBQuery for details.
java.sql.SQLException
public DBResults search(java.sql.Connection con, java.lang.String table, java.lang.String whereClause, java.lang.String[] dataColumns) throws java.sql.SQLException
whereClause
- The SQL to use in selecting the rows - do not say "WHERE". dataColumns
- The column names that have the data you want.
java.sql.SQLException
public DBResults search(java.lang.String table, java.lang.String whereClause, java.lang.String[] dataColumns) throws java.sql.SQLException
whereClause
- The SQL to use in selecting the rows - do not say "WHERE". dataColumns
- The column names that have the data you want.
java.sql.SQLException
public DBResults search(java.sql.Connection con, java.lang.String table, java.lang.String searchColumn, java.lang.String searchText, java.lang.String[] dataColumns) throws java.sql.SQLException
dataColumns
- The searchColumn names that have the data you want.
java.sql.SQLException
public DBResults search(java.sql.Connection con, java.lang.String table, java.lang.String searchColumn, java.lang.String[] searchText, java.lang.String[] dataColumns) throws java.sql.SQLException
dataColumns
- The searchColumn names that have the data you want.
java.sql.SQLException
public DBResults search(java.sql.Connection con, java.lang.String table, java.lang.String searchColumn, int searchValue, java.lang.String[] dataColumns) throws java.sql.SQLException
dataColumns
- The searchColumn names that have the data you want.
java.sql.SQLException
public DBResults search(java.sql.Connection con, java.lang.String table, java.lang.String searchColumn, int[] searchValues, java.lang.String[] dataColumns) throws java.sql.SQLException
dataColumns
- The searchColumn names that have the data you want.
java.sql.SQLException
public DBResults search(DBQuery q) throws java.sql.SQLException
java.sql.SQLException
public DBResults search(java.lang.String table, java.lang.String searchColumn, java.lang.String searchText, java.lang.String[] dataColumns) throws java.sql.SQLException
dataColumns
- The searchColumn names that have the data you want.
java.sql.SQLException
public DBResults search(java.lang.String table, java.lang.String searchColumn, java.lang.String[] searchText, java.lang.String[] dataColumns) throws java.sql.SQLException
dataColumns
- The searchColumn names that have the data you want.
java.sql.SQLException
public DBResults search(java.lang.String table, java.lang.String searchColumn, int searchValue, java.lang.String[] dataColumns) throws java.sql.SQLException
dataColumns
- The searchColumn names that have the data you want.
java.sql.SQLException
public DBResults search(java.lang.String table, java.lang.String searchColumn, int[] searchValues, java.lang.String[] dataColumns) throws java.sql.SQLException
dataColumns
- The searchColumn names that have the data you want.
java.sql.SQLException
public void update(java.lang.String table, java.lang.String searchColumn, java.lang.String searchText, java.util.Map data) throws java.sql.SQLException
data
- A key-value collection of searchColumn names (key) and data (value). If a searchColumn name starts with ">", that searchColumn will be processed as a stream (good for big Strings).
java.sql.SQLException
public void update(java.sql.Connection con, java.lang.String table, java.lang.String searchColumn, java.lang.String searchText, java.util.Map data) throws java.sql.SQLException
data
- A key-value collection of searchColumn names (key) and data (value). If a searchColumn name starts with ">", that searchColumn will be processed as a stream (good for big Strings).
java.sql.SQLException
public void update(java.lang.String table, java.lang.String searchColumn, java.lang.String[] searchText, java.util.Map data) throws java.sql.SQLException
data
- A key-value collection of searchColumn names (key) and data (value). If a searchColumn name starts with ">", that searchColumn will be processed as a stream (good for big Strings).
java.sql.SQLException
public void update(java.sql.Connection con, java.lang.String table, java.lang.String searchColumn, java.lang.String[] searchText, java.util.Map data) throws java.sql.SQLException
data
- A key-value collection of searchColumn names (key) and data (value). If a searchColumn name starts with ">", that searchColumn will be processed as a stream (good for big Strings).
java.sql.SQLException
public void update(java.lang.String table, java.lang.String whereClause, java.util.Map data) throws java.sql.SQLException
whereClause
- The SQL to use in selecting the rows - do not say "WHERE".data
- A key-value collection of searchColumn names (key) and data (value). If a searchColumn name starts with ">", that searchColumn will be processed as a stream (good for big Strings).
java.sql.SQLException
public void update(java.sql.Connection con, java.lang.String table, java.lang.String whereClause, java.util.Map data) throws java.sql.SQLException
whereClause
- The SQL to use in selecting the rows - do not say "WHERE".data
- A key-value collection of searchColumn names (key) and data (value). If a searchColumn name starts with ">", that searchColumn will be processed as a stream (good for big Strings).
java.sql.SQLException
public static java.lang.String normalizeSearchText(java.lang.String searchText)
public void delete(java.sql.Connection con, DBQuery q) throws java.sql.SQLException
java.sql.SQLException
public void delete(DBQuery q) throws java.sql.SQLException
java.sql.SQLException
public void delete(java.sql.Connection con, java.lang.String table, java.lang.String searchColumn, java.lang.String searchText) throws java.sql.SQLException
java.sql.SQLException
public void delete(java.lang.String table, java.lang.String searchColumn, java.lang.String searchText) throws java.sql.SQLException
java.sql.SQLException
public void insert(java.sql.Connection con, java.lang.String table, java.util.Map data) throws java.sql.SQLException
data
- A key-value collection of searchColumn names (key) and data (value). If a searchColumn name starts with ">", that searchColumn will be processed as a stream (good for big Strings).
java.sql.SQLException
public void insert(java.lang.String table, java.util.Map data) throws java.sql.SQLException
data
- A key-value collection of searchColumn names (key) and data (value). If a searchColumn name starts with ">", that searchColumn will be processed as a stream (good for big Strings).
java.sql.SQLException
public long insertAndGetID(java.lang.String table, java.util.Map data) throws java.sql.SQLException
Requires that a table exists called "TableIDCounters" complete with a row representing the table you specified in getPKTable(). "TableIDCounters" must have three columns: "tableName", "counter" and "skip". The name in the "tableName" row must be an exact match to the table string you provide as the first parameter. Make sure that before you call this method for the first time, that you initialize the "counter" field for the respective row to be zero. "skip" represents how often the counter should go to the database for a new "high" value (more on the high/low algorithm below). If you're not sure what skip value to use, use 100. Higher numbers will give higher performance, but will use up your numbers faster.
This method uses a high/low algorithm to insure unique numbers despite the complexity of a distributed system. With a skip value of 100, the algorithm will retrieve the high value from the database. If the high value is, say, 22, the number 2200 will be returned. On the next request, 2201 will be returned without a database call. Subsequent calls will return 2202 through 2209. The next request will force another database call which may return 27 (other servers in the server group may also be using this algorithm) - so the value 2700 is returned.
The ID column name is assumed to be "ID".
data
- A key-value collection of column names (key) and data (value). If a column name starts with ">",
that column will be processed as a stream (good for big Strings).
java.sql.SQLException
public long insertAndGetID(java.sql.Connection con, java.lang.String table, java.util.Map data) throws java.sql.SQLException
java.sql.SQLException
public long insertAndGetID(java.lang.String table, java.util.Map data, java.lang.String idColumnName) throws java.sql.SQLException
java.sql.SQLException
public long insertAndGetID(java.sql.Connection con, java.lang.String table, java.util.Map data, java.lang.String idColumnName) throws java.sql.SQLException
java.sql.SQLException
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |