java.lang.Object
com.github.hypfvieh.db.SimpleDatabaseConnector
Simple class to allow connection to any JDBC compatible database.
Allows to send any SQL statement to the database as well as retrieving data (SELECT) from a database in a List-of-Map format.
All columns selected will be accessible by iterating through the list of maps. Each item of the list represents one row as HashMap where each key is one column. Column names are case insensitive!
It allows caching of retrieved DB results in JSON format. If caching is enabled it will automatically use the offline cache if database is unreachable.
One instance of
Sample Usage:
All columns selected will be accessible by iterating through the list of maps. Each item of the list represents one row as HashMap where each key is one column. Column names are case insensitive!
It allows caching of retrieved DB results in JSON format. If caching is enabled it will automatically use the offline cache if database is unreachable.
One instance of
SimpleDatabaseConnector can store multiple select queries in offline cache (map of SQL-Query(key) and Result (value)).
Sample Usage:
IDatabaseConnector sdc = newSqlConnectorInstance(getDatabaseConnectionParameters("EnxMasterdata"), true); // use false to disable cache
if (sdc.openDatabase() || sdc.isOffline()) { // if cache is disabled you should stop if isOffline() == true
List<Map<String, String>> selectedRows = sdc.executeSelectQuery(true, sqlGattungString);
for (Map<String, String> tableEntry : selectedRows) {
isin2enx.put(tableEntry.get("isin"), tableEntry.get("enx_code"));
}
}
- Seit:
- 1.0.1
- Autor:
- hypfvieh
-
Konstruktorübersicht
Konstruktoren -
Methodenübersicht
Modifizierer und TypMethodeBeschreibungvoidCloses the database connection if it was previously connected.Creates a prepared statement which can be used for batch statements.booleanexecuteBatchQuery(String _sqlQuery, List<Object[]> _sqlParameters, int _batchSize) Run update/inserts as batch update.booleanexecuteQuery(String _sql, Object... _args) Executes an sql query.booleanExecute a previously created prepared statement (update or delete).executeSelectQuery(String _sql, Object... _args) Returns the result of an SQL Select-PreparedStatement as list of maps where each key in the map is a column.booleanReturns status of autocommit option.booleanisDbOpen()Returns true if database has been opened.booleanfinal booleanTrys to open a connection to the given database parameters.voidsetAutoCommit(boolean _onOff) Enable/Disable autocommit on database connection.final StringtoString()
-
Konstruktordetails
-
SimpleDatabaseConnector
-
-
Methodendetails
-
openDatabase
Trys to open a connection to the given database parameters. Returns true if connection could be established, false otherwise. Throws InvalidClassException if given SQL-Driver is not JDBC compatible. Throws ClassNotFoundException if given SQL-Driver class could not be found. Throws SQLException on any connection error.- Gibt zurück:
- true if connected, false otherwise
- Löst aus:
InvalidClassException- if class is not a java.sql.Driver derivativeClassNotFoundException- if class could not be found
-
getDbConnection
-
closeDatabase
Closes the database connection if it was previously connected.- Löst aus:
SQLException- if closing fails
-
isDbOpen
public boolean isDbOpen()Returns true if database has been opened.- Gibt zurück:
- true if open, false otherwise
-
executeBatchQuery
Run update/inserts as batch update. Will fallback to sequential insert/update if database implemenation does not support batch.
Attention: Do not use batch when calling stored procedures in oracle databases, as this is not supported by oracle and will not work.- Parameter:
_sqlQuery- sql query to use (with '?' placeholders)_sqlParameters- an array of values for replacing '?' placeholders in query_batchSize- batch size to use- Gibt zurück:
- true on successful execution, false if any error occurred
-
executeQuery
Executes an sql query.- Parameter:
_sql- query to execute_args- arguments to fill-in placeholders in query, can be omitted if none needed- Gibt zurück:
- true if SQL-query returns an update-count, false on error or if result is resultset instead of update count.
-
executeSelectQuery
Returns the result of an SQL Select-PreparedStatement as list of maps where each key in the map is a column.- Parameter:
_sql- the sql statement to execute (can use '?' placeholder which will be replaced by the parameters in _args)_args- parameters to replace '?'- placeholder insert _sql (if none, this can be omitted)- Gibt zurück:
- list of maps with the result of the query, each list entry is one row of the database
-
createPreparedStatement
Creates a prepared statement which can be used for batch statements.- Parameter:
_sql- sql to create prepared statement for- Gibt zurück:
- new prepared statement or null on error
-
executeQuery
Execute a previously created prepared statement (update or delete).- Parameter:
_ps- prepared statement to execute- Gibt zurück:
- true if execution successfully, false otherwise
-
isSupportsBatch
public boolean isSupportsBatch() -
setAutoCommit
Enable/Disable autocommit on database connection.- Parameter:
_onOff- enable/disable autocommit- Löst aus:
SQLException- if autocommit option cannot be changed
-
isAutoCommit
Returns status of autocommit option.- Gibt zurück:
- true if autocommit enabled, false otherwise
- Löst aus:
SQLException- if autocommit option status could not be determined
-
toString
-