public final class DB extends Object
This class is used for making SQL query's utilizing the database connection
defined in database.properties
Here are the functions that one may use to utilize this class in creating an
ease of access to database information.
Parameters:
setParams()) and read the replied data from the query
easily.
DB.select("SELECT name FROM test_table WHERE id=?", new ParamReadStH() {
public void setParams(PreparedStatement stmt) throws SQLException {
stmt.setInt(1, 50);
}
public void handleRead(ResultSet rset) throws SQLException {
while (rset.next()) {
// Usually here in the custom class you would set it to your needed
// var.
var = rset.getString("name");
}
}
});
Parameters:
DB.insertUpdate("UPDATE test_table SET some_column=1");
DB.insertUpdate("INSERT INTO test_table VALUES (?)", new IUStH() {
public void handleInsertUpdate(PreparedStatement stmt) {
// Usually this would be data from the custom class that implements
// IUSth
String[] batchTestVars = { "bob", "mike", "joe" };
for (String n : batchTestVars) {
stmt.setString(1, n);
stmt.addBatch();
}
// REQUIRED
stmt.executeBatch();
}
});
DB.insertUpdate("UPDATE test_table SET some_column=? WHERE other_column=?", new IUStH() {
public void handleInsertUpdate(PreparedStatement stmt) {
stmt.setString(1, "xxx");
stmt.setInt(2, 10);
stmt.executeUpdate();
}
});
| Modifier and Type | Field and Description |
|---|---|
protected static org.slf4j.Logger |
log
Logger
|
| Modifier and Type | Method and Description |
|---|---|
static Transaction |
beginTransaction()
Begins new transaction
|
static boolean |
call(String query,
ReadStH reader)
Call stored procedure
|
static boolean |
call(String query,
ReadStH reader,
String errMsg)
Call stored procedure
|
static void |
close(PreparedStatement statement)
Closes PreparedStatemet, it's connection and last ResultSet
|
static ResultSet |
executeQuerry(PreparedStatement statement)
Executes Querry and returns ResultSet
|
static int |
executeUpdate(PreparedStatement statement)
Executes PreparedStatement
|
static void |
executeUpdateAndClose(PreparedStatement statement)
Executes PreparedStatement and closes it and it's connection
|
static boolean |
insertUpdate(String query)
Executes Insert or Update Query not needing any further modification or
batching.
|
static boolean |
insertUpdate(String query,
IUStH batch)
Executes Insert / Update Query.
|
static boolean |
insertUpdate(String query,
IUStH batch,
String errMsg)
Executes Insert or Update Query.
|
static boolean |
insertUpdate(String query,
String errMsg)
Executes Insert or Update Query not needing any further modification or
batching.
|
static PreparedStatement |
prepareStatement(String sql)
Creates PreparedStatement with given sql string.
|
static PreparedStatement |
prepareStatement(String sql,
int resultSetType,
int resultSetConcurrency)
Creates
PreparedStatement with given sql |
static boolean |
select(String query,
ReadStH reader)
Executes Select Query.
|
static boolean |
select(String query,
ReadStH reader,
String errMsg)
Executes Select Query.
|
public static boolean select(String query, ReadStH reader)
query - reader - public static boolean select(String query, ReadStH reader, String errMsg)
query - reader - errMsg - public static boolean call(String query, ReadStH reader)
query - reader - public static boolean call(String query, ReadStH reader, String errMsg)
query - reader - errMsg - public static boolean insertUpdate(String query)
query - public static boolean insertUpdate(String query, String errMsg)
query - errMsg - public static boolean insertUpdate(String query, IUStH batch)
query - batch - public static boolean insertUpdate(String query, IUStH batch, String errMsg)
query - batch - errMsg - public static Transaction beginTransaction() throws SQLException
SQLException - if was unable to create transactionpublic static PreparedStatement prepareStatement(String sql)
ResultSet.TYPE_FORWARD_ONLY
and ResultSet.CONCUR_READ_ONLYsql - SQL querrypublic static PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
PreparedStatement with given sqlsql - SQL querryresultSetType - a result set type; one of ResultSet.TYPE_FORWARD_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVEresultSetConcurrency - a concurrency type; one of ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLEpublic static int executeUpdate(PreparedStatement statement)
statement - PreparedStatement to executePreparedStatement.executeQuery() or -1 in case
of errorpublic static void executeUpdateAndClose(PreparedStatement statement)
statement - PreparedStatement to closepublic static ResultSet executeQuerry(PreparedStatement statement)
statement - preparedStement to executepublic static void close(PreparedStatement statement)
statement - statement to closeCopyright © 2014–2015. All rights reserved.