AnyLogic
Expand
Font size
All Implemented Interfaces:
com.anylogic.engine.internal.Child, DatabaseConstants, Serializable

public final class Database
extends ConnectivityBase
implements DatabaseConstants, com.anylogic.engine.internal.Child
Database connection manager class
Author:
AnyLogic North America, LLC https://anylogic.com
See Also:
Serialized Form

Field Summary

Fields inherited from interface com.anylogic.engine.presentation.DatabaseConstants

DRIVER_EXCEL_SQLSHEET, DRIVER_JDBC_MICROSOFT_SQLSERVER, DRIVER_JTDS_JDBC, DRIVER_UCANACCESS

Constructor Summary

ConstructorDescription
Database(Presentable owner, String name, String fileName)
Creates new Database object for interaction with file-based (Access or Excel) database
Database(Presentable owner, String name, String fileName, String login, char[] password)
Creates new Database object for interaction with file-based (Access or Excel) database
Database(Presentable owner, String name, String jdbcDriver, String connectionURL, String login, char[] password)
Creates new Database object for interaction with database
Database(Presentable owner, String name, String sqlJdbcDriver, String host, String databaseName, String login, char[] password)
Creates new Database object for interaction with MS SQL Server database

Method Summary

Modifier and TypeMethodDescription
voidcloseResultSet(ResultSet rs)
Deprecated.
call ResultSet.close() method instead
booleanconnect()
The method connects a data source, specified by constructor parameters.
voiddisconnect()
Disconnects currently connected database
This method call has no effect if this Database object is not connected
It is strongly recommended that user explicitly commits or rolls back an active transaction (if any has been explicitly opened via Connection.setAutoCommit(boolean)) prior to calling the this method.
ConnectiongetConnection()
Returns existing Connection to the database or connects to the database if not connected
If any error occurs, throws RuntimeException
DatabaseDescriptorgetDbDescriptor() 
IntegergetFieldType(String sTableName, String sFieldName)
The methods returns SQL-type of specified field.
ObjectgetMatrix(String sqlQuery, String type)
The method executes specified SQL query and returns the produced values as 2D array of values of specified type.
ResultSetgetQueryResultSet(String queryText, String listOfFields, String keyField, String keyFieldValue)
The method returns result set produced by querying data from specified fields and rows of specified query.
ResultSetgetResultSet(String sqlQuery)
The method executes specified SQL query and returns the produced result as ResultSet object.
Map<String,String>getRow(String sqlQuery)
The method executes specified SQL query and returns the produced values.
static ResultSetgetSQLResultSet(ResultSet rs) 
static StatementgetSQLStatement(Statement statement)
Returns Java SQL Statement, backed by given Statement object
StatementgetStatement()
The method creates and returns each time new statement
Please call Statement.close() method after all needed operations are completed
ResultSetgetTableResultSet(String tableName, String listOfFields, String keyField, String keyFieldValue)
The method returns result set produced by querying data from specified fields and rows of specified table.
StringgetValue(String sqlQuery)
The method executes specified SQL query and returns the produced value.
booleanmodify(String sqlQuery)
The method executes specified SQL query.
voidreleaseStatement(Statement statement)
Deprecated.
call Statement.close() method instead
voidrestoreOwner(Object owner)
Deprecated.
voidsetExcelStreamingMode(DatabaseExcelStreamingMode mode)
Sets excel streaming mode to improve speed while working with large excel files DatabaseExcelStreamingMode.READ allows to read huge collection of data a bit faster Reconnects database
StringtoString()
Returns formatted database connection properties (excluding password value)

Methods inherited from class com.anylogic.engine.connectivity.ConnectivityBase

destroy, getName

Methods inherited from class java.lang.Object

equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructor Details

Database

public Database(Presentable owner,
 String name,
 String jdbcDriver,
 String connectionURL,
 String login,
 char[] password)
Creates new Database object for interaction with database
Parameters:
owner - this object's owner (agent or experiment)
name - object name (decoration purpose)
jdbcDriver -
connectionURL -
login -
password - the password is copied to internal variable, then given array is wiped

Database

public Database(Presentable owner,
 String name,
 String fileName)
Creates new Database object for interaction with file-based (Access or Excel) database
Parameters:
owner - this object's owner (agent or experiment)
name - object name (decoration purpose)
fileName - file name with extension .xls or .mdb

Database

public Database(Presentable owner,
 String name,
 String fileName,
 String login,
 char[] password)
Creates new Database object for interaction with file-based (Access or Excel) database
Parameters:
owner - this object's owner (agent or experiment)
name - object name (decoration purpose)
fileName - file name with extension .xls or .mdb
login -
password -

Database

public Database(Presentable owner,
 String name,
 String sqlJdbcDriver,
 String host,
 String databaseName,
 String login,
 char[] password)
Creates new Database object for interaction with MS SQL Server database
Parameters:
owner - this object's owner (agent or experiment)
name - object name (decoration purpose)
sqlJdbcDriver - one of
host -
databaseName -
login - user name or null for Windows authentication
password - password or null for Windows authentication
the password is copied to internal variable, then given array is wiped

Method Details

toString

public String toString()
Returns formatted database connection properties (excluding password value)
Overrides:
toString in class ConnectivityBase

disconnect

public void disconnect()
Disconnects currently connected database
This method call has no effect if this Database object is not connected
It is strongly recommended that user explicitly commits or rolls back an active transaction (if any has been explicitly opened via Connection.setAutoCommit(boolean)) prior to calling the this method. If this method is called and there is an active transaction, the results are generally unpredictable.
If any error occurs, throws RuntimeException

