AnyLogic
Expand
Font size

Database element

 Database is an element of the AnyLogic model that represents an actual existing database and is responsible for communicating with it. By creating an instance of the Database element, you declare that your model can to access a corresponding database.

Demo model: Dumping Simulation Output into a Database Table Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).

Follow one of the instructions in this article depending on the type of your database. To navigate, use the sidebar on the right.

Connecting to MS Access/MS Excel database

You can work with MS Access and MS Excel databases only if MS Office is the same 32 or 64-bit version as AnyLogic installed on your computer. 32-bit AnyLogic requires 32-bit MS Office and 64-bit AnyLogic works only with 64-bit MS Office.

To access MS Access or MS Excel database in your model, you should link your model to a corresponding file. This approach is simple and does not require you to have anything other than one of the tools mentioned above. Its disadvantage is that it requires storing an absolute file path in an AnyLogic project. Therefore, if you change the location of a database, you must manually change the corresponding path in the AnyLogic project.

To connect AnyLogic to MS Access / MS Excel database

  1. Select the  Database element you want to associate with Access file.
  2. In the Properties view, select Excel/Access from the Type drop-down list.
  3. Specify the database file to be associated. Click the Browse button. The Open dialog box appears.
  4. Browse to the MS Access file you want to use.
  5. Double-click the file or select it and click the Open button to select the file.
  6. If necessary, use the button to the right of the File field to toggle between the absolute and relative file paths.

Connecting to MS SQL Server database

To connect AnyLogic to MS SQL Server database

  1. Select the  Database element you want to associate with MS SQL Server database.
  2. In the Properties view, select MS SQL Server from the Type drop-down list.
  3. Specify the host computer in the Host field and the name of the database you want to access in the Database name field.
  4. Enter Login and Password if they are required by the database.

Connecting to other databases using JDBC/ODBC drivers

The other way to connect to a database is to associate it with an JDBC/ODBC data source. This technique uses the so-called JDBC/ODBC drivers and makes your model independent of the database type and location.

To connect AnyLogic to a different type of database using JDBC/ODBC drivers

  1. Select the  Database element you want to associate with an actual database.
  2. In the Properties view, select Other Database from the Type drop-down list.
  3. Specify the JDBC driver you want to use in the JDBC Driver combo box.
    You must have the specified driver defined in your system or this tool will not be available. Refer to the driver documentation for installation details.
  4. Enter the Connection URL for the JDBC driver. The string syntax depends on the driver you are using, so refer to the driver documentation again.
  5. Enter Login and Password if required by the database.

Setting up the database connection on model startup

After the initial setup, you can interact with the database using the database API. You can also set up the model to connect to the database during startup.

To set up a model startup database connection

  1. Select the  Database element in the graphical editor.
  2. In the Properties view, select the Connect on startup check box. AnyLogic will attempt to connect to the specified database at model startup.
How can we improve this article?