DATABASELINK TUTORIAL

Database Resources

DatabaseLink allows other Mathematica applications to hold resource information for database connections in DatabaseResources directories. There are a number of possible locations of DatabaseResources directories inside $InstallationDirectory, $BaseDirectory, and $UserBaseDirectory.

DatabaseResourcesPath[]DatabaseResources directories to search for resources
$InstallationDirectory/AddOns/ExtraPackages/*possible locations for DatabaseResources directories
$InstallationDirectory/AddOns/StandardPackages/*
$InstallationDirectory/AddOns/Autoload/*
$InstallationDirectory/AddOns/Applications/*
$BaseDirectory/Autoload/*
$BaseDirectory/Applications/*
$UserBaseDirectory/Autoload/*
$UserBaseDirectory/Applications/*

The command DatabaseResourcesPath shows the current locations of DatabaseResources directories.

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=

DatabaseResources directories can hold two sorts of files: those that contain JDBC settings and those that contain connection settings.

JDBC Configuration

Any file that is in a DatabaseResources directory with an extension of .m will be inspected to see if it contains possible JDBC configuration information. Here is the format of a JDBC configuration file.

In this format name is the name of the connection (as might be used in OpenSQLConnection), driverclass is the class file of the JDBC driver, and protocol is the JDBC protocol. The version of the configuration file is specified by the setting.

Here is an example file (configured for HSQLDB).

This file specifies the driver and protocol to use when OpenSQLConnection is invoked for an hsqldb connection, such as the following command.

Here is another example file (configured for Oracle).

This specifies the driver and protocol to use when OpenSQLConnection is invoked for an oracle connection, such as the following command.

Note that if you added an application to hold JDBC driver classes (as shown in "Database Connections: JDBC Connections"), you could create a DatabaseResources directory in the same application to hold JDBC configuration information. The following table shows the layout of an application, named Oracle, that could be used for connecting to the Oracle database.

$UserBaseDirectory/Applications/Oracle/Javalocation for database driver class files
$UserBaseDirectory/Applications/Oracle/DatabaseResourceslocation for JDBC configuration files

When you have installed a new JDBC driver, you might want to confirm that your new driver is accessible to the system. This is described in "Database Connections: JDBC Connections".

Connection Configuration

Any file that is in a DatabaseResources directory with an extension of .m will be inspected to see if it contains possible connection configuration information. Here is the format of a connection configuration file.

Here connectdata holds connection data (typically a JDBC setting), name is the name of the connection (as might be used in OpenSQLConnection), text is a textual description of the connection, and user and pass are the username and password to use when connecting to the database. A password of $Prompt causes a GUI to appear to enter the password. If the connection data involves a relative path, this is specified with the RelativePath setting. The version of the configuration file is specified by the setting.

Here is an example file (configured for HSQLDB).

This file specifies that HSQLDB should be used to connect to the file Examples/example, which is found relative to the location of the configuration file. The username sa and a blank password are also given. This connection information is given the name "example". This configuration file supports the following OpenSQLConnection command.

Here is another example file (configured for Oracle).

This specifies connection information to use when OpenSQLConnection is invoked with businessDB, such as the following command.

Note that if you added an application to hold JDBC driver classes (as shown in "Database Connections: JDBC Connections"), and JDBC configuration information (as shown previously), you could use the same location for holding the Oracle connection information. The following table shows the layout of an application that could be used for connecting to Oracle.

$UserBaseDirectory/Applications/Oracle/Javalocation for database driver class files
$UserBaseDirectory/Applications/Oracle/DatabaseResourceslocation for JDBC configuration files
$UserBaseDirectory/Applications/Oracle/DatabaseResourceslocation for connection configuration files

To help you to write the connection configuration file, you can use the command WriteDataSource.

In[1]:=
Click for copyable input

This creates a data source named testSource; it will use the HSQL database.

In[2]:=
Click for copyable input
Out[2]=

The new data source shows up in the listing from DataSourceNames.

In[3]:=
Click for copyable input
Out[3]=

You can connect to the data source and start to work with it. One benefit of the HSQL database is that it will create the database if it does not exist.

In[3]:=
Click for copyable input
Out[3]=

It is typically a good practice to close the connection.

In[4]:=
Click for copyable input

If you want to connect to a database other than HSQL you can give a second argument to WriteDataSource. For example, the following will write a data source file that uses a MySQL database.

In[5]:=
Click for copyable input
Out[5]=

Note that this does not communicate with the MySQL server to create the database, main/test. It is assumed that the database already exists. However, this is still a convenient way to create a named connection. Note how the parameters such as Username, Password, and Location are set. The choices for Location are "User" and "System".

If you did not wish to write the connection configuration file yourself, you could use the New Connection Wizard, described in "The Database Explorer: New Connection Wizard".

When you have made a new named connection, you might want to confirm that the new connection is accessible to the system. This is described in "Database Connections: Named Connections".

New to Mathematica? Find your learning path »
Have a question? Ask support »