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 |
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(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 void |
open()
This method will open a connection defined in the file 'database.properties' located at
root of classpath.
|
static void |
open(DataSource dataSource)
Opens a connection from a datasource.
|
static void |
open(String jndiName)
Opens a connection from JNDI based on a registered name.
|
static void |
open(String jndiName,
Properties jndiProperties)
Opens a new connection from JNDI data source by name using explicit JNDI properties.
|
static void |
open(String driver,
String url,
Properties props)
Opens a new connection in case additional driver-specific parameters need to be passed in.
|
static void |
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. |
public static void open()
ACTIVE_ENV
environment variable. 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 void open(String driver, String url, String user, String password)
driver
- class name of driverurl
- URL connection to DBuser
- user name.password
- password.public static void open(String driver, String url, Properties props)
driver
- driver class nameurl
- JDBC URLprops
- connection propertiespublic static void open(String jndiName)
jndi.properties
file with proper JNDI configuration in it.jndiName
- name of a configured data source.public static void open(String jndiName, Properties jndiProperties)
jndi.properties
cannot be easily modified.jndiName
- name of JNDI data source.jndiProperties
- JNDI propertiespublic static void 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 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 attach(Connection connection)
connection
- instance of connection to attach to current thread.public static Connection detach()
Copyright © 2016 JavaLite. All rights reserved.