getConnection

public Connection getConnection()
Returns existing Connection to the database or connects to the database if not connected
If any error occurs, throws RuntimeException
Returns:
database connection, never null

connect

public boolean connect()
The method connects a data source, specified by constructor parameters. The connection process is performed only if the object has no connection, otherwise it do nothing.
Returns:
true if connection is set properly or already exists, false otherwise.

getStatement

public Statement getStatement()
The method creates and returns each time new statement
Please call Statement.close() method after all needed operations are completed
Returns:
statement

getValue

public String getValue(String sqlQuery)
The method executes specified SQL query and returns the produced value. The method is designed for SQL queries that return only one value (for example, SQL aggregate functions - MAX, MIN, and so on).
Parameters:
sqlQuery - SQL query to be executed
Returns:
the value produced by the query or null if an error occurs or if the query produces no values

getRow

public Map<String,String> getRow(String sqlQuery)
The method executes specified SQL query and returns the produced values. The method is designed for SQL queries that return only one row. Results are returned as a Map that contains {"Field name"; "Field value"} String pairs.
Parameters:
sqlQuery - SQL query to be executed
Returns:
Map that contains {"Field name"; "Field value"} String pairs or null if an error occurs or if the query produces no values

getMatrix

public Object getMatrix(String sqlQuery,
 String type)
The method executes specified SQL query and returns the produced values as 2D array of values of specified type. The method supports all Java primitive types and String.
Parameters:
sqlQuery - SQL query to be executed
type - one of:
  • "byte"
  • "short"
  • "int"
  • "long"
  • "float"
  • "double"
  • "boolean"
  • "String"
Returns:
the values produced by the query or null if an error occurs or if the query produces no values or if specified type is invalid

getTableResultSet

public ResultSet getTableResultSet(String tableName,
 String listOfFields,
 String keyField,
 String keyFieldValue)
The method returns result set produced by querying data from specified fields and rows of specified table.
Parameters:
tableName - Table name
listOfFields - List of fields (ignored if null)
keyField - Name of the key field (ignored if null or if sListOfFields is null)
keyFieldValue - Value of the key field (ignored if null or if sListOfFields is null)
Returns:
result set produced by querying data from specified fields and rows of specified table.
Please call ResultSet.close() method after all needed operations are completed

getQueryResultSet

public ResultSet getQueryResultSet(String queryText,
 String listOfFields,
 String keyField,
 String keyFieldValue)
The method returns result set produced by querying data from specified fields and rows of specified query.
Parameters:
queryText - Query text
listOfFields - List of fields (ignored if null)
keyField - Name of the key field (ignored if null or if sListOfFields is null)
keyFieldValue - Value of the key field (ignored if null or if sListOfFields is null)
Returns:
result set produced by querying data from specified fields and rows of specified query.
Please call ResultSet.close() method after all needed operations are completed

getResultSet

public ResultSet getResultSet(String sqlQuery)
The method executes specified SQL query and returns the produced result as ResultSet object.
Parameters:
sqlQuery - SQL query to be executed
Returns:
ResultSet produced by specified query or null if an error occurs
Please call ResultSet.close() method after all needed operations are completed

modify

public boolean modify(String sqlQuery)
The method executes specified SQL query. The method is designed for SQL queries that modify data in the data source.
Parameters:
sqlQuery - SQL query to be executed
Returns:
boolean value that indicates whether the query execution completed successfully

getFieldType

public Integer getFieldType(String sTableName,
 String sFieldName)
The methods returns SQL-type of specified field.
Parameters:
sTableName - Table name
sFieldName - Field name
Returns:
type of specified field or null if an error occurs

restoreOwner

@AnyLogicInternalCodegenAPI
@Deprecated
public void restoreOwner(Object owner)
Deprecated.
This method normally should not be called by user
This method restores owner of this object
The method is used in snapshot saving/loading
Specified by:
restoreOwner in interface com.anylogic.engine.internal.Child
Parameters:
owner - owner of this object, usually Agent, Experiment or ShapeGroup

releaseStatement

@Deprecated
public void releaseStatement(Statement statement)
Deprecated.
call Statement.close() method instead
Releases Statement
This method should be called by user, previously called getStatement()
Parameters:
statement - a Statement previously obtained through getStatement()
See Also:
getStatement()

closeResultSet

@Deprecated
public void closeResultSet(ResultSet rs)
Deprecated.
call ResultSet.close() method instead
Closes given ResultSet, obtained using preceding getTableResultSet(String, String, String, String), getQueryResultSet(String, String, String, String) or getResultSet(String) call
Also all opened ResultSets are closed automatically on this Database destroy
Parameters:
rs -

getSQLStatement

public static Statement getSQLStatement(Statement statement)
Returns Java SQL Statement, backed by given Statement object
Parameters:
statement - the Statement object, obtained by getStatement()
Returns:
Java SQL Statement, used in the given statement

getSQLResultSet

public static ResultSet getSQLResultSet(ResultSet rs)

setExcelStreamingMode

public void setExcelStreamingMode(DatabaseExcelStreamingMode mode)
Sets excel streaming mode to improve speed while working with large excel files DatabaseExcelStreamingMode.READ allows to read huge collection of data a bit faster Reconnects database
Parameters:
mode - streaming mode, NONE for default mode

getDbDescriptor

@AnyLogicInternalAPI
public DatabaseDescriptor getDbDescriptor()