Database API

AnyLogic Professional places a set of tools (Insert, Update, Query, Key Value Table) simplifying model connectivity with databases at your disposal. Using them you do not need to puzzle over writing SQL queries and statements any more - they can be easily composed using visual properties of the listed tools.

However, if you are AnyLogic University Researcher or AnyLogic PLE user, all this functionality is still available, but in that case you will have only Database tool at your hand. You should create Database object, connect it to the actual database and then work with this database with the help of the functions listed below:

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

public void disconnect() - Disconnects currently connected database. This function 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 function. If this function is called and there is an active transaction, the results are generally unpredictable. If any error occurs, throws RuntimeException

public java.sql.Connection getConnection() - Returns existing Connection to the database or connects to the database if not connected
If any error occurs, throws RuntimeException.

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

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

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

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

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

public Object getMatrix(String sqlQuery, String type) - The function executes specified SQL query and returns the produced values as 2D array of values of specified type  or null if an error occurs or if the query produces no values or if specified type is invalid. The function supports all Java primitive types and String.

public ResultSet getTableResultSet(String tableName, String listOfFields, String keyField, String keyFieldValue) - The function returns result set produced by querying data from specified fields and rows of specified table. Please call ResultSet.close() function after all needed operations are completed
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)

public ResultSet getQueryResultSet(String queryText, String listOfFields, String keyField, String keyFieldValue) - The function returns result set produced by querying data from specified fields and rows of specified query. Please call ResultSet.close() function after all needed operations are completed.
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