public class Base extends Object
DB
such that
in this class you a logical name for a connection is hard-coded to be "default". Use this class when you have
only one database.
This class is a convenience wrapper of DB
Modifier and Type | Method and Description |
---|---|
static void |
addBatch(PreparedStatement ps,
Object... parameters)
Adds a batch statement using given
java.sql.PreparedStatement and parameters. |
static void |
attach(Connection connection)
Attaches a database connection to current thread under a default name.
|
static void |
close()
Closes connection and detaches it from current thread.
|
static void |
close(boolean suppressWarning)
Closes connection and detaches it from current thread.
|
static void |
closePreparedStatement(PreparedStatement ps)
Quietly closes the
java.sql.PreparedStatement used in a batch execution. |
static void |
commitTransaction()
Commits local transaction.
|
static Connection |
connection()
Returns connection attached to a current thread and named "default".
|
static Long |
count(String table)
Returns count of rows in table.
|
static Long |
count(String table,
String query,
Object... params)
Runs a count query, returns a number of matching records.
|
static Connection |
detach()
Detaches a default connection from current thread and returns an instance of it.
|
static int |
exec(String query)
Executes DML.
|
static int |
exec(String query,
Object... params)
Executes parametrized DML - will contain question marks as placeholders.
|
static int[] |
executeBatch(PreparedStatement ps)
Executes a batch on
java.sql.PreparedStatement . |
static RowProcessor |
find(RowProcessor.ResultSetType type,
RowProcessor.ResultSetConcur concur,
int fetchSize,
String query,
Object... params)
Executes a raw query and returns an instance of
RowProcessor . |
static RowProcessor |
find(String query,
Object... params)
Executes a raw query and returns an instance of
RowProcessor . |
static void |
find(String sql,
RowListener listener)
Executes a raw query and calls instance of
RowListener with every row found. |
static List<Map> |
findAll(String query)
This method returns entire resultset as one list.
|
static List<Map> |
findAll(String query,
Object... params)
This method returns entire resultset as one list.
|
static Object |
firstCell(String query,
Object... params)
Returns a value of the first column of the first row.
|
static List |
firstColumn(String query,
Object... params)
This method returns entire resultset as one list.
|
static boolean |
hasConnection()
Use to check if there is a default connection present on current thread.
|
static DB |
open()
This method will open a connection defined in the file 'database.properties' set by an initial previous call to
DBConfiguration.loadConfiguration(String) . |
static DB |
open(DataSource dataSource)
Opens a connection from a datasource.
|
static DB |
open(String jndiName)
Opens a connection from JNDI based on a registered name.
|
static DB |
open(String jndiName,
Properties jndiProperties)
Opens a new connection from JNDI data source by name using explicit JNDI properties.
|
static DB |
open(String driver,
String url,
Properties props)
Opens a new connection in case additional driver-specific parameters need to be passed in.
|
static DB |
open(String driver,
String url,
String user,
String password)
Opens a new connection based on JDBC properties and attaches it to a current thread.
|
static void |
openTransaction()
Opens local transaction.
|
static void |
rollbackTransaction()
Rolls back local transaction.
|
static PreparedStatement |
startBatch(String parametrizedStatement)
Creates a
java.sql.PreparedStatement to be used in batch executions later. |
static <T> T |
withDb(DataSource dataSource,
Supplier<T> supplier)
Same as
DB.withDb(DataSource, Supplier) , but with db name DB.DEFAULT_NAME . |
static <T> T |
withDb(String jndiName,
Properties jndiProperties,
Supplier<T> supplier)
Same as
DB.withDb(String, Properties, Supplier) , but with db name DB.DEFAULT_NAME . |
static <T> T |
withDb(String driver,
String url,
Properties properties,
Supplier<T> supplier)
Same as
DB.withDb(String, Properties, Supplier) , but with db name DB.DEFAULT_NAME . |
static <T> T |
withDb(String driver,
String url,
String user,
String password,
Supplier<T> supplier)
Same as
DB.withDb(String, String, String, String, Supplier) , but with db name DB.DEFAULT_NAME . |
static <T> T |
withDb(String jndiName,
Supplier<T> supplier)
Same as
DB.withDb(String, Supplier) , but with db name DB.DEFAULT_NAME . |
static <T> T |
withDb(Supplier<T> supplier)
Same as
DB.withDb(Supplier) , but with db name DB.DEFAULT_NAME . |
public static DB open()
DBConfiguration.loadConfiguration(String)
.
The connection picked up from the file is defined by ACTIVE_ENV
environment variable or active_env
system property.
If this variable is not defined, it defaults to 'development' environment.
If there is JUnit on classpath, this method assumes it is running under test, and defaults to 'test'.public static DB open(String driver, String url, String user, String password)
driver
- class name of driverurl
- URL connection to DBuser
- user name.password
- password.public static DB open(String driver, String url, Properties props)
driver
- driver class nameurl
- JDBC URLprops
- connection propertiespublic static DB open(String jndiName)
jndi.properties
file with proper JNDI configuration in it.jndiName
- name of a configured data source.public static DB open(String jndiName, Properties jndiProperties)
jndi.properties
cannot be easily modified.jndiName
- name of JNDI data source.jndiProperties
- JNDI propertiespublic static DB open(DataSource dataSource)
dataSource
- datasource will be used to acquire a connection.public static Connection connection()
public static boolean hasConnection()
public static void close(boolean suppressWarning)
suppressWarning
- true to not display a warning in case of a problem (connection not there)public static void close()
public static Long count(String table)
table
- name of table.public static Long count(String table, String query, Object... params)
table
- table in which to count rows.query
- this is a filtering query for the count. If '*' provided, all records will be counted. Example:
"age > 65 AND department = 'accounting'"
params
- parameters for placeholder substitution.public static Object firstCell(String query, Object... params)
IllegalArgumentException
.query
- queryparams
- parameterspublic static List<Map> findAll(String query, Object... params)
List<Map> people = Base.findAll("select * from people where first_name = ?", "John"); for (Map person : people) System.out.println(person.get("first_name"));
query
- raw SQL query. This query is parametrized.params
- list of parameters for a parametrized query.public static List firstColumn(String query, Object... params)
List ssns = Base.firstColumn("select ssn from people where first_name = ?", "John"); for(Object ssn: ssns) System.out.println(ssn);This methods expects a query which selects only one column from a table/view. It will throw an exception if more than one columns are fetched in a result set.
query
- raw SQL query. This query is parametrized.params
- list of parameters for a parametrized query.public static List<Map> findAll(String query)
query
- raw SQL query. This query is not parametrized.public static RowProcessor find(String query, Object... params)
RowProcessor
. Use it in the following pattern:
Base.find("select first_name, last_name from really_large_table").with(new RowListenerAdapter() { public void onNext(Map row) { ///write your code here Object o1 = row.get("first_name"); Object o2 = row.get("last_name"); } });
query
- raw SQL.params
- list of parameters if query is parametrized.RowProcessor
which has with() method for convenience.public static RowProcessor find(RowProcessor.ResultSetType type, RowProcessor.ResultSetConcur concur, int fetchSize, String query, Object... params)
RowProcessor
. Use it in the following pattern:
Base.find("select first_name, last_name from really_large_table", ....).with(new RowListenerAdapter() { public void onNext(Map row) { ///write your code here Object o1 = row.get("first_name"); Object o2 = row.get("last_name"); } });See ResultSet Docs
query
- raw SQL.type
- type of result setconcur
- concurrent mode of result setfetchSize
- size of result setparams
- list of parameters if query is parametrized.RowProcessor
which has with() method for convenience.public static void find(String sql, RowListener listener)
RowListener
with every row found.
Use this method for very large result sets.sql
- raw SQL query.listener
- client listener implementation for processing individual rows.public static int exec(String query)
query
- raw DML.public static int exec(String query, Object... params)
query
- query to execute - will contain question marks as placeholders.params
- query parameters.public static void openTransaction()
public static void commitTransaction()
public static void rollbackTransaction()
public static PreparedStatement startBatch(String parametrizedStatement)
java.sql.PreparedStatement
to be used in batch executions later.parametrizedStatement
- Example of a statement: INSERT INTO employees VALUES (?, ?)
.java.sql.PreparedStatement
with compiled query.public static void addBatch(PreparedStatement ps, Object... parameters)
java.sql.PreparedStatement
and parameters.ps
- java.sql.PreparedStatement
to add batch to.parameters
- parameters for the query in java.sql.PreparedStatement
. Parameters will be
set on the statement in the same order as provided here.public static int[] executeBatch(PreparedStatement ps)
java.sql.PreparedStatement
.ps
- java.sql.PreparedStatement
to execute batch on.public static void closePreparedStatement(PreparedStatement ps)
java.sql.PreparedStatement
used in a batch execution. The advantage over calling
java.sql.PreparedStatement.close()
directly is not having to explicitly handle a checked exception
(java.sql.SQLException
).
This method should typically be called in a finally block. So as not to displace any exception (e.g. from a failed
batch execution) that might already be in flight, this method swallows any exception that might arise from
closing the statement. This is generally seen as a worthwhile trade-off, as it much less likely for a close to fail
without a prior failure.ps
- java.sql.PreparedStatement
with which a batch has been executed. If null, this is a no-op.public static void attach(Connection connection)
connection
- instance of connection to attach to current thread.public static Connection detach()
public static <T> T withDb(String jndiName, Properties jndiProperties, Supplier<T> supplier)
DB.withDb(String, Properties, Supplier)
, but with db name DB.DEFAULT_NAME
.public static <T> T withDb(DataSource dataSource, Supplier<T> supplier)
DB.withDb(DataSource, Supplier)
, but with db name DB.DEFAULT_NAME
.public static <T> T withDb(String jndiName, Supplier<T> supplier)
DB.withDb(String, Supplier)
, but with db name DB.DEFAULT_NAME
.public static <T> T withDb(String driver, String url, Properties properties, Supplier<T> supplier)
DB.withDb(String, Properties, Supplier)
, but with db name DB.DEFAULT_NAME
.public static <T> T withDb(String driver, String url, String user, String password, Supplier<T> supplier)
DB.withDb(String, String, String, String, Supplier)
, but with db name DB.DEFAULT_NAME
.public static <T> T withDb(Supplier<T> supplier)
DB.withDb(Supplier)
, but with db name DB.DEFAULT_NAME
.Copyright © 2020 JavaLite. All rights reserved